blob: 35b4510b65edd445b4c1eaef8882958be3acbff3 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @file
19 *
20 * @brief Public kernel APIs.
21 */
22
23#ifndef _kernel__h_
24#define _kernel__h_
25
26#include <stddef.h>
27#include <stdint.h>
28#include <toolchain.h>
29#include <sections.h>
30#include <atomic.h>
31#include <errno.h>
32#include <misc/__assert.h>
33#include <misc/dlist.h>
34#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050035#include <misc/util.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040036
37#ifdef __cplusplus
38extern "C" {
39#endif
40
Anas Nashif61f4b242016-11-18 10:53:59 -050041#ifdef CONFIG_KERNEL_DEBUG
42#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040043#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
44#else
45#define K_DEBUG(fmt, ...)
46#endif
47
48#define K_PRIO_COOP(x) (-(CONFIG_NUM_COOP_PRIORITIES - (x)))
49#define K_PRIO_PREEMPT(x) (x)
50
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_ANY NULL
52#define K_END NULL
53
Benjamin Walsh456c6da2016-09-02 18:55:39 -040054#if CONFIG_NUM_COOP_PRIORITIES > 0
55#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
56#else
57#define K_HIGHEST_THREAD_PRIO 0
58#endif
59
60#if CONFIG_NUM_PREEMPT_PRIORITIES > 0
61#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
62#else
63#define K_LOWEST_THREAD_PRIO -1
64#endif
65
Benjamin Walshfab8d922016-11-08 15:36:36 -050066#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
67
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
69#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
70
71typedef sys_dlist_t _wait_q_t;
72
Anas Nashif2f203c22016-12-18 06:57:45 -050073#ifdef CONFIG_OBJECT_TRACING
74#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
75#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#else
Anas Nashif2f203c22016-12-18 06:57:45 -050077#define _OBJECT_TRACING_INIT
78#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#endif
80
Benjamin Walshf6ca7de2016-11-08 10:36:50 -050081#define tcs k_thread
82struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083struct k_mutex;
84struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -040085struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086struct k_msgq;
87struct k_mbox;
88struct k_pipe;
89struct k_fifo;
90struct k_lifo;
91struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -040092struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093struct k_mem_pool;
94struct k_timer;
95
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -040096typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098enum execution_context_types {
99 K_ISR = 0,
100 K_COOP_THREAD,
101 K_PREEMPT_THREAD,
102};
103
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400104/**
Allan Stephensc98da842016-11-11 15:45:03 -0500105 * @defgroup thread_apis Thread APIs
106 * @ingroup kernel_apis
107 * @{
108 */
109
110/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500111 * @typedef k_thread_entry_t
112 * @brief Thread entry point function type.
113 *
114 * A thread's entry point function is invoked when the thread starts executing.
115 * Up to 3 argument values can be passed to the function.
116 *
117 * The thread terminates execution permanently if the entry point function
118 * returns. The thread is responsible for releasing any shared resources
119 * it may own (such as mutexes and dynamically allocated memory), prior to
120 * returning.
121 *
122 * @param p1 First argument.
123 * @param p2 Second argument.
124 * @param p3 Third argument.
125 *
126 * @return N/A
127 */
128typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
129
130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500131 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500133 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500135 * The new thread may be scheduled for immediate execution or a delayed start.
136 * If the newly spawned thread does not have a delayed start the kernel
137 * scheduler may preempt the current thread to allow the new thread to
138 * execute.
139 *
140 * Thread options are architecture-specific, and can include K_ESSENTIAL,
141 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
142 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400143 *
144 * @param stack Pointer to the stack space.
145 * @param stack_size Stack size in bytes.
146 * @param entry Thread entry function.
147 * @param p1 1st entry point parameter.
148 * @param p2 2nd entry point parameter.
149 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500150 * @param prio Thread priority.
151 * @param options Thread options.
152 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500154 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400155 */
Benjamin Walsh669360d2016-11-14 16:46:14 -0500156extern k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500157 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400158 void *p1, void *p2, void *p3,
Benjamin Walsh669360d2016-11-14 16:46:14 -0500159 int prio, uint32_t options, int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400160
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400161/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500162 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400163 *
Allan Stephensc98da842016-11-11 15:45:03 -0500164 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500165 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500167 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400168 *
169 * @return N/A
170 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400171extern void k_sleep(int32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400172
173/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500174 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400175 *
176 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500177 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400178 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400179 * @return N/A
180 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400181extern void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400182
183/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500184 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500186 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400187 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500188 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400189 *
190 * @return N/A
191 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400192extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400193
194/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500195 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500197 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500199 * If @a thread is not currently sleeping, the routine has no effect.
200 *
201 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400202 *
203 * @return N/A
204 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400205extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400206
207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500208 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500210 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400211 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400212extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400213
214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500215 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500217 * This routine prevents @a thread from executing if it has not yet started
218 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500220 * @param thread ID of thread to cancel.
221 *
Allan Stephens9ef50f42016-11-16 15:33:31 -0500222 * @retval 0 Thread spawning cancelled.
223 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400224 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400225extern int k_thread_cancel(k_tid_t thread);
226
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400227/**
Allan Stephensc98da842016-11-11 15:45:03 -0500228 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500230 * This routine permanently stops execution of @a thread. The thread is taken
231 * off all kernel queues it is part of (i.e. the ready queue, the timeout
232 * queue, or a kernel object wait queue). However, any kernel resources the
233 * thread might currently own (such as mutexes or memory blocks) are not
234 * released. It is the responsibility of the caller of this routine to ensure
235 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500237 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400238 *
239 * @return N/A
240 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400241extern void k_thread_abort(k_tid_t thread);
242
Allan Stephensc98da842016-11-11 15:45:03 -0500243/**
244 * @cond INTERNAL_HIDDEN
245 */
246
Benjamin Walshd211a522016-12-06 11:44:01 -0500247/* timeout has timed out and is not on _timeout_q anymore */
248#define _EXPIRED (-2)
249
250/* timeout is not in use */
251#define _INACTIVE (-1)
252
Benjamin Walsh1a5450b2016-10-06 15:04:23 -0400253#ifdef CONFIG_SYS_CLOCK_EXISTS
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400254#define _THREAD_TIMEOUT_INIT(obj) \
255 (obj).nano_timeout = { \
256 .node = { {0}, {0} }, \
Benjamin Walsh055262c2016-10-05 17:16:01 -0400257 .thread = NULL, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400258 .wait_q = NULL, \
Benjamin Walshd211a522016-12-06 11:44:01 -0500259 .delta_ticks_from_prev = _INACTIVE, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260 },
261#else
262#define _THREAD_TIMEOUT_INIT(obj)
263#endif
264
265#ifdef CONFIG_ERRNO
266#define _THREAD_ERRNO_INIT(obj) (obj).errno_var = 0,
267#else
268#define _THREAD_ERRNO_INIT(obj)
269#endif
270
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400271struct _static_thread_data {
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400272 union {
273 char *init_stack;
274 struct k_thread *thread;
275 };
276 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500277 void (*init_entry)(void *, void *, void *);
278 void *init_p1;
279 void *init_p2;
280 void *init_p3;
281 int init_prio;
282 uint32_t init_options;
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400283 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500284 void (*init_abort)(void);
285 uint32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400286};
287
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400288#define _THREAD_INITIALIZER(stack, stack_size, \
289 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500290 prio, options, delay, abort, groups) \
291 { \
292 .init_stack = (stack), \
293 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400294 .init_entry = (void (*)(void *, void *, void *))entry, \
295 .init_p1 = (void *)p1, \
296 .init_p2 = (void *)p2, \
297 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500298 .init_prio = (prio), \
299 .init_options = (options), \
300 .init_delay = (delay), \
301 .init_abort = (abort), \
302 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400303 }
304
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400305/**
Allan Stephensc98da842016-11-11 15:45:03 -0500306 * INTERNAL_HIDDEN @endcond
307 */
308
309/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500310 * @brief Statically define and initialize a thread.
311 *
312 * The thread may be scheduled for immediate execution or a delayed start.
313 *
314 * Thread options are architecture-specific, and can include K_ESSENTIAL,
315 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
316 * them using "|" (the logical OR operator).
317 *
318 * The ID of the thread can be accessed using:
319 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500320 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500321 *
322 * @param name Name of the thread.
323 * @param stack_size Stack size in bytes.
324 * @param entry Thread entry function.
325 * @param p1 1st entry point parameter.
326 * @param p2 2nd entry point parameter.
327 * @param p3 3rd entry point parameter.
328 * @param prio Thread priority.
329 * @param options Thread options.
330 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400331 *
332 * @internal It has been observed that the x86 compiler by default aligns
333 * these _static_thread_data structures to 32-byte boundaries, thereby
334 * wasting space. To work around this, force a 4-byte alignment.
335 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500336#define K_THREAD_DEFINE(name, stack_size, \
337 entry, p1, p2, p3, \
338 prio, options, delay) \
339 char __noinit __stack _k_thread_obj_##name[stack_size]; \
340 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500341 __in_section(_static_thread_data, static, name) = \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500342 _THREAD_INITIALIZER(_k_thread_obj_##name, stack_size, \
343 entry, p1, p2, p3, prio, options, delay, \
Allan Stephens88095022016-10-26 14:15:08 -0500344 NULL, 0); \
345 const k_tid_t name = (k_tid_t)_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400346
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500348 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500350 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500352 * @param thread ID of thread whose priority is needed.
353 *
354 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400355 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500356extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400357
358/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500359 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500361 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400362 *
363 * Rescheduling can occur immediately depending on the priority @a thread is
364 * set to:
365 *
366 * - If its priority is raised above the priority of the caller of this
367 * function, and the caller is preemptible, @a thread will be scheduled in.
368 *
369 * - If the caller operates on itself, it lowers its priority below that of
370 * other threads in the system, and the caller is preemptible, the thread of
371 * highest priority will be scheduled in.
372 *
373 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
374 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
375 * highest priority.
376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500377 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400378 * @param prio New priority.
379 *
380 * @warning Changing the priority of a thread currently involved in mutex
381 * priority inheritance may result in undefined behavior.
382 *
383 * @return N/A
384 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400385extern void k_thread_priority_set(k_tid_t thread, int prio);
386
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500388 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500390 * This routine prevents the kernel scheduler from making @a thread the
391 * current thread. All other internal operations on @a thread are still
392 * performed; for example, any timeout it is waiting on keeps ticking,
393 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500395 * If @a thread is already suspended, the routine has no effect.
396 *
397 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400398 *
399 * @return N/A
400 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400401extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400402
403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500404 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500406 * This routine allows the kernel scheduler to make @a thread the current
407 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500409 * If @a thread is not currently suspended, the routine has no effect.
410 *
411 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400412 *
413 * @return N/A
414 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400415extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400416
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400417/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500418 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500420 * This routine specifies how the scheduler will perform time slicing of
421 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500423 * To enable time slicing, @a slice must be non-zero. The scheduler
424 * ensures that no thread runs for more than the specified time limit
425 * before other threads of that priority are given a chance to execute.
426 * Any thread whose priority is higher than @a prio is exempted, and may
427 * execute as long as desired without being pre-empted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500429 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400430 * execute. Once the scheduler selects a thread for execution, there is no
431 * minimum guaranteed time the thread will execute before threads of greater or
432 * equal priority are scheduled.
433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 * for execution, this routine has no effect; the thread is immediately
436 * rescheduled after the slice period expires.
437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500438 * To disable timeslicing, set both @a slice and @a prio to zero.
439 *
440 * @param slice Maximum time slice length (in milliseconds).
441 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400442 *
443 * @return N/A
444 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400445extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400446
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447/**
Allan Stephensc98da842016-11-11 15:45:03 -0500448 * @} end defgroup thread_apis
449 */
450
451/**
452 * @addtogroup isr_apis
453 * @{
454 */
455
456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
Allan Stephensc98da842016-11-11 15:45:03 -0500459 * This routine allows the caller to customize its actions, depending on
460 * whether it is a thread or an ISR.
461 *
462 * @note Can be called by ISRs.
463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @return 0 if invoked by a thread.
465 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400466 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500467extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400468
Benjamin Walsh445830d2016-11-10 15:54:27 -0500469/**
470 * @brief Determine if code is running in a preemptible thread.
471 *
Allan Stephensc98da842016-11-11 15:45:03 -0500472 * This routine allows the caller to customize its actions, depending on
473 * whether it can be preempted by another thread. The routine returns a 'true'
474 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500475 *
Allan Stephensc98da842016-11-11 15:45:03 -0500476 * - The code is running in a thread, not at ISR.
477 * - The thread's priority is in the preemptible range.
478 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500479 *
Allan Stephensc98da842016-11-11 15:45:03 -0500480 * @note Can be called by ISRs.
481 *
482 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500483 * @return Non-zero if invoked by a preemptible thread.
484 */
485extern int k_is_preempt_thread(void);
486
Allan Stephensc98da842016-11-11 15:45:03 -0500487/**
488 * @} end addtogroup isr_apis
489 */
490
491/**
492 * @addtogroup thread_apis
493 * @{
494 */
495
496/**
497 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500498 *
Allan Stephensc98da842016-11-11 15:45:03 -0500499 * This routine prevents the current thread from being preempted by another
500 * thread by instructing the scheduler to treat it as a cooperative thread.
501 * If the thread subsequently performs an operation that makes it unready,
502 * it will be context switched out in the normal manner. When the thread
503 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500504 *
Allan Stephensc98da842016-11-11 15:45:03 -0500505 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500506 *
Allan Stephensc98da842016-11-11 15:45:03 -0500507 * @note k_sched_lock() and k_sched_unlock() should normally be used
508 * when the operation being performed can be safely interrupted by ISRs.
509 * However, if the amount of processing involved is very small, better
510 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500511 *
512 * @return N/A
513 */
514extern void k_sched_lock(void);
515
Allan Stephensc98da842016-11-11 15:45:03 -0500516/**
517 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500518 *
Allan Stephensc98da842016-11-11 15:45:03 -0500519 * This routine reverses the effect of a previous call to k_sched_lock().
520 * A thread must call the routine once for each time it called k_sched_lock()
521 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500522 *
523 * @return N/A
524 */
525extern void k_sched_unlock(void);
526
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500528 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500530 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500532 * Custom data is not used by the kernel itself, and is freely available
533 * for a thread to use as it sees fit. It can be used as a framework
534 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500536 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400537 *
538 * @return N/A
539 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400540extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541
542/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500543 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500545 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500547 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400548 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400549extern void *k_thread_custom_data_get(void);
550
551/**
Allan Stephensc98da842016-11-11 15:45:03 -0500552 * @} end addtogroup thread_apis
553 */
554
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400555#include <sys_clock.h>
556
Allan Stephensc2f15a42016-11-17 12:24:22 -0500557/**
558 * @addtogroup clock_apis
559 * @{
560 */
561
562/**
563 * @brief Generate null timeout delay.
564 *
565 * This macro generates a timeout delay that that instructs a kernel API
566 * not to wait if the requested operation cannot be performed immediately.
567 *
568 * @return Timeout delay value.
569 */
570#define K_NO_WAIT 0
571
572/**
573 * @brief Generate timeout delay from milliseconds.
574 *
575 * This macro generates a timeout delay that that instructs a kernel API
576 * to wait up to @a ms milliseconds to perform the requested operation.
577 *
578 * @param ms Duration in milliseconds.
579 *
580 * @return Timeout delay value.
581 */
Johan Hedberg14471692016-11-13 10:52:15 +0200582#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500583
584/**
585 * @brief Generate timeout delay from seconds.
586 *
587 * This macro generates a timeout delay that that instructs a kernel API
588 * to wait up to @a s seconds to perform the requested operation.
589 *
590 * @param s Duration in seconds.
591 *
592 * @return Timeout delay value.
593 */
Johan Hedberg14471692016-11-13 10:52:15 +0200594#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500595
596/**
597 * @brief Generate timeout delay from minutes.
598 *
599 * This macro generates a timeout delay that that instructs a kernel API
600 * to wait up to @a m minutes to perform the requested operation.
601 *
602 * @param m Duration in minutes.
603 *
604 * @return Timeout delay value.
605 */
Johan Hedberg14471692016-11-13 10:52:15 +0200606#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500607
608/**
609 * @brief Generate timeout delay from hours.
610 *
611 * This macro generates a timeout delay that that instructs a kernel API
612 * to wait up to @a h hours to perform the requested operation.
613 *
614 * @param h Duration in hours.
615 *
616 * @return Timeout delay value.
617 */
Johan Hedberg14471692016-11-13 10:52:15 +0200618#define K_HOURS(h) K_MINUTES((h) * 60)
619
Allan Stephensc98da842016-11-11 15:45:03 -0500620/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500621 * @brief Generate infinite timeout delay.
622 *
623 * This macro generates a timeout delay that that instructs a kernel API
624 * to wait as long as necessary to perform the requested operation.
625 *
626 * @return Timeout delay value.
627 */
628#define K_FOREVER (-1)
629
630/**
631 * @} end addtogroup clock_apis
632 */
633
634/**
Allan Stephensc98da842016-11-11 15:45:03 -0500635 * @cond INTERNAL_HIDDEN
636 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400637
Benjamin Walsh62092182016-12-20 14:39:08 -0500638/* kernel clocks */
639
640#if (sys_clock_ticks_per_sec == 1000) || \
641 (sys_clock_ticks_per_sec == 500) || \
642 (sys_clock_ticks_per_sec == 250) || \
643 (sys_clock_ticks_per_sec == 125) || \
644 (sys_clock_ticks_per_sec == 100) || \
645 (sys_clock_ticks_per_sec == 50) || \
646 (sys_clock_ticks_per_sec == 25) || \
647 (sys_clock_ticks_per_sec == 20) || \
648 (sys_clock_ticks_per_sec == 10) || \
649 (sys_clock_ticks_per_sec == 1)
650
651 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
652#else
653 /* yields horrible 64-bit math on many architectures: try to avoid */
654 #define _NON_OPTIMIZED_TICKS_PER_SEC
655#endif
656
657#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
658extern int32_t _ms_to_ticks(int32_t ms);
659#else
660static ALWAYS_INLINE int32_t _ms_to_ticks(int32_t ms)
661{
662 return (int32_t)ceiling_fraction((uint32_t)ms, _ms_per_tick);
663}
664#endif
665
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500666/* added tick needed to account for tick in progress */
667#define _TICK_ALIGN 1
668
Benjamin Walsh62092182016-12-20 14:39:08 -0500669static inline int64_t __ticks_to_ms(int64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400670{
Benjamin Walsh62092182016-12-20 14:39:08 -0500671#ifdef CONFIG_SYS_CLOCK_EXISTS
672
673#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400674 return (MSEC_PER_SEC * (uint64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400675#else
Benjamin Walsh62092182016-12-20 14:39:08 -0500676 return (uint64_t)ticks * _ms_per_tick;
677#endif
678
679#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400680 __ASSERT(ticks == 0, "");
681 return 0;
682#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400683}
684
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400685/* timeouts */
686
687struct _timeout;
688typedef void (*_timeout_func_t)(struct _timeout *t);
689
690struct _timeout {
Benjamin Walsha2c58d52016-12-10 10:26:35 -0500691 sys_dnode_t node;
Benjamin Walsh055262c2016-10-05 17:16:01 -0400692 struct k_thread *thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400693 sys_dlist_t *wait_q;
694 int32_t delta_ticks_from_prev;
695 _timeout_func_t func;
696};
697
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200698extern int32_t _timeout_remaining_get(struct _timeout *timeout);
699
Allan Stephensc98da842016-11-11 15:45:03 -0500700/**
701 * INTERNAL_HIDDEN @endcond
702 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500703
Allan Stephensc98da842016-11-11 15:45:03 -0500704/**
705 * @cond INTERNAL_HIDDEN
706 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400707
708struct k_timer {
709 /*
710 * _timeout structure must be first here if we want to use
711 * dynamic timer allocation. timeout.node is used in the double-linked
712 * list of free timers
713 */
714 struct _timeout timeout;
715
Allan Stephens45bfa372016-10-12 12:39:42 -0500716 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400717 _wait_q_t wait_q;
718
719 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500720 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400721
722 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500723 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400724
725 /* timer period */
726 int32_t period;
727
Allan Stephens45bfa372016-10-12 12:39:42 -0500728 /* timer status */
729 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400730
Allan Stephens45bfa372016-10-12 12:39:42 -0500731 /* used to support legacy timer APIs */
732 void *_legacy_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400733
Anas Nashif2f203c22016-12-18 06:57:45 -0500734 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400735};
736
Allan Stephens1342adb2016-11-03 13:54:53 -0500737#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400738 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500739 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500740 .timeout.wait_q = NULL, \
741 .timeout.thread = NULL, \
742 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400743 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500744 .expiry_fn = expiry, \
745 .stop_fn = stop, \
746 .status = 0, \
747 ._legacy_data = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500748 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400749 }
750
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751/**
Allan Stephensc98da842016-11-11 15:45:03 -0500752 * INTERNAL_HIDDEN @endcond
753 */
754
755/**
756 * @defgroup timer_apis Timer APIs
757 * @ingroup kernel_apis
758 * @{
759 */
760
761/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500762 * @typedef k_timer_expiry_t
763 * @brief Timer expiry function type.
764 *
765 * A timer's expiry function is executed by the system clock interrupt handler
766 * each time the timer expires. The expiry function is optional, and is only
767 * invoked if the timer has been initialized with one.
768 *
769 * @param timer Address of timer.
770 *
771 * @return N/A
772 */
773typedef void (*k_timer_expiry_t)(struct k_timer *timer);
774
775/**
776 * @typedef k_timer_stop_t
777 * @brief Timer stop function type.
778 *
779 * A timer's stop function is executed if the timer is stopped prematurely.
780 * The function runs in the context of the thread that stops the timer.
781 * The stop function is optional, and is only invoked if the timer has been
782 * initialized with one.
783 *
784 * @param timer Address of timer.
785 *
786 * @return N/A
787 */
788typedef void (*k_timer_stop_t)(struct k_timer *timer);
789
790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500791 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500793 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500795 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
797 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @param expiry_fn Function to invoke each time the timer expires.
799 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 */
Allan Stephens1342adb2016-11-03 13:54:53 -0500801#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500802 struct k_timer name \
803 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -0500804 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400805
Allan Stephens45bfa372016-10-12 12:39:42 -0500806/**
807 * @brief Initialize a timer.
808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -0500810 *
811 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * @param expiry_fn Function to invoke each time the timer expires.
813 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -0500814 *
815 * @return N/A
816 */
817extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -0500818 k_timer_expiry_t expiry_fn,
819 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -0700820
Allan Stephens45bfa372016-10-12 12:39:42 -0500821/**
822 * @brief Start a timer.
823 *
824 * This routine starts a timer, and resets its status to zero. The timer
825 * begins counting down using the specified duration and period values.
826 *
827 * Attempting to start a timer that is already running is permitted.
828 * The timer's status is reset to zero and the timer begins counting down
829 * using the new duration and period values.
830 *
831 * @param timer Address of timer.
832 * @param duration Initial timer duration (in milliseconds).
833 * @param period Timer period (in milliseconds).
834 *
835 * @return N/A
836 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400837extern void k_timer_start(struct k_timer *timer,
Allan Stephens45bfa372016-10-12 12:39:42 -0500838 int32_t duration, int32_t period);
839
840/**
841 * @brief Stop a timer.
842 *
843 * This routine stops a running timer prematurely. The timer's stop function,
844 * if one exists, is invoked by the caller.
845 *
846 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -0500848 *
849 * @param timer Address of timer.
850 *
851 * @return N/A
852 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400853extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500854
855/**
856 * @brief Read timer status.
857 *
858 * This routine reads the timer's status, which indicates the number of times
859 * it has expired since its status was last read.
860 *
861 * Calling this routine resets the timer's status to zero.
862 *
863 * @param timer Address of timer.
864 *
865 * @return Timer status.
866 */
867extern uint32_t k_timer_status_get(struct k_timer *timer);
868
869/**
870 * @brief Synchronize thread to timer expiration.
871 *
872 * This routine blocks the calling thread until the timer's status is non-zero
873 * (indicating that it has expired at least once since it was last examined)
874 * or the timer is stopped. If the timer status is already non-zero,
875 * or the timer is already stopped, the caller continues without waiting.
876 *
877 * Calling this routine resets the timer's status to zero.
878 *
879 * This routine must not be used by interrupt handlers, since they are not
880 * allowed to block.
881 *
882 * @param timer Address of timer.
883 *
884 * @return Timer status.
885 */
886extern uint32_t k_timer_status_sync(struct k_timer *timer);
887
888/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -0500890 *
891 * This routine computes the (approximate) time remaining before a running
892 * timer next expires. If the timer is not running, it returns zero.
893 *
894 * @param timer Address of timer.
895 *
896 * @return Remaining time (in milliseconds).
897 */
Johan Hedbergf99ad3f2016-12-09 10:39:49 +0200898static inline int32_t k_timer_remaining_get(struct k_timer *timer)
899{
900 return _timeout_remaining_get(&timer->timeout);
901}
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400902
Allan Stephensc98da842016-11-11 15:45:03 -0500903/**
904 * @} end defgroup timer_apis
905 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400906
Allan Stephensc98da842016-11-11 15:45:03 -0500907/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500908 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -0500909 * @{
910 */
Allan Stephens45bfa372016-10-12 12:39:42 -0500911
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500913 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500915 * This routine returns the elapsed time since the system booted,
916 * in milliseconds.
917 *
918 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400919 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400920extern int64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400921
922/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * This routine returns the lower 32-bits of the elapsed time since the system
926 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 * This routine can be more efficient than k_uptime_get(), as it reduces the
929 * need for interrupt locking and 64-bit math. However, the 32-bit result
930 * cannot hold a system uptime time larger than approximately 50 days, so the
931 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400934 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400935extern uint32_t k_uptime_get_32(void);
936
937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500940 * This routine computes the elapsed time between the current system uptime
941 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @param reftime Pointer to a reference time, which is updated to the current
944 * uptime upon return.
945 *
946 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400947 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400948extern int64_t k_uptime_delta(int64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400949
950/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500951 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 * This routine computes the elapsed time between the current system uptime
954 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500956 * This routine can be more efficient than k_uptime_delta(), as it reduces the
957 * need for interrupt locking and 64-bit math. However, the 32-bit result
958 * cannot hold an elapsed time larger than approximately 50 days, so the
959 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500961 * @param reftime Pointer to a reference time, which is updated to the current
962 * uptime upon return.
963 *
964 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400965 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -0400966extern uint32_t k_uptime_delta_32(int64_t *reftime);
967
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500969 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500971 * This routine returns the current time, as measured by the system's hardware
972 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400976extern uint32_t k_cycle_get_32(void);
977
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400978/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500979 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400980 */
981
Allan Stephensc98da842016-11-11 15:45:03 -0500982/**
983 * @cond INTERNAL_HIDDEN
984 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400985
986struct k_fifo {
987 _wait_q_t wait_q;
988 sys_slist_t data_q;
989
Anas Nashif2f203c22016-12-18 06:57:45 -0500990 _OBJECT_TRACING_NEXT_PTR(k_fifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400991};
992
Allan Stephensc98da842016-11-11 15:45:03 -0500993#define K_FIFO_INITIALIZER(obj) \
994 { \
995 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
996 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Anas Nashif2f203c22016-12-18 06:57:45 -0500997 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -0500998 }
999
1000/**
1001 * INTERNAL_HIDDEN @endcond
1002 */
1003
1004/**
1005 * @defgroup fifo_apis Fifo APIs
1006 * @ingroup kernel_apis
1007 * @{
1008 */
1009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 *
1017 * @return N/A
1018 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001019extern void k_fifo_init(struct k_fifo *fifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020
1021/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * This routine adds a data item to @a fifo. A fifo data item must be
1025 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1026 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * @note Can be called by ISRs.
1029 *
1030 * @param fifo Address of the fifo.
1031 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032 *
1033 * @return N/A
1034 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001035extern void k_fifo_put(struct k_fifo *fifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036
1037/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * This routine adds a list of data items to @a fifo in one operation.
1041 * The data items must be in a singly-linked list, with the first 32 bits
1042 * each data item pointing to the next data item; the list must be
1043 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * @param fifo Address of the fifo.
1048 * @param head Pointer to first node in singly-linked list.
1049 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 *
1051 * @return N/A
1052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053extern void k_fifo_put_list(struct k_fifo *fifo, void *head, void *tail);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine adds a list of data items to @a fifo in one operation.
1059 * The data items must be in a singly-linked list implemented using a
1060 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 * and must be re-initialized via sys_slist_init().
1062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001063 * @note Can be called by ISRs.
1064 *
1065 * @param fifo Address of the fifo.
1066 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001067 *
1068 * @return N/A
1069 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070extern void k_fifo_put_slist(struct k_fifo *fifo, sys_slist_t *list);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001071
1072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * This routine removes a data item from @a fifo in a "first in, first out"
1076 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1079 *
1080 * @param fifo Address of the fifo.
1081 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 * or one of the special values K_NO_WAIT and K_FOREVER.
1083 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001084 * @return Address of the data item if successful; NULL if returned
1085 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001087extern void *k_fifo_get(struct k_fifo *fifo, int32_t timeout);
1088
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001094 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001096 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001099 struct k_fifo name \
1100 __in_section(_k_fifo, static, name) = \
1101 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001102
Allan Stephensc98da842016-11-11 15:45:03 -05001103/**
1104 * @} end defgroup fifo_apis
1105 */
1106
1107/**
1108 * @cond INTERNAL_HIDDEN
1109 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001110
1111struct k_lifo {
1112 _wait_q_t wait_q;
1113 void *list;
1114
Anas Nashif2f203c22016-12-18 06:57:45 -05001115 _OBJECT_TRACING_NEXT_PTR(k_lifo);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001116};
1117
Allan Stephensc98da842016-11-11 15:45:03 -05001118#define K_LIFO_INITIALIZER(obj) \
1119 { \
1120 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1121 .list = NULL, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001122 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001123 }
1124
1125/**
1126 * INTERNAL_HIDDEN @endcond
1127 */
1128
1129/**
1130 * @defgroup lifo_apis Lifo APIs
1131 * @ingroup kernel_apis
1132 * @{
1133 */
1134
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001138 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001140 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141 *
1142 * @return N/A
1143 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001144extern void k_lifo_init(struct k_lifo *lifo);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145
1146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001147 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001149 * This routine adds a data item to @a lifo. A lifo data item must be
1150 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1151 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001153 * @note Can be called by ISRs.
1154 *
1155 * @param lifo Address of the lifo.
1156 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
1158 * @return N/A
1159 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001160extern void k_lifo_put(struct k_lifo *lifo, void *data);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161
1162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * This routine removes a data item from @a lifo in a "last in, first out"
1166 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1169 *
1170 * @param lifo Address of the lifo.
1171 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001172 * or one of the special values K_NO_WAIT and K_FOREVER.
1173 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001174 * @return Address of the data item if successful; NULL if returned
1175 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001176 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001177extern void *k_lifo_get(struct k_lifo *lifo, int32_t timeout);
1178
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001183 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001184 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001187 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001189 struct k_lifo name \
1190 __in_section(_k_lifo, static, name) = \
1191 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192
Allan Stephensc98da842016-11-11 15:45:03 -05001193/**
1194 * @} end defgroup lifo_apis
1195 */
1196
1197/**
1198 * @cond INTERNAL_HIDDEN
1199 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200
1201struct k_stack {
1202 _wait_q_t wait_q;
1203 uint32_t *base, *next, *top;
1204
Anas Nashif2f203c22016-12-18 06:57:45 -05001205 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001206};
1207
Allan Stephensc98da842016-11-11 15:45:03 -05001208#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1209 { \
1210 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1211 .base = stack_buffer, \
1212 .next = stack_buffer, \
1213 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001214 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001215 }
1216
1217/**
1218 * INTERNAL_HIDDEN @endcond
1219 */
1220
1221/**
1222 * @defgroup stack_apis Stack APIs
1223 * @ingroup kernel_apis
1224 * @{
1225 */
1226
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001227/**
1228 * @brief Initialize a stack.
1229 *
1230 * This routine initializes a stack object, prior to its first use.
1231 *
1232 * @param stack Address of the stack.
1233 * @param buffer Address of array used to hold stacked values.
1234 * @param num_entries Maximum number of values that can be stacked.
1235 *
1236 * @return N/A
1237 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001238extern void k_stack_init(struct k_stack *stack,
1239 uint32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240
1241/**
1242 * @brief Push an element onto a stack.
1243 *
1244 * This routine adds a 32-bit value @a data to @a stack.
1245 *
1246 * @note Can be called by ISRs.
1247 *
1248 * @param stack Address of the stack.
1249 * @param data Value to push onto the stack.
1250 *
1251 * @return N/A
1252 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001253extern void k_stack_push(struct k_stack *stack, uint32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254
1255/**
1256 * @brief Pop an element from a stack.
1257 *
1258 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1259 * manner and stores the value in @a data.
1260 *
1261 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1262 *
1263 * @param stack Address of the stack.
1264 * @param data Address of area to hold the value popped from the stack.
1265 * @param timeout Waiting period to obtain a value (in milliseconds),
1266 * or one of the special values K_NO_WAIT and K_FOREVER.
1267 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001268 * @retval 0 Element popped from stack.
1269 * @retval -EBUSY Returned without waiting.
1270 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001271 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001272extern int k_stack_pop(struct k_stack *stack, uint32_t *data, int32_t timeout);
1273
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001279 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * @param name Name of the stack.
1282 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001284#define K_STACK_DEFINE(name, stack_num_entries) \
1285 uint32_t __noinit \
1286 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001287 struct k_stack name \
1288 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001289 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1290 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001292/**
Allan Stephensc98da842016-11-11 15:45:03 -05001293 * @} end defgroup stack_apis
1294 */
1295
Allan Stephens6bba9b02016-11-16 14:56:54 -05001296struct k_work;
1297
Allan Stephensc98da842016-11-11 15:45:03 -05001298/**
1299 * @defgroup workqueue_apis Workqueue Thread APIs
1300 * @ingroup kernel_apis
1301 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001302 */
1303
Allan Stephens6bba9b02016-11-16 14:56:54 -05001304/**
1305 * @typedef k_work_handler_t
1306 * @brief Work item handler function type.
1307 *
1308 * A work item's handler function is executed by a workqueue's thread
1309 * when the work item is processed by the workqueue.
1310 *
1311 * @param work Address of the work item.
1312 *
1313 * @return N/A
1314 */
1315typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001316
1317/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001318 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001319 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001320
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001321struct k_work_q {
1322 struct k_fifo fifo;
1323};
1324
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001326 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001327};
1328
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001329struct k_work {
1330 void *_reserved; /* Used by k_fifo implementation. */
1331 k_work_handler_t handler;
1332 atomic_t flags[1];
1333};
1334
Allan Stephens6bba9b02016-11-16 14:56:54 -05001335struct k_delayed_work {
1336 struct k_work work;
1337 struct _timeout timeout;
1338 struct k_work_q *work_q;
1339};
1340
1341extern struct k_work_q k_sys_work_q;
1342
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001343/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001344 * INTERNAL_HIDDEN @endcond
1345 */
1346
1347/**
1348 * @brief Initialize a statically-defined work item.
1349 *
1350 * This macro can be used to initialize a statically-defined workqueue work
1351 * item, prior to its first use. For example,
1352 *
1353 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1354 *
1355 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001356 */
1357#define K_WORK_INITIALIZER(work_handler) \
1358 { \
1359 ._reserved = NULL, \
1360 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001361 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001362 }
1363
1364/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001365 * @brief Initialize a work item.
1366 *
1367 * This routine initializes a workqueue work item, prior to its first use.
1368 *
1369 * @param work Address of work item.
1370 * @param handler Function to invoke each time work item is processed.
1371 *
1372 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373 */
1374static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1375{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001376 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377 work->handler = handler;
1378}
1379
1380/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001381 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001382 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001383 * This routine submits work item @a work to be processed by workqueue
1384 * @a work_q. If the work item is already pending in the workqueue's queue
1385 * as a result of an earlier submission, this routine has no effect on the
1386 * work item. If the work item has already been processed, or is currently
1387 * being processed, its work is considered complete and the work item can be
1388 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001389 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001390 * @warning
1391 * A submitted work item must not be modified until it has been processed
1392 * by the workqueue.
1393 *
1394 * @note Can be called by ISRs.
1395 *
1396 * @param work_q Address of workqueue.
1397 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001398 *
1399 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400 */
1401static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1402 struct k_work *work)
1403{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001404 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405 k_fifo_put(&work_q->fifo, work);
1406 }
1407}
1408
1409/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001410 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001411 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001412 * This routine indicates if work item @a work is pending in a workqueue's
1413 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001414 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001415 * @note Can be called by ISRs.
1416 *
1417 * @param work Address of work item.
1418 *
1419 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001420 */
1421static inline int k_work_pending(struct k_work *work)
1422{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001423 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001424}
1425
1426/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001427 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001428 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001429 * This routine starts workqueue @a work_q. The workqueue spawns its work
1430 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001431 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001432 * @param work_q Address of workqueue.
1433 * @param stack Pointer to work queue thread's stack space.
1434 * @param stack_size Size of the work queue thread's stack (in bytes).
1435 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436 *
1437 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438 */
Allan Stephens904cf972016-10-07 13:59:23 -05001439extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001440 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001442/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001443 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001444 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001445 * This routine initializes a workqueue delayed work item, prior to
1446 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001447 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001448 * @param work Address of delayed work item.
1449 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450 *
1451 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001452 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001453extern void k_delayed_work_init(struct k_delayed_work *work,
1454 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455
1456/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001457 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001458 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001459 * This routine schedules work item @a work to be processed by workqueue
1460 * @a work_q after a delay of @a delay milliseconds. The routine initiates
1461 * an asychronous countdown for the work item and then returns to the caller.
1462 * Only when the countdown completes is the work item actually submitted to
1463 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001465 * Submitting a previously submitted delayed work item that is still
1466 * counting down cancels the existing submission and restarts the countdown
1467 * using the new delay. If the work item is currently pending on the
1468 * workqueue's queue because the countdown has completed it is too late to
1469 * resubmit the item, and resubmission fails without impacting the work item.
1470 * If the work item has already been processed, or is currently being processed,
1471 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001472 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001473 * @warning
1474 * A delayed work item must not be modified until it has been processed
1475 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001476 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001477 * @note Can be called by ISRs.
1478 *
1479 * @param work_q Address of workqueue.
1480 * @param work Address of delayed work item.
1481 * @param delay Delay before submitting the work item (in milliseconds).
1482 *
1483 * @retval 0 Work item countdown started.
1484 * @retval -EINPROGRESS Work item is already pending.
1485 * @retval -EINVAL Work item is being processed or has completed its work.
1486 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001487 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001488extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
1489 struct k_delayed_work *work,
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001490 int32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491
1492/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001493 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001494 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001495 * This routine cancels the submission of delayed work item @a work.
1496 * A delayed work item can only be cancelled while its countdown is still
1497 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001498 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001499 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001500 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001501 * @param work Address of delayed work item.
1502 *
1503 * @retval 0 Work item countdown cancelled.
1504 * @retval -EINPROGRESS Work item is already pending.
1505 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001506 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001507extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001508
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001509/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001510 * @brief Submit a work item to the system workqueue.
1511 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001512 * This routine submits work item @a work to be processed by the system
1513 * workqueue. If the work item is already pending in the workqueue's queue
1514 * as a result of an earlier submission, this routine has no effect on the
1515 * work item. If the work item has already been processed, or is currently
1516 * being processed, its work is considered complete and the work item can be
1517 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001518 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001519 * @warning
1520 * Work items submitted to the system workqueue should avoid using handlers
1521 * that block or yield since this may prevent the system workqueue from
1522 * processing other work items in a timely manner.
1523 *
1524 * @note Can be called by ISRs.
1525 *
1526 * @param work Address of work item.
1527 *
1528 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001529 */
1530static inline void k_work_submit(struct k_work *work)
1531{
1532 k_work_submit_to_queue(&k_sys_work_q, work);
1533}
1534
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001535/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536 * @brief Submit a delayed work item to the system workqueue.
1537 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001538 * This routine schedules work item @a work to be processed by the system
1539 * workqueue after a delay of @a delay milliseconds. The routine initiates
1540 * an asychronous countdown for the work item and then returns to the caller.
1541 * Only when the countdown completes is the work item actually submitted to
1542 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001543 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001544 * Submitting a previously submitted delayed work item that is still
1545 * counting down cancels the existing submission and restarts the countdown
1546 * using the new delay. If the work item is currently pending on the
1547 * workqueue's queue because the countdown has completed it is too late to
1548 * resubmit the item, and resubmission fails without impacting the work item.
1549 * If the work item has already been processed, or is currently being processed,
1550 * its work is considered complete and the work item can be resubmitted.
1551 *
1552 * @warning
1553 * Work items submitted to the system workqueue should avoid using handlers
1554 * that block or yield since this may prevent the system workqueue from
1555 * processing other work items in a timely manner.
1556 *
1557 * @note Can be called by ISRs.
1558 *
1559 * @param work Address of delayed work item.
1560 * @param delay Delay before submitting the work item (in milliseconds).
1561 *
1562 * @retval 0 Work item countdown started.
1563 * @retval -EINPROGRESS Work item is already pending.
1564 * @retval -EINVAL Work item is being processed or has completed its work.
1565 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001566 */
1567static inline int k_delayed_work_submit(struct k_delayed_work *work,
Allan Stephens6bba9b02016-11-16 14:56:54 -05001568 int32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001569{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001570 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001571}
1572
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02001574 * @brief Get time remaining before a delayed work gets scheduled.
1575 *
1576 * This routine computes the (approximate) time remaining before a
1577 * delayed work gets executed. If the delayed work is not waiting to be
1578 * schedules, it returns zero.
1579 *
1580 * @param work Delayed work item.
1581 *
1582 * @return Remaining time (in milliseconds).
1583 */
1584static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
1585{
1586 return _timeout_remaining_get(&work->timeout);
1587}
1588
1589/**
Allan Stephensc98da842016-11-11 15:45:03 -05001590 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001591 */
1592
Allan Stephensc98da842016-11-11 15:45:03 -05001593/**
1594 * @cond INTERNAL_HIDDEN
1595 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001596
1597struct k_mutex {
1598 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04001599 struct k_thread *owner;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001600 uint32_t lock_count;
1601 int owner_orig_prio;
1602#ifdef CONFIG_OBJECT_MONITOR
1603 int num_lock_state_changes;
1604 int num_conflicts;
1605#endif
1606
Anas Nashif2f203c22016-12-18 06:57:45 -05001607 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001608};
1609
1610#ifdef CONFIG_OBJECT_MONITOR
1611#define _MUTEX_INIT_OBJECT_MONITOR \
1612 .num_lock_state_changes = 0, .num_conflicts = 0,
1613#else
1614#define _MUTEX_INIT_OBJECT_MONITOR
1615#endif
1616
1617#define K_MUTEX_INITIALIZER(obj) \
1618 { \
1619 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1620 .owner = NULL, \
1621 .lock_count = 0, \
1622 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
1623 _MUTEX_INIT_OBJECT_MONITOR \
Anas Nashif2f203c22016-12-18 06:57:45 -05001624 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001625 }
1626
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001627/**
Allan Stephensc98da842016-11-11 15:45:03 -05001628 * INTERNAL_HIDDEN @endcond
1629 */
1630
1631/**
1632 * @defgroup mutex_apis Mutex APIs
1633 * @ingroup kernel_apis
1634 * @{
1635 */
1636
1637/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001638 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001640 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001641 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001642 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001645 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001646#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001647 struct k_mutex name \
1648 __in_section(_k_mutex, static, name) = \
1649 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001650
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001652 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001654 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * Upon completion, the mutex is available and does not have an owner.
1657 *
1658 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001659 *
1660 * @return N/A
1661 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001662extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001663
1664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001665 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001667 * This routine locks @a mutex. If the mutex is locked by another thread,
1668 * the calling thread waits until the mutex becomes available or until
1669 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001671 * A thread is permitted to lock a mutex it has already locked. The operation
1672 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001674 * @param mutex Address of the mutex.
1675 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001676 * or one of the special values K_NO_WAIT and K_FOREVER.
1677 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001678 * @retval 0 Mutex locked.
1679 * @retval -EBUSY Returned without waiting.
1680 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001682extern int k_mutex_lock(struct k_mutex *mutex, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001683
1684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001687 * This routine unlocks @a mutex. The mutex must already be locked by the
1688 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001689 *
1690 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001692 * thread.
1693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001695 *
1696 * @return N/A
1697 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001698extern void k_mutex_unlock(struct k_mutex *mutex);
1699
Allan Stephensc98da842016-11-11 15:45:03 -05001700/**
1701 * @} end defgroup mutex_apis
1702 */
1703
1704/**
1705 * @cond INTERNAL_HIDDEN
1706 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001707
1708struct k_sem {
1709 _wait_q_t wait_q;
1710 unsigned int count;
1711 unsigned int limit;
1712
Anas Nashif2f203c22016-12-18 06:57:45 -05001713 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001714};
1715
Allan Stephensc98da842016-11-11 15:45:03 -05001716#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
1717 { \
1718 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1719 .count = initial_count, \
1720 .limit = count_limit, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001721 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001722 }
1723
1724/**
1725 * INTERNAL_HIDDEN @endcond
1726 */
1727
1728/**
1729 * @defgroup semaphore_apis Semaphore APIs
1730 * @ingroup kernel_apis
1731 * @{
1732 */
1733
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001734/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * @param sem Address of the semaphore.
1740 * @param initial_count Initial semaphore count.
1741 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001742 *
1743 * @return N/A
1744 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001745extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
1746 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001747
1748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1754 *
1755 * @param sem Address of the semaphore.
1756 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001757 * or one of the special values K_NO_WAIT and K_FOREVER.
1758 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05001759 * @note When porting code from the nanokernel legacy API to the new API, be
1760 * careful with the return value of this function. The return value is the
1761 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
1762 * non-zero means failure, while the nano_sem_take family returns 1 for success
1763 * and 0 for failure.
1764 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001765 * @retval 0 Semaphore taken.
1766 * @retval -EBUSY Returned without waiting.
1767 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001768 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001769extern int k_sem_take(struct k_sem *sem, int32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001770
1771/**
1772 * @brief Give a semaphore.
1773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * This routine gives @a sem, unless the semaphore is already at its maximum
1775 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001777 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001780 *
1781 * @return N/A
1782 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001783extern void k_sem_give(struct k_sem *sem);
1784
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001785/**
1786 * @brief Reset a semaphore's count to zero.
1787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001788 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001791 *
1792 * @return N/A
1793 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04001794static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001795{
1796 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001797}
1798
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001799/**
1800 * @brief Get a semaphore's count.
1801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001806 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001807 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02001808static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001809{
1810 return sem->count;
1811}
1812
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001814 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001817 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001818 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * @param name Name of the semaphore.
1821 * @param initial_count Initial semaphore count.
1822 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04001823 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001824#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001825 struct k_sem name \
1826 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001827 K_SEM_INITIALIZER(name, initial_count, count_limit)
1828
Allan Stephensc98da842016-11-11 15:45:03 -05001829/**
1830 * @} end defgroup semaphore_apis
1831 */
1832
1833/**
1834 * @defgroup alert_apis Alert APIs
1835 * @ingroup kernel_apis
1836 * @{
1837 */
1838
Allan Stephens5eceb852016-11-16 10:16:30 -05001839/**
1840 * @typedef k_alert_handler_t
1841 * @brief Alert handler function type.
1842 *
1843 * An alert's alert handler function is invoked by the system workqueue
1844 * when the alert is signalled. The alert handler function is optional,
1845 * and is only invoked if the alert has been initialized with one.
1846 *
1847 * @param alert Address of the alert.
1848 *
1849 * @return 0 if alert has been consumed; non-zero if alert should pend.
1850 */
1851typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05001852
1853/**
1854 * @} end defgroup alert_apis
1855 */
1856
1857/**
1858 * @cond INTERNAL_HIDDEN
1859 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001860
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001861#define K_ALERT_DEFAULT NULL
1862#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001863
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001864struct k_alert {
1865 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001866 atomic_t send_count;
1867 struct k_work work_item;
1868 struct k_sem sem;
1869
Anas Nashif2f203c22016-12-18 06:57:45 -05001870 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001871};
1872
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001873extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001874
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001875#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001876 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001877 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001878 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001879 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001880 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05001881 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001882 }
1883
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884/**
Allan Stephensc98da842016-11-11 15:45:03 -05001885 * INTERNAL_HIDDEN @endcond
1886 */
1887
1888/**
1889 * @addtogroup alert_apis
1890 * @{
1891 */
1892
1893/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001894 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001896 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001897 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001898 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * @param name Name of the alert.
1901 * @param alert_handler Action to take when alert is sent. Specify either
1902 * the address of a function to be invoked by the system workqueue
1903 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
1904 * K_ALERT_DEFAULT (which causes the alert to pend).
1905 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001907#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001908 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001909 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001910 K_ALERT_INITIALIZER(name, alert_handler, \
1911 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001912
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001914 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001916 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001918 * @param alert Address of the alert.
1919 * @param handler Action to take when alert is sent. Specify either the address
1920 * of a function to be invoked by the system workqueue thread,
1921 * K_ALERT_IGNORE (which causes the alert to be ignored), or
1922 * K_ALERT_DEFAULT (which causes the alert to pend).
1923 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 *
1925 * @return N/A
1926 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04001927extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
1928 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001929
1930/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001931 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001935 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1936 *
1937 * @param alert Address of the alert.
1938 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939 * or one of the special values K_NO_WAIT and K_FOREVER.
1940 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001941 * @retval 0 Alert received.
1942 * @retval -EBUSY Returned without waiting.
1943 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001944 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001945extern int k_alert_recv(struct k_alert *alert, int32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001946
1947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001948 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001950 * This routine signals @a alert. The action specified for @a alert will
1951 * be taken, which may trigger the execution of an alert handler function
1952 * and/or cause the alert to pend (assuming the alert has not reached its
1953 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001955 * @note Can be called by ISRs.
1956 *
1957 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958 *
1959 * @return N/A
1960 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04001961extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962
1963/**
Allan Stephensc98da842016-11-11 15:45:03 -05001964 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965 */
1966
Allan Stephensc98da842016-11-11 15:45:03 -05001967/**
1968 * @cond INTERNAL_HIDDEN
1969 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970
1971struct k_msgq {
1972 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04001973 size_t msg_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974 uint32_t max_msgs;
1975 char *buffer_start;
1976 char *buffer_end;
1977 char *read_ptr;
1978 char *write_ptr;
1979 uint32_t used_msgs;
1980
Anas Nashif2f203c22016-12-18 06:57:45 -05001981 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001982};
1983
Peter Mitsis1da807e2016-10-06 11:36:59 -04001984#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985 { \
1986 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001987 .max_msgs = q_max_msgs, \
1988 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001989 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04001990 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991 .read_ptr = q_buffer, \
1992 .write_ptr = q_buffer, \
1993 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001994 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995 }
1996
Peter Mitsis1da807e2016-10-06 11:36:59 -04001997/**
Allan Stephensc98da842016-11-11 15:45:03 -05001998 * INTERNAL_HIDDEN @endcond
1999 */
2000
2001/**
2002 * @defgroup msgq_apis Message Queue APIs
2003 * @ingroup kernel_apis
2004 * @{
2005 */
2006
2007/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002008 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002010 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2011 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002012 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2013 * message is similarly aligned to this boundary, @a q_msg_size must also be
2014 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002016 * The message queue can be accessed outside the module where it is defined
2017 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002019 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002021 * @param q_name Name of the message queue.
2022 * @param q_msg_size Message size (in bytes).
2023 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002024 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002025 */
2026#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2027 static char __noinit __aligned(q_align) \
2028 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002029 struct k_msgq q_name \
2030 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002031 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2032 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033
Peter Mitsisd7a37502016-10-13 11:37:40 -04002034/**
2035 * @brief Initialize a message queue.
2036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002037 * This routine initializes a message queue object, prior to its first use.
2038 *
Allan Stephensda827222016-11-09 14:23:58 -06002039 * The message queue's ring buffer must contain space for @a max_msgs messages,
2040 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2041 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2042 * that each message is similarly aligned to this boundary, @a q_msg_size
2043 * must also be a multiple of N.
2044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002045 * @param q Address of the message queue.
2046 * @param buffer Pointer to ring buffer that holds queued messages.
2047 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002048 * @param max_msgs Maximum number of messages that can be queued.
2049 *
2050 * @return N/A
2051 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002052extern void k_msgq_init(struct k_msgq *q, char *buffer,
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002053 size_t msg_size, uint32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002054
2055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002059 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002060 * @note Can be called by ISRs.
2061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002062 * @param q Address of the message queue.
2063 * @param data Pointer to the message.
2064 * @param timeout Waiting period to add the message (in milliseconds),
2065 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002066 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002067 * @retval 0 Message sent.
2068 * @retval -ENOMSG Returned without waiting or queue purged.
2069 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071extern int k_msgq_put(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002072
2073/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * This routine receives a message from message queue @a q in a "first in,
2077 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002078 *
Allan Stephensc98da842016-11-11 15:45:03 -05002079 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002081 * @param q Address of the message queue.
2082 * @param data Address of area to hold the received message.
2083 * @param timeout Waiting period to receive the message (in milliseconds),
2084 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002085 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002086 * @retval 0 Message received.
2087 * @retval -ENOMSG Returned without waiting.
2088 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002089 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002090extern int k_msgq_get(struct k_msgq *q, void *data, int32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002091
2092/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * This routine discards all unreceived messages in a message queue's ring
2096 * buffer. Any threads that are blocked waiting to send a message to the
2097 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002100 *
2101 * @return N/A
2102 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002103extern void k_msgq_purge(struct k_msgq *q);
2104
Peter Mitsis67be2492016-10-07 11:44:34 -04002105/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002108 * This routine returns the number of unused entries in a message queue's
2109 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002111 * @param q Address of the message queue.
2112 *
2113 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002114 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002115static inline uint32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002116{
2117 return q->max_msgs - q->used_msgs;
2118}
2119
Peter Mitsisd7a37502016-10-13 11:37:40 -04002120/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002125 * @param q Address of the message queue.
2126 *
2127 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002128 */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002129static inline uint32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002130{
2131 return q->used_msgs;
2132}
2133
Allan Stephensc98da842016-11-11 15:45:03 -05002134/**
2135 * @} end defgroup msgq_apis
2136 */
2137
2138/**
2139 * @defgroup mem_pool_apis Memory Pool APIs
2140 * @ingroup kernel_apis
2141 * @{
2142 */
2143
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144struct k_mem_block {
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002145 struct k_mem_pool *pool_id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146 void *addr_in_pool;
2147 void *data;
Peter Mitsis5f399242016-10-13 13:26:25 -04002148 size_t req_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149};
2150
Allan Stephensc98da842016-11-11 15:45:03 -05002151/**
2152 * @} end defgroup mem_pool_apis
2153 */
2154
2155/**
2156 * @defgroup mailbox_apis Mailbox APIs
2157 * @ingroup kernel_apis
2158 * @{
2159 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160
2161struct k_mbox_msg {
2162 /** internal use only - needed for legacy API support */
2163 uint32_t _mailbox;
2164 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002165 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002166 /** application-defined information value */
2167 uint32_t info;
2168 /** sender's message data buffer */
2169 void *tx_data;
2170 /** internal use only - needed for legacy API support */
2171 void *_rx_data;
2172 /** message data block descriptor */
2173 struct k_mem_block tx_block;
2174 /** source thread id */
2175 k_tid_t rx_source_thread;
2176 /** target thread id */
2177 k_tid_t tx_target_thread;
2178 /** internal use only - thread waiting on send (may be a dummy) */
2179 k_tid_t _syncing_thread;
2180#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2181 /** internal use only - semaphore used during asynchronous send */
2182 struct k_sem *_async_sem;
2183#endif
2184};
2185
Allan Stephensc98da842016-11-11 15:45:03 -05002186/**
2187 * @cond INTERNAL_HIDDEN
2188 */
2189
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190struct k_mbox {
2191 _wait_q_t tx_msg_queue;
2192 _wait_q_t rx_msg_queue;
2193
Anas Nashif2f203c22016-12-18 06:57:45 -05002194 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195};
2196
2197#define K_MBOX_INITIALIZER(obj) \
2198 { \
2199 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2200 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002201 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202 }
2203
Peter Mitsis12092702016-10-14 12:57:23 -04002204/**
Allan Stephensc98da842016-11-11 15:45:03 -05002205 * INTERNAL_HIDDEN @endcond
2206 */
2207
2208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002209 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002211 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002212 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002213 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002215 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002218 struct k_mbox name \
2219 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 K_MBOX_INITIALIZER(name) \
2221
Peter Mitsis12092702016-10-14 12:57:23 -04002222/**
2223 * @brief Initialize a mailbox.
2224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002225 * This routine initializes a mailbox object, prior to its first use.
2226 *
2227 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002228 *
2229 * @return N/A
2230 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231extern void k_mbox_init(struct k_mbox *mbox);
2232
Peter Mitsis12092702016-10-14 12:57:23 -04002233/**
2234 * @brief Send a mailbox message in a synchronous manner.
2235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002236 * This routine sends a message to @a mbox and waits for a receiver to both
2237 * receive and process it. The message data may be in a buffer, in a memory
2238 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002240 * @param mbox Address of the mailbox.
2241 * @param tx_msg Address of the transmit message descriptor.
2242 * @param timeout Waiting period for the message to be received (in
2243 * milliseconds), or one of the special values K_NO_WAIT
2244 * and K_FOREVER. Once the message has been received,
2245 * this routine waits as long as necessary for the message
2246 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002247 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002248 * @retval 0 Message sent.
2249 * @retval -ENOMSG Returned without waiting.
2250 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002251 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002252extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253 int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002254
Peter Mitsis12092702016-10-14 12:57:23 -04002255/**
2256 * @brief Send a mailbox message in an asynchronous manner.
2257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002258 * This routine sends a message to @a mbox without waiting for a receiver
2259 * to process it. The message data may be in a buffer, in a memory pool block,
2260 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2261 * will be given when the message has been both received and completely
2262 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * @param mbox Address of the mailbox.
2265 * @param tx_msg Address of the transmit message descriptor.
2266 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002267 *
2268 * @return N/A
2269 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002270extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271 struct k_sem *sem);
2272
Peter Mitsis12092702016-10-14 12:57:23 -04002273/**
2274 * @brief Receive a mailbox message.
2275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002276 * This routine receives a message from @a mbox, then optionally retrieves
2277 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002279 * @param mbox Address of the mailbox.
2280 * @param rx_msg Address of the receive message descriptor.
2281 * @param buffer Address of the buffer to receive data, or NULL to defer data
2282 * retrieval and message disposal until later.
2283 * @param timeout Waiting period for a message to be received (in
2284 * milliseconds), or one of the special values K_NO_WAIT
2285 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002286 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002287 * @retval 0 Message received.
2288 * @retval -ENOMSG Returned without waiting.
2289 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002290 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002291extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292 void *buffer, int32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002293
2294/**
2295 * @brief Retrieve mailbox message data into a buffer.
2296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * This routine completes the processing of a received message by retrieving
2298 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002299 *
2300 * Alternatively, this routine can be used to dispose of a received message
2301 * without retrieving its data.
2302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002303 * @param rx_msg Address of the receive message descriptor.
2304 * @param buffer Address of the buffer to receive data, or NULL to discard
2305 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002306 *
2307 * @return N/A
2308 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002309extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002310
2311/**
2312 * @brief Retrieve mailbox message data into a memory pool block.
2313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002314 * This routine completes the processing of a received message by retrieving
2315 * its data into a memory pool block, then disposing of the message.
2316 * The memory pool block that results from successful retrieval must be
2317 * returned to the pool once the data has been processed, even in cases
2318 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002319 *
2320 * Alternatively, this routine can be used to dispose of a received message
2321 * without retrieving its data. In this case there is no need to return a
2322 * memory pool block to the pool.
2323 *
2324 * This routine allocates a new memory pool block for the data only if the
2325 * data is not already in one. If a new block cannot be allocated, the routine
2326 * returns a failure code and the received message is left unchanged. This
2327 * permits the caller to reattempt data retrieval at a later time or to dispose
2328 * of the received message without retrieving its data.
2329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * @param rx_msg Address of a receive message descriptor.
2331 * @param pool Address of memory pool, or NULL to discard data.
2332 * @param block Address of the area to hold memory pool block info.
2333 * @param timeout Waiting period to wait for a memory pool block (in
2334 * milliseconds), or one of the special values K_NO_WAIT
2335 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002336 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002337 * @retval 0 Data retrieved.
2338 * @retval -ENOMEM Returned without waiting.
2339 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002340 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002341extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002342 struct k_mem_pool *pool,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 struct k_mem_block *block, int32_t timeout);
2344
Allan Stephensc98da842016-11-11 15:45:03 -05002345/**
2346 * @} end defgroup mailbox_apis
2347 */
2348
2349/**
2350 * @cond INTERNAL_HIDDEN
2351 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352
2353struct k_pipe {
2354 unsigned char *buffer; /* Pipe buffer: may be NULL */
2355 size_t size; /* Buffer size */
2356 size_t bytes_used; /* # bytes used in buffer */
2357 size_t read_index; /* Where in buffer to read from */
2358 size_t write_index; /* Where in buffer to write */
2359
2360 struct {
2361 _wait_q_t readers; /* Reader wait queue */
2362 _wait_q_t writers; /* Writer wait queue */
2363 } wait_q;
2364
Anas Nashif2f203c22016-12-18 06:57:45 -05002365 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002366};
2367
Peter Mitsise5d9c582016-10-14 14:44:57 -04002368#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369 { \
2370 .buffer = pipe_buffer, \
2371 .size = pipe_buffer_size, \
2372 .bytes_used = 0, \
2373 .read_index = 0, \
2374 .write_index = 0, \
2375 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2376 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002377 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 }
2379
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002380/**
Allan Stephensc98da842016-11-11 15:45:03 -05002381 * INTERNAL_HIDDEN @endcond
2382 */
2383
2384/**
2385 * @defgroup pipe_apis Pipe APIs
2386 * @ingroup kernel_apis
2387 * @{
2388 */
2389
2390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002394 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002395 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @param name Name of the pipe.
2398 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2399 * or zero if no ring buffer is used.
2400 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002402#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2403 static unsigned char __noinit __aligned(pipe_align) \
2404 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002405 struct k_pipe name \
2406 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002407 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 * This routine initializes a pipe object, prior to its first use.
2413 *
2414 * @param pipe Address of the pipe.
2415 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2416 * is used.
2417 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2418 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002419 *
2420 * @return N/A
2421 */
2422extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2423 size_t size);
2424
2425/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002426 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * @param pipe Address of the pipe.
2431 * @param data Address of data to write.
2432 * @param bytes_to_write Size of data (in bytes).
2433 * @param bytes_written Address of area to hold the number of bytes written.
2434 * @param min_xfer Minimum number of bytes to write.
2435 * @param timeout Waiting period to wait for the data to be written (in
2436 * milliseconds), or one of the special values K_NO_WAIT
2437 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002439 * @retval 0 At least @a min_xfer bytes of data were written.
2440 * @retval -EIO Returned without waiting; zero data bytes were written.
2441 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002444extern int k_pipe_put(struct k_pipe *pipe, void *data,
2445 size_t bytes_to_write, size_t *bytes_written,
2446 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447
2448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002449 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453 * @param pipe Address of the pipe.
2454 * @param data Address to place the data read from pipe.
2455 * @param bytes_to_read Maximum number of data bytes to read.
2456 * @param bytes_read Address of area to hold the number of bytes read.
2457 * @param min_xfer Minimum number of data bytes to read.
2458 * @param timeout Waiting period to wait for the data to be read (in
2459 * milliseconds), or one of the special values K_NO_WAIT
2460 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002462 * @retval 0 At least @a min_xfer bytes of data were read.
2463 * @retval -EIO Returned without waiting; zero data bytes were read.
2464 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002467extern int k_pipe_get(struct k_pipe *pipe, void *data,
2468 size_t bytes_to_read, size_t *bytes_read,
2469 size_t min_xfer, int32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470
2471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002472 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * This routine writes the data contained in a memory block to @a pipe.
2475 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479 * @param block Memory block containing data to send
2480 * @param size Number of data bytes in memory block to send
2481 * @param sem Semaphore to signal upon completion (else NULL)
2482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002484 */
2485extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
2486 size_t size, struct k_sem *sem);
2487
2488/**
Allan Stephensc98da842016-11-11 15:45:03 -05002489 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002490 */
2491
Allan Stephensc98da842016-11-11 15:45:03 -05002492/**
2493 * @cond INTERNAL_HIDDEN
2494 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002496struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497 _wait_q_t wait_q;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002498 uint32_t num_blocks;
2499 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500 char *buffer;
2501 char *free_list;
Peter Mitsisfb02d572016-10-13 16:55:45 -04002502 uint32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503
Anas Nashif2f203c22016-12-18 06:57:45 -05002504 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505};
2506
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002507#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
2508 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509 { \
2510 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002511 .num_blocks = slab_num_blocks, \
2512 .block_size = slab_block_size, \
2513 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514 .free_list = NULL, \
2515 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002516 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517 }
2518
Peter Mitsis578f9112016-10-07 13:50:31 -04002519/**
Allan Stephensc98da842016-11-11 15:45:03 -05002520 * INTERNAL_HIDDEN @endcond
2521 */
2522
2523/**
2524 * @defgroup mem_slab_apis Memory Slab APIs
2525 * @ingroup kernel_apis
2526 * @{
2527 */
2528
2529/**
Allan Stephensda827222016-11-09 14:23:58 -06002530 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04002531 *
Allan Stephensda827222016-11-09 14:23:58 -06002532 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002534 * @a slab_align -byte boundary. To ensure that each memory block is similarly
2535 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002536 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04002537 *
Allan Stephensda827222016-11-09 14:23:58 -06002538 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002540 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002541 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * @param name Name of the memory slab.
2544 * @param slab_block_size Size of each memory block (in bytes).
2545 * @param slab_num_blocks Number memory blocks.
2546 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04002547 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002548#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
2549 char __noinit __aligned(slab_align) \
2550 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
2551 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002552 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002553 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
2554 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002556/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002557 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002559 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002560 *
Allan Stephensda827222016-11-09 14:23:58 -06002561 * The memory slab's buffer contains @a slab_num_blocks memory blocks
2562 * that are @a slab_block_size bytes long. The buffer must be aligned to an
2563 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
2564 * To ensure that each memory block is similarly aligned to this boundary,
2565 * @a slab_block_size must also be a multiple of N.
2566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002567 * @param slab Address of the memory slab.
2568 * @param buffer Pointer to buffer used for the memory blocks.
2569 * @param block_size Size of each memory block (in bytes).
2570 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002571 *
2572 * @return N/A
2573 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002574extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Peter Mitsisfb02d572016-10-13 16:55:45 -04002575 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002576
2577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * @param slab Address of the memory slab.
2583 * @param mem Pointer to block address area.
2584 * @param timeout Maximum time to wait for operation to complete
2585 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2586 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002587 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002588 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002590 * @retval -ENOMEM Returned without waiting.
2591 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002592 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002593extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
2594 int32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002595
2596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * This routine releases a previously allocated memory block back to its
2600 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @param slab Address of the memory slab.
2603 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002604 *
2605 * @return N/A
2606 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002607extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002608
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002609/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * This routine gets the number of memory blocks that are currently
2613 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04002618 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002619static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002621 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002622}
2623
Peter Mitsisc001aa82016-10-13 13:53:37 -04002624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002625 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627 * This routine gets the number of memory blocks that are currently
2628 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04002633 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002634static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04002635{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04002636 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04002637}
2638
Allan Stephensc98da842016-11-11 15:45:03 -05002639/**
2640 * @} end defgroup mem_slab_apis
2641 */
2642
2643/**
2644 * @cond INTERNAL_HIDDEN
2645 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002647/*
2648 * Memory pool requires a buffer and two arrays of structures for the
2649 * memory block accounting:
2650 * A set of arrays of k_mem_pool_quad_block structures where each keeps a
2651 * status of four blocks of memory.
2652 */
2653struct k_mem_pool_quad_block {
2654 char *mem_blocks; /* pointer to the first of four memory blocks */
2655 uint32_t mem_status; /* four bits. If bit is set, memory block is
2656 allocated */
2657};
2658/*
2659 * Memory pool mechanism uses one array of k_mem_pool_quad_block for accounting
2660 * blocks of one size. Block sizes go from maximal to minimal. Next memory
2661 * block size is 4 times less than the previous one and thus requires 4 times
2662 * bigger array of k_mem_pool_quad_block structures to keep track of the
2663 * memory blocks.
2664 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002665
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002666/*
2667 * The array of k_mem_pool_block_set keeps the information of each array of
2668 * k_mem_pool_quad_block structures
2669 */
2670struct k_mem_pool_block_set {
Peter Mitsis5f399242016-10-13 13:26:25 -04002671 size_t block_size; /* memory block size */
2672 uint32_t nr_of_entries; /* nr of quad block structures in the array */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002673 struct k_mem_pool_quad_block *quad_block;
2674 int count;
2675};
2676
2677/* Memory pool descriptor */
2678struct k_mem_pool {
Peter Mitsis5f399242016-10-13 13:26:25 -04002679 size_t max_block_size;
2680 size_t min_block_size;
2681 uint32_t nr_of_maxblocks;
2682 uint32_t nr_of_block_sets;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002683 struct k_mem_pool_block_set *block_set;
2684 char *bufblock;
2685 _wait_q_t wait_q;
Anas Nashif2f203c22016-12-18 06:57:45 -05002686 _OBJECT_TRACING_NEXT_PTR(k_mem_pool);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687};
2688
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002689#ifdef CONFIG_ARM
2690#define _SECTION_TYPE_SIGN "%"
2691#else
2692#define _SECTION_TYPE_SIGN "@"
2693#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002695/*
2696 * Static memory pool initialization
2697 */
Allan Stephensc98da842016-11-11 15:45:03 -05002698
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002699/*
2700 * Use .altmacro to be able to recalculate values and pass them as string
2701 * arguments when calling assembler macros resursively
2702 */
2703__asm__(".altmacro\n\t");
2704
2705/*
2706 * Recursively calls a macro
2707 * The followig global symbols need to be initialized:
2708 * __memory_pool_max_block_size - maximal size of the memory block
2709 * __memory_pool_min_block_size - minimal size of the memory block
2710 * Notes:
2711 * Global symbols are used due the fact that assembler macro allows only
2712 * one argument be passed with the % conversion
2713 * Some assemblers do not get division operation ("/"). To avoid it >> 2
2714 * is used instead of / 4.
2715 * n_max argument needs to go first in the invoked macro, as some
2716 * assemblers concatenate \name and %(\n_max * 4) arguments
2717 * if \name goes first
2718 */
2719__asm__(".macro __do_recurse macro_name, name, n_max\n\t"
2720 ".ifge __memory_pool_max_block_size >> 2 -"
2721 " __memory_pool_min_block_size\n\t\t"
2722 "__memory_pool_max_block_size = __memory_pool_max_block_size >> 2\n\t\t"
2723 "\\macro_name %(\\n_max * 4) \\name\n\t"
2724 ".endif\n\t"
2725 ".endm\n");
2726
2727/*
2728 * Build quad blocks
2729 * Macro allocates space in memory for the array of k_mem_pool_quad_block
2730 * structures and recursively calls itself for the next array, 4 times
2731 * larger.
2732 * The followig global symbols need to be initialized:
2733 * __memory_pool_max_block_size - maximal size of the memory block
2734 * __memory_pool_min_block_size - minimal size of the memory block
2735 * __memory_pool_quad_block_size - sizeof(struct k_mem_pool_quad_block)
2736 */
2737__asm__(".macro _build_quad_blocks n_max, name\n\t"
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002738 ".balign 4\n\t"
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002739 "_mem_pool_quad_blocks_\\name\\()_\\n_max:\n\t"
2740 ".skip __memory_pool_quad_block_size * \\n_max >> 2\n\t"
2741 ".if \\n_max % 4\n\t\t"
2742 ".skip __memory_pool_quad_block_size\n\t"
2743 ".endif\n\t"
2744 "__do_recurse _build_quad_blocks \\name \\n_max\n\t"
2745 ".endm\n");
2746
2747/*
2748 * Build block sets and initialize them
2749 * Macro initializes the k_mem_pool_block_set structure and
2750 * recursively calls itself for the next one.
2751 * The followig global symbols need to be initialized:
2752 * __memory_pool_max_block_size - maximal size of the memory block
2753 * __memory_pool_min_block_size - minimal size of the memory block
2754 * __memory_pool_block_set_count, the number of the elements in the
2755 * block set array must be set to 0. Macro calculates it's real
2756 * value.
2757 * Since the macro initializes pointers to an array of k_mem_pool_quad_block
2758 * structures, _build_quad_blocks must be called prior it.
2759 */
2760__asm__(".macro _build_block_set n_max, name\n\t"
2761 ".int __memory_pool_max_block_size\n\t" /* block_size */
2762 ".if \\n_max % 4\n\t\t"
2763 ".int \\n_max >> 2 + 1\n\t" /* nr_of_entries */
2764 ".else\n\t\t"
2765 ".int \\n_max >> 2\n\t"
2766 ".endif\n\t"
2767 ".int _mem_pool_quad_blocks_\\name\\()_\\n_max\n\t" /* quad_block */
2768 ".int 0\n\t" /* count */
2769 "__memory_pool_block_set_count = __memory_pool_block_set_count + 1\n\t"
2770 "__do_recurse _build_block_set \\name \\n_max\n\t"
2771 ".endm\n");
2772
2773/*
2774 * Build a memory pool structure and initialize it
2775 * Macro uses __memory_pool_block_set_count global symbol,
2776 * block set addresses and buffer address, it may be called only after
2777 * _build_block_set
2778 */
2779__asm__(".macro _build_mem_pool name, min_size, max_size, n_max\n\t"
Allan Stephense7d2cc22016-10-19 16:10:46 -05002780 ".pushsection ._k_mem_pool.static.\\name,\"aw\","
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002781 _SECTION_TYPE_SIGN "progbits\n\t"
2782 ".globl \\name\n\t"
2783 "\\name:\n\t"
2784 ".int \\max_size\n\t" /* max_block_size */
2785 ".int \\min_size\n\t" /* min_block_size */
2786 ".int \\n_max\n\t" /* nr_of_maxblocks */
2787 ".int __memory_pool_block_set_count\n\t" /* nr_of_block_sets */
2788 ".int _mem_pool_block_sets_\\name\n\t" /* block_set */
2789 ".int _mem_pool_buffer_\\name\n\t" /* bufblock */
2790 ".int 0\n\t" /* wait_q->head */
2791 ".int 0\n\t" /* wait_q->next */
2792 ".popsection\n\t"
2793 ".endm\n");
2794
2795#define _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max) \
2796 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2797 _SECTION_TYPE_SIGN "progbits\n\t"); \
2798 __asm__("__memory_pool_min_block_size = " STRINGIFY(min_size) "\n\t"); \
2799 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2800 __asm__("_build_quad_blocks " STRINGIFY(n_max) " " \
2801 STRINGIFY(name) "\n\t"); \
2802 __asm__(".popsection\n\t")
2803
2804#define _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max) \
2805 __asm__("__memory_pool_block_set_count = 0\n\t"); \
2806 __asm__("__memory_pool_max_block_size = " STRINGIFY(max_size) "\n\t"); \
2807 __asm__(".pushsection ._k_memory_pool.struct,\"aw\"," \
2808 _SECTION_TYPE_SIGN "progbits\n\t"); \
Dmitriy Korovkin3c906512016-10-06 15:50:40 -04002809 __asm__(".balign 4\n\t"); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002810 __asm__("_mem_pool_block_sets_" STRINGIFY(name) ":\n\t"); \
2811 __asm__("_build_block_set " STRINGIFY(n_max) " " \
2812 STRINGIFY(name) "\n\t"); \
2813 __asm__("_mem_pool_block_set_count_" STRINGIFY(name) ":\n\t"); \
2814 __asm__(".int __memory_pool_block_set_count\n\t"); \
2815 __asm__(".popsection\n\t"); \
2816 extern uint32_t _mem_pool_block_set_count_##name; \
2817 extern struct k_mem_pool_block_set _mem_pool_block_sets_##name[]
2818
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002819#define _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align) \
2820 char __noinit __aligned(align) \
2821 _mem_pool_buffer_##name[(max_size) * (n_max)]
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002822
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002823/*
2824 * Dummy function that assigns the value of sizeof(struct k_mem_pool_quad_block)
2825 * to __memory_pool_quad_block_size absolute symbol.
2826 * This function does not get called, but compiler calculates the value and
2827 * assigns it to the absolute symbol, that, in turn is used by assembler macros.
2828 */
2829static void __attribute__ ((used)) __k_mem_pool_quad_block_size_define(void)
2830{
2831 __asm__(".globl __memory_pool_quad_block_size\n\t"
2832#ifdef CONFIG_NIOS2
2833 "__memory_pool_quad_block_size = %0\n\t"
2834#else
2835 "__memory_pool_quad_block_size = %c0\n\t"
2836#endif
2837 :
2838 : "n"(sizeof(struct k_mem_pool_quad_block)));
2839}
2840
2841/**
Allan Stephensc98da842016-11-11 15:45:03 -05002842 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04002843 */
2844
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002845/**
Allan Stephensc98da842016-11-11 15:45:03 -05002846 * @addtogroup mem_pool_apis
2847 * @{
2848 */
2849
2850/**
2851 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
2854 * long. The memory pool allows blocks to be repeatedly partitioned into
2855 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
2856 * to a @a align -byte boundary. To ensure that the minimum sized blocks are
Allan Stephensda827222016-11-09 14:23:58 -06002857 * similarly aligned to this boundary, @a min_size must also be a multiple of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * @a align.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002859 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002860 * If the pool is to be accessed outside the module where it is defined, it
2861 * can be declared via
2862 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002863 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @param name Name of the memory pool.
2866 * @param min_size Size of the smallest blocks in the pool (in bytes).
2867 * @param max_size Size of the largest blocks in the pool (in bytes).
2868 * @param n_max Number of maximum sized blocks in the pool.
2869 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002870 */
2871#define K_MEM_POOL_DEFINE(name, min_size, max_size, n_max, align) \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002872 _MEMORY_POOL_QUAD_BLOCK_DEFINE(name, min_size, max_size, n_max); \
2873 _MEMORY_POOL_BLOCK_SETS_DEFINE(name, min_size, max_size, n_max); \
Peter Mitsis2a2b0752016-10-06 16:27:01 -04002874 _MEMORY_POOL_BUFFER_DEFINE(name, max_size, n_max, align); \
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002875 __asm__("_build_mem_pool " STRINGIFY(name) " " STRINGIFY(min_size) " " \
2876 STRINGIFY(max_size) " " STRINGIFY(n_max) "\n\t"); \
2877 extern struct k_mem_pool name
2878
Peter Mitsis937042c2016-10-13 13:18:26 -04002879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @param pool Address of the memory pool.
2885 * @param block Pointer to block descriptor for the allocated memory.
2886 * @param size Amount of memory to allocate (in bytes).
2887 * @param timeout Maximum time to wait for operation to complete
2888 * (in milliseconds). Use K_NO_WAIT to return without waiting,
2889 * or K_FOREVER to wait as long as necessary.
2890 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002891 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05002893 * @retval -ENOMEM Returned without waiting.
2894 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04002895 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002896extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Peter Mitsis5f399242016-10-13 13:26:25 -04002897 size_t size, int32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04002898
2899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * This routine releases a previously allocated memory block back to its
2903 * memory pool.
2904 *
2905 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002906 *
2907 * @return N/A
2908 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002909extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04002910
2911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * This routine instructs a memory pool to concatenate unused memory blocks
2915 * into larger blocks wherever possible. Manually defragmenting the memory
2916 * pool may speed up future allocations of memory blocks by eliminating the
2917 * need for the memory pool to perform an automatic partial defragmentation.
2918 *
2919 * @param pool Address of the memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002920 *
2921 * @return N/A
2922 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04002923extern void k_mem_pool_defrag(struct k_mem_pool *pool);
Peter Mitsis937042c2016-10-13 13:18:26 -04002924
2925/**
Allan Stephensc98da842016-11-11 15:45:03 -05002926 * @} end addtogroup mem_pool_apis
2927 */
2928
2929/**
2930 * @defgroup heap_apis Heap Memory Pool APIs
2931 * @ingroup kernel_apis
2932 * @{
2933 */
2934
2935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04002937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05002939 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04002944 */
Peter Mitsis5f399242016-10-13 13:26:25 -04002945extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04002946
2947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05002949 *
2950 * This routine provides traditional free() semantics. The memory being
2951 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04002952 *
Anas Nashif345fdd52016-12-20 08:36:04 -05002953 * If @a ptr is NULL, no operation is performed.
2954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04002956 *
2957 * @return N/A
2958 */
2959extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960
Allan Stephensc98da842016-11-11 15:45:03 -05002961/**
2962 * @} end defgroup heap_apis
2963 */
2964
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05002965/**
2966 * @brief Make the CPU idle.
2967 *
2968 * This function makes the CPU idle until an event wakes it up.
2969 *
2970 * In a regular system, the idle thread should be the only thread responsible
2971 * for making the CPU idle and triggering any type of power management.
2972 * However, in some more constrained systems, such as a single-threaded system,
2973 * the only thread would be responsible for this if needed.
2974 *
2975 * @return N/A
2976 */
2977extern void k_cpu_idle(void);
2978
2979/**
2980 * @brief Make the CPU idle in an atomic fashion.
2981 *
2982 * Similar to k_cpu_idle(), but called with interrupts locked if operations
2983 * must be done atomically before making the CPU idle.
2984 *
2985 * @param key Interrupt locking key obtained from irq_lock().
2986 *
2987 * @return N/A
2988 */
2989extern void k_cpu_atomic_idle(unsigned int key);
2990
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991/*
2992 * legacy.h must be before arch/cpu.h to allow the ioapic/loapic drivers to
2993 * hook into the device subsystem, which itself uses nanokernel semaphores,
2994 * and thus currently requires the definition of nano_sem.
2995 */
2996#include <legacy.h>
2997#include <arch/cpu.h>
2998
2999/*
3000 * private APIs that are utilized by one or more public APIs
3001 */
3002
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003003#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003005#else
3006#define _init_static_threads() do { } while ((0))
3007#endif
3008
3009extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003010extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011
3012#ifdef __cplusplus
3013}
3014#endif
3015
Andrew Boiee004dec2016-11-07 09:01:19 -08003016#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3017/*
3018 * Define new and delete operators.
3019 * At this moment, the operators do nothing since objects are supposed
3020 * to be statically allocated.
3021 */
3022inline void operator delete(void *ptr)
3023{
3024 (void)ptr;
3025}
3026
3027inline void operator delete[](void *ptr)
3028{
3029 (void)ptr;
3030}
3031
3032inline void *operator new(size_t size)
3033{
3034 (void)size;
3035 return NULL;
3036}
3037
3038inline void *operator new[](size_t size)
3039{
3040 (void)size;
3041 return NULL;
3042}
3043
3044/* Placement versions of operator new and delete */
3045inline void operator delete(void *ptr1, void *ptr2)
3046{
3047 (void)ptr1;
3048 (void)ptr2;
3049}
3050
3051inline void operator delete[](void *ptr1, void *ptr2)
3052{
3053 (void)ptr1;
3054 (void)ptr2;
3055}
3056
3057inline void *operator new(size_t size, void *ptr)
3058{
3059 (void)size;
3060 return ptr;
3061}
3062
3063inline void *operator new[](size_t size, void *ptr)
3064{
3065 (void)size;
3066 return ptr;
3067}
3068
3069#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3070
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071#endif /* _kernel__h_ */