blob: 6aa05a0bb53dbf197758dcffee254e4239432d63 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
101#define _POLL_EVENT_OBJ_INIT \
102 .poll_event = NULL,
103#define _POLL_EVENT struct k_poll_event *poll_event
104#else
105#define _POLL_EVENT_OBJ_INIT
106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Andrew Boie73abd322017-04-04 13:19:13 -0700126/* timeouts */
127
128struct _timeout;
129typedef void (*_timeout_func_t)(struct _timeout *t);
130
131struct _timeout {
132 sys_dnode_t node;
133 struct k_thread *thread;
134 sys_dlist_t *wait_q;
135 s32_t delta_ticks_from_prev;
136 _timeout_func_t func;
137};
138
139extern s32_t _timeout_remaining_get(struct _timeout *timeout);
140
141/* Threads */
142typedef void (*_thread_entry_t)(void *, void *, void *);
143
144#ifdef CONFIG_THREAD_MONITOR
145struct __thread_entry {
146 _thread_entry_t pEntry;
147 void *parameter1;
148 void *parameter2;
149 void *parameter3;
150};
151#endif
152
153/* can be used for creating 'dummy' threads, e.g. for pending on objects */
154struct _thread_base {
155
156 /* this thread's entry in a ready/wait queue */
157 sys_dnode_t k_q_node;
158
159 /* user facing 'thread options'; values defined in include/kernel.h */
160 u8_t user_options;
161
162 /* thread state */
163 u8_t thread_state;
164
165 /*
166 * scheduler lock count and thread priority
167 *
168 * These two fields control the preemptibility of a thread.
169 *
170 * When the scheduler is locked, sched_locked is decremented, which
171 * means that the scheduler is locked for values from 0xff to 0x01. A
172 * thread is coop if its prio is negative, thus 0x80 to 0xff when
173 * looked at the value as unsigned.
174 *
175 * By putting them end-to-end, this means that a thread is
176 * non-preemptible if the bundled value is greater than or equal to
177 * 0x0080.
178 */
179 union {
180 struct {
181#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
182 u8_t sched_locked;
183 s8_t prio;
184#else /* LITTLE and PDP */
185 s8_t prio;
186 u8_t sched_locked;
187#endif
188 };
189 u16_t preempt;
190 };
191
192 /* data returned by APIs */
193 void *swap_data;
194
195#ifdef CONFIG_SYS_CLOCK_EXISTS
196 /* this thread's entry in a timeout queue */
197 struct _timeout timeout;
198#endif
199
200};
201
202typedef struct _thread_base _thread_base_t;
203
204#if defined(CONFIG_THREAD_STACK_INFO)
205/* Contains the stack information of a thread */
206struct _thread_stack_info {
207 /* Stack Start */
208 u32_t start;
209 /* Stack Size */
210 u32_t size;
211};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700212
213typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700214#endif /* CONFIG_THREAD_STACK_INFO */
215
216struct k_thread {
217
218 struct _thread_base base;
219
220 /* defined by the architecture, but all archs need these */
221 struct _caller_saved caller_saved;
222 struct _callee_saved callee_saved;
223
224 /* static thread init data */
225 void *init_data;
226
227 /* abort function */
228 void (*fn_abort)(void);
229
230#if defined(CONFIG_THREAD_MONITOR)
231 /* thread entry and parameters description */
232 struct __thread_entry *entry;
233
234 /* next item in list of all threads */
235 struct k_thread *next_thread;
236#endif
237
238#ifdef CONFIG_THREAD_CUSTOM_DATA
239 /* crude thread-local storage */
240 void *custom_data;
241#endif
242
243#ifdef CONFIG_ERRNO
244 /* per-thread errno variable */
245 int errno_var;
246#endif
247
248#if defined(CONFIG_THREAD_STACK_INFO)
249 /* Stack Info */
250 struct _thread_stack_info stack_info;
251#endif /* CONFIG_THREAD_STACK_INFO */
252
253 /* arch-specifics: must always be at the end */
254 struct _thread_arch arch;
255};
256
257typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400258typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700259#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261enum execution_context_types {
262 K_ISR = 0,
263 K_COOP_THREAD,
264 K_PREEMPT_THREAD,
265};
266
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100268 * @defgroup profiling_apis Profiling APIs
269 * @ingroup kernel_apis
270 * @{
271 */
272
273/**
274 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
275 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700276 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100277 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
278 *
279 * CONFIG_MAIN_STACK_SIZE
280 * CONFIG_IDLE_STACK_SIZE
281 * CONFIG_ISR_STACK_SIZE
282 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
283 *
284 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
285 * produce output.
286 *
287 * @return N/A
288 */
289extern void k_call_stacks_analyze(void);
290
291/**
292 * @} end defgroup profiling_apis
293 */
294
295/**
Allan Stephensc98da842016-11-11 15:45:03 -0500296 * @defgroup thread_apis Thread APIs
297 * @ingroup kernel_apis
298 * @{
299 */
300
301/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500302 * @typedef k_thread_entry_t
303 * @brief Thread entry point function type.
304 *
305 * A thread's entry point function is invoked when the thread starts executing.
306 * Up to 3 argument values can be passed to the function.
307 *
308 * The thread terminates execution permanently if the entry point function
309 * returns. The thread is responsible for releasing any shared resources
310 * it may own (such as mutexes and dynamically allocated memory), prior to
311 * returning.
312 *
313 * @param p1 First argument.
314 * @param p2 Second argument.
315 * @param p3 Third argument.
316 *
317 * @return N/A
318 */
319typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
320
Benjamin Walshed240f22017-01-22 13:05:08 -0500321#endif /* !_ASMLANGUAGE */
322
323
324/*
325 * Thread user options. May be needed by assembly code. Common part uses low
326 * bits, arch-specific use high bits.
327 */
328
329/* system thread that must not abort */
330#define K_ESSENTIAL (1 << 0)
331
332#if defined(CONFIG_FP_SHARING)
333/* thread uses floating point registers */
334#define K_FP_REGS (1 << 1)
335#endif
336
337#ifdef CONFIG_X86
338/* x86 Bitmask definitions for threads user options */
339
340#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
341/* thread uses SSEx (and also FP) registers */
342#define K_SSE_REGS (1 << 7)
343#endif
344#endif
345
346/* end - thread options */
347
348#if !defined(_ASMLANGUAGE)
349
Allan Stephens5eceb852016-11-16 10:16:30 -0500350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500353 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500355 * The new thread may be scheduled for immediate execution or a delayed start.
356 * If the newly spawned thread does not have a delayed start the kernel
357 * scheduler may preempt the current thread to allow the new thread to
358 * execute.
359 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700360 * Kernel data structures for bookkeeping and context storage for this thread
361 * will be placed at the beginning of the thread's stack memory region and may
362 * become corrupted if too much of the stack is used. This function has been
363 * deprecated in favor of k_thread_create() to give the user more control on
364 * where these data structures reside.
365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500366 * Thread options are architecture-specific, and can include K_ESSENTIAL,
367 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
368 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400369 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700370 * The stack itself should be declared with K_THREAD_STACK_DEFINE or variant
371 * macros. The stack size parameter should either be a defined constant
372 * also passed to K_THREAD_STACK_DEFINE, or the value of K_THREAD_STACK_SIZEOF.
373 * Do not use regular C sizeof().
374 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400375 * @param stack Pointer to the stack space.
376 * @param stack_size Stack size in bytes.
377 * @param entry Thread entry function.
378 * @param p1 1st entry point parameter.
379 * @param p2 2nd entry point parameter.
380 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @param prio Thread priority.
382 * @param options Thread options.
383 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500385 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386 */
Andrew Boied26cf2d2017-03-30 13:07:02 -0700387extern __deprecated k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500388 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400389 void *p1, void *p2, void *p3,
Kumar Galacc334c72017-04-21 10:55:34 -0500390 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400391
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400392/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700393 * @brief Create a thread.
394 *
395 * This routine initializes a thread, then schedules it for execution.
396 *
397 * The new thread may be scheduled for immediate execution or a delayed start.
398 * If the newly spawned thread does not have a delayed start the kernel
399 * scheduler may preempt the current thread to allow the new thread to
400 * execute.
401 *
402 * Thread options are architecture-specific, and can include K_ESSENTIAL,
403 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
404 * them using "|" (the logical OR operator).
405 *
406 * Historically, users often would use the beginning of the stack memory region
407 * to store the struct k_thread data, although corruption will occur if the
408 * stack overflows this region and stack protection features may not detect this
409 * situation.
410 *
411 * @param new_thread Pointer to uninitialized struct k_thread
412 * @param stack Pointer to the stack space.
413 * @param stack_size Stack size in bytes.
414 * @param entry Thread entry function.
415 * @param p1 1st entry point parameter.
416 * @param p2 2nd entry point parameter.
417 * @param p3 3rd entry point parameter.
418 * @param prio Thread priority.
419 * @param options Thread options.
420 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
421 *
422 * @return ID of new thread.
423 */
424extern k_tid_t k_thread_create(struct k_thread *new_thread, char *stack,
425 size_t stack_size,
426 void (*entry)(void *, void *, void*),
427 void *p1, void *p2, void *p3,
428 int prio, u32_t options, s32_t delay);
429
430/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432 *
Allan Stephensc98da842016-11-11 15:45:03 -0500433 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
438 * @return N/A
439 */
Kumar Galacc334c72017-04-21 10:55:34 -0500440extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441
442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500443 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444 *
445 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500446 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 * @return N/A
449 */
Kumar Galacc334c72017-04-21 10:55:34 -0500450extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451
452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500453 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500455 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
459 * @return N/A
460 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400461extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462
463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500466 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500468 * If @a thread is not currently sleeping, the routine has no effect.
469 *
470 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
472 * @return N/A
473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475
476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500477 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400481extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400482
483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500486 * This routine prevents @a thread from executing if it has not yet started
487 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * @param thread ID of thread to cancel.
490 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700491 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500492 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400493 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400494extern int k_thread_cancel(k_tid_t thread);
495
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400496/**
Allan Stephensc98da842016-11-11 15:45:03 -0500497 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500499 * This routine permanently stops execution of @a thread. The thread is taken
500 * off all kernel queues it is part of (i.e. the ready queue, the timeout
501 * queue, or a kernel object wait queue). However, any kernel resources the
502 * thread might currently own (such as mutexes or memory blocks) are not
503 * released. It is the responsibility of the caller of this routine to ensure
504 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500506 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400507 *
508 * @return N/A
509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400510extern void k_thread_abort(k_tid_t thread);
511
Allan Stephensc98da842016-11-11 15:45:03 -0500512/**
513 * @cond INTERNAL_HIDDEN
514 */
515
Benjamin Walshd211a522016-12-06 11:44:01 -0500516/* timeout has timed out and is not on _timeout_q anymore */
517#define _EXPIRED (-2)
518
519/* timeout is not in use */
520#define _INACTIVE (-1)
521
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400522struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700523 struct k_thread *init_thread;
524 char *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400525 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500526 void (*init_entry)(void *, void *, void *);
527 void *init_p1;
528 void *init_p2;
529 void *init_p3;
530 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500531 u32_t init_options;
532 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500533 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500534 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400535};
536
Andrew Boied26cf2d2017-03-30 13:07:02 -0700537#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400538 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500539 prio, options, delay, abort, groups) \
540 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700541 .init_thread = (thread), \
542 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500543 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400544 .init_entry = (void (*)(void *, void *, void *))entry, \
545 .init_p1 = (void *)p1, \
546 .init_p2 = (void *)p2, \
547 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500548 .init_prio = (prio), \
549 .init_options = (options), \
550 .init_delay = (delay), \
551 .init_abort = (abort), \
552 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400553 }
554
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400555/**
Allan Stephensc98da842016-11-11 15:45:03 -0500556 * INTERNAL_HIDDEN @endcond
557 */
558
559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * @brief Statically define and initialize a thread.
561 *
562 * The thread may be scheduled for immediate execution or a delayed start.
563 *
564 * Thread options are architecture-specific, and can include K_ESSENTIAL,
565 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
566 * them using "|" (the logical OR operator).
567 *
568 * The ID of the thread can be accessed using:
569 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500570 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500571 *
572 * @param name Name of the thread.
573 * @param stack_size Stack size in bytes.
574 * @param entry Thread entry function.
575 * @param p1 1st entry point parameter.
576 * @param p2 2nd entry point parameter.
577 * @param p3 3rd entry point parameter.
578 * @param prio Thread priority.
579 * @param options Thread options.
580 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400581 *
582 * @internal It has been observed that the x86 compiler by default aligns
583 * these _static_thread_data structures to 32-byte boundaries, thereby
584 * wasting space. To work around this, force a 4-byte alignment.
585 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500586#define K_THREAD_DEFINE(name, stack_size, \
587 entry, p1, p2, p3, \
588 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700589 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700590 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500591 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500592 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700593 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
594 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500595 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700596 NULL, 0); \
597 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @param thread ID of thread whose priority is needed.
605 *
606 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500608extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609
610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500613 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 *
615 * Rescheduling can occur immediately depending on the priority @a thread is
616 * set to:
617 *
618 * - If its priority is raised above the priority of the caller of this
619 * function, and the caller is preemptible, @a thread will be scheduled in.
620 *
621 * - If the caller operates on itself, it lowers its priority below that of
622 * other threads in the system, and the caller is preemptible, the thread of
623 * highest priority will be scheduled in.
624 *
625 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
626 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
627 * highest priority.
628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500629 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630 * @param prio New priority.
631 *
632 * @warning Changing the priority of a thread currently involved in mutex
633 * priority inheritance may result in undefined behavior.
634 *
635 * @return N/A
636 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400637extern void k_thread_priority_set(k_tid_t thread, int prio);
638
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500640 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * This routine prevents the kernel scheduler from making @a thread the
643 * current thread. All other internal operations on @a thread are still
644 * performed; for example, any timeout it is waiting on keeps ticking,
645 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500647 * If @a thread is already suspended, the routine has no effect.
648 *
649 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 *
651 * @return N/A
652 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400653extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400654
655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500658 * This routine allows the kernel scheduler to make @a thread the current
659 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * If @a thread is not currently suspended, the routine has no effect.
662 *
663 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
665 * @return N/A
666 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400667extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * This routine specifies how the scheduler will perform time slicing of
673 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500675 * To enable time slicing, @a slice must be non-zero. The scheduler
676 * ensures that no thread runs for more than the specified time limit
677 * before other threads of that priority are given a chance to execute.
678 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700679 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400682 * execute. Once the scheduler selects a thread for execution, there is no
683 * minimum guaranteed time the thread will execute before threads of greater or
684 * equal priority are scheduled.
685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500686 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687 * for execution, this routine has no effect; the thread is immediately
688 * rescheduled after the slice period expires.
689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * To disable timeslicing, set both @a slice and @a prio to zero.
691 *
692 * @param slice Maximum time slice length (in milliseconds).
693 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694 *
695 * @return N/A
696 */
Kumar Galacc334c72017-04-21 10:55:34 -0500697extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400698
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699/**
Allan Stephensc98da842016-11-11 15:45:03 -0500700 * @} end defgroup thread_apis
701 */
702
703/**
704 * @addtogroup isr_apis
705 * @{
706 */
707
708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
Allan Stephensc98da842016-11-11 15:45:03 -0500711 * This routine allows the caller to customize its actions, depending on
712 * whether it is a thread or an ISR.
713 *
714 * @note Can be called by ISRs.
715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500716 * @return 0 if invoked by a thread.
717 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500719extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400720
Benjamin Walsh445830d2016-11-10 15:54:27 -0500721/**
722 * @brief Determine if code is running in a preemptible thread.
723 *
Allan Stephensc98da842016-11-11 15:45:03 -0500724 * This routine allows the caller to customize its actions, depending on
725 * whether it can be preempted by another thread. The routine returns a 'true'
726 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500727 *
Allan Stephensc98da842016-11-11 15:45:03 -0500728 * - The code is running in a thread, not at ISR.
729 * - The thread's priority is in the preemptible range.
730 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500731 *
Allan Stephensc98da842016-11-11 15:45:03 -0500732 * @note Can be called by ISRs.
733 *
734 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500735 * @return Non-zero if invoked by a preemptible thread.
736 */
737extern int k_is_preempt_thread(void);
738
Allan Stephensc98da842016-11-11 15:45:03 -0500739/**
740 * @} end addtogroup isr_apis
741 */
742
743/**
744 * @addtogroup thread_apis
745 * @{
746 */
747
748/**
749 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500750 *
Allan Stephensc98da842016-11-11 15:45:03 -0500751 * This routine prevents the current thread from being preempted by another
752 * thread by instructing the scheduler to treat it as a cooperative thread.
753 * If the thread subsequently performs an operation that makes it unready,
754 * it will be context switched out in the normal manner. When the thread
755 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500756 *
Allan Stephensc98da842016-11-11 15:45:03 -0500757 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500758 *
Allan Stephensc98da842016-11-11 15:45:03 -0500759 * @note k_sched_lock() and k_sched_unlock() should normally be used
760 * when the operation being performed can be safely interrupted by ISRs.
761 * However, if the amount of processing involved is very small, better
762 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500763 *
764 * @return N/A
765 */
766extern void k_sched_lock(void);
767
Allan Stephensc98da842016-11-11 15:45:03 -0500768/**
769 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500770 *
Allan Stephensc98da842016-11-11 15:45:03 -0500771 * This routine reverses the effect of a previous call to k_sched_lock().
772 * A thread must call the routine once for each time it called k_sched_lock()
773 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500774 *
775 * @return N/A
776 */
777extern void k_sched_unlock(void);
778
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * Custom data is not used by the kernel itself, and is freely available
785 * for a thread to use as it sees fit. It can be used as a framework
786 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
790 * @return N/A
791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400801extern void *k_thread_custom_data_get(void);
802
803/**
Allan Stephensc98da842016-11-11 15:45:03 -0500804 * @} end addtogroup thread_apis
805 */
806
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400807#include <sys_clock.h>
808
Allan Stephensc2f15a42016-11-17 12:24:22 -0500809/**
810 * @addtogroup clock_apis
811 * @{
812 */
813
814/**
815 * @brief Generate null timeout delay.
816 *
817 * This macro generates a timeout delay that that instructs a kernel API
818 * not to wait if the requested operation cannot be performed immediately.
819 *
820 * @return Timeout delay value.
821 */
822#define K_NO_WAIT 0
823
824/**
825 * @brief Generate timeout delay from milliseconds.
826 *
827 * This macro generates a timeout delay that that instructs a kernel API
828 * to wait up to @a ms milliseconds to perform the requested operation.
829 *
830 * @param ms Duration in milliseconds.
831 *
832 * @return Timeout delay value.
833 */
Johan Hedberg14471692016-11-13 10:52:15 +0200834#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500835
836/**
837 * @brief Generate timeout delay from seconds.
838 *
839 * This macro generates a timeout delay that that instructs a kernel API
840 * to wait up to @a s seconds to perform the requested operation.
841 *
842 * @param s Duration in seconds.
843 *
844 * @return Timeout delay value.
845 */
Johan Hedberg14471692016-11-13 10:52:15 +0200846#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500847
848/**
849 * @brief Generate timeout delay from minutes.
850 *
851 * This macro generates a timeout delay that that instructs a kernel API
852 * to wait up to @a m minutes to perform the requested operation.
853 *
854 * @param m Duration in minutes.
855 *
856 * @return Timeout delay value.
857 */
Johan Hedberg14471692016-11-13 10:52:15 +0200858#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500859
860/**
861 * @brief Generate timeout delay from hours.
862 *
863 * This macro generates a timeout delay that that instructs a kernel API
864 * to wait up to @a h hours to perform the requested operation.
865 *
866 * @param h Duration in hours.
867 *
868 * @return Timeout delay value.
869 */
Johan Hedberg14471692016-11-13 10:52:15 +0200870#define K_HOURS(h) K_MINUTES((h) * 60)
871
Allan Stephensc98da842016-11-11 15:45:03 -0500872/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500873 * @brief Generate infinite timeout delay.
874 *
875 * This macro generates a timeout delay that that instructs a kernel API
876 * to wait as long as necessary to perform the requested operation.
877 *
878 * @return Timeout delay value.
879 */
880#define K_FOREVER (-1)
881
882/**
883 * @} end addtogroup clock_apis
884 */
885
886/**
Allan Stephensc98da842016-11-11 15:45:03 -0500887 * @cond INTERNAL_HIDDEN
888 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400889
Benjamin Walsh62092182016-12-20 14:39:08 -0500890/* kernel clocks */
891
892#if (sys_clock_ticks_per_sec == 1000) || \
893 (sys_clock_ticks_per_sec == 500) || \
894 (sys_clock_ticks_per_sec == 250) || \
895 (sys_clock_ticks_per_sec == 125) || \
896 (sys_clock_ticks_per_sec == 100) || \
897 (sys_clock_ticks_per_sec == 50) || \
898 (sys_clock_ticks_per_sec == 25) || \
899 (sys_clock_ticks_per_sec == 20) || \
900 (sys_clock_ticks_per_sec == 10) || \
901 (sys_clock_ticks_per_sec == 1)
902
903 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
904#else
905 /* yields horrible 64-bit math on many architectures: try to avoid */
906 #define _NON_OPTIMIZED_TICKS_PER_SEC
907#endif
908
909#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500910extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500911#else
Kumar Galacc334c72017-04-21 10:55:34 -0500912static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500913{
Kumar Galacc334c72017-04-21 10:55:34 -0500914 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500915}
916#endif
917
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500918/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800919#ifdef CONFIG_TICKLESS_KERNEL
920#define _TICK_ALIGN 0
921#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500922#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800923#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500924
Kumar Galacc334c72017-04-21 10:55:34 -0500925static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400926{
Benjamin Walsh62092182016-12-20 14:39:08 -0500927#ifdef CONFIG_SYS_CLOCK_EXISTS
928
929#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500930 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400931#else
Kumar Galacc334c72017-04-21 10:55:34 -0500932 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500933#endif
934
935#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400936 __ASSERT(ticks == 0, "");
937 return 0;
938#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400939}
940
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941struct k_timer {
942 /*
943 * _timeout structure must be first here if we want to use
944 * dynamic timer allocation. timeout.node is used in the double-linked
945 * list of free timers
946 */
947 struct _timeout timeout;
948
Allan Stephens45bfa372016-10-12 12:39:42 -0500949 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950 _wait_q_t wait_q;
951
952 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500953 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400954
955 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500956 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400957
958 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500959 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400960
Allan Stephens45bfa372016-10-12 12:39:42 -0500961 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500962 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400963
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500964 /* user-specific data, also used to support legacy features */
965 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966
Anas Nashif2f203c22016-12-18 06:57:45 -0500967 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400968};
969
Andrew Boie65a9d2a2017-06-27 10:51:23 -0700970#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500972 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500973 .timeout.wait_q = NULL, \
974 .timeout.thread = NULL, \
975 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400976 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500977 .expiry_fn = expiry, \
978 .stop_fn = stop, \
979 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500980 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500981 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400982 }
983
Andrew Boie65a9d2a2017-06-27 10:51:23 -0700984#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
985
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986/**
Allan Stephensc98da842016-11-11 15:45:03 -0500987 * INTERNAL_HIDDEN @endcond
988 */
989
990/**
991 * @defgroup timer_apis Timer APIs
992 * @ingroup kernel_apis
993 * @{
994 */
995
996/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500997 * @typedef k_timer_expiry_t
998 * @brief Timer expiry function type.
999 *
1000 * A timer's expiry function is executed by the system clock interrupt handler
1001 * each time the timer expires. The expiry function is optional, and is only
1002 * invoked if the timer has been initialized with one.
1003 *
1004 * @param timer Address of timer.
1005 *
1006 * @return N/A
1007 */
1008typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1009
1010/**
1011 * @typedef k_timer_stop_t
1012 * @brief Timer stop function type.
1013 *
1014 * A timer's stop function is executed if the timer is stopped prematurely.
1015 * The function runs in the context of the thread that stops the timer.
1016 * The stop function is optional, and is only invoked if the timer has been
1017 * initialized with one.
1018 *
1019 * @param timer Address of timer.
1020 *
1021 * @return N/A
1022 */
1023typedef void (*k_timer_stop_t)(struct k_timer *timer);
1024
1025/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001030 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001031 *
1032 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * @param expiry_fn Function to invoke each time the timer expires.
1034 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001036#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001037 struct k_timer name \
1038 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001039 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001040
Allan Stephens45bfa372016-10-12 12:39:42 -05001041/**
1042 * @brief Initialize a timer.
1043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001045 *
1046 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * @param expiry_fn Function to invoke each time the timer expires.
1048 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001049 *
1050 * @return N/A
1051 */
1052extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001053 k_timer_expiry_t expiry_fn,
1054 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001055
Allan Stephens45bfa372016-10-12 12:39:42 -05001056/**
1057 * @brief Start a timer.
1058 *
1059 * This routine starts a timer, and resets its status to zero. The timer
1060 * begins counting down using the specified duration and period values.
1061 *
1062 * Attempting to start a timer that is already running is permitted.
1063 * The timer's status is reset to zero and the timer begins counting down
1064 * using the new duration and period values.
1065 *
1066 * @param timer Address of timer.
1067 * @param duration Initial timer duration (in milliseconds).
1068 * @param period Timer period (in milliseconds).
1069 *
1070 * @return N/A
1071 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001072extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001073 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001074
1075/**
1076 * @brief Stop a timer.
1077 *
1078 * This routine stops a running timer prematurely. The timer's stop function,
1079 * if one exists, is invoked by the caller.
1080 *
1081 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001083 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001084 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1085 * if @a k_timer_stop is to be called from ISRs.
1086 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001087 * @param timer Address of timer.
1088 *
1089 * @return N/A
1090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001091extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001092
1093/**
1094 * @brief Read timer status.
1095 *
1096 * This routine reads the timer's status, which indicates the number of times
1097 * it has expired since its status was last read.
1098 *
1099 * Calling this routine resets the timer's status to zero.
1100 *
1101 * @param timer Address of timer.
1102 *
1103 * @return Timer status.
1104 */
Kumar Galacc334c72017-04-21 10:55:34 -05001105extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001106
1107/**
1108 * @brief Synchronize thread to timer expiration.
1109 *
1110 * This routine blocks the calling thread until the timer's status is non-zero
1111 * (indicating that it has expired at least once since it was last examined)
1112 * or the timer is stopped. If the timer status is already non-zero,
1113 * or the timer is already stopped, the caller continues without waiting.
1114 *
1115 * Calling this routine resets the timer's status to zero.
1116 *
1117 * This routine must not be used by interrupt handlers, since they are not
1118 * allowed to block.
1119 *
1120 * @param timer Address of timer.
1121 *
1122 * @return Timer status.
1123 */
Kumar Galacc334c72017-04-21 10:55:34 -05001124extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001125
1126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001128 *
1129 * This routine computes the (approximate) time remaining before a running
1130 * timer next expires. If the timer is not running, it returns zero.
1131 *
1132 * @param timer Address of timer.
1133 *
1134 * @return Remaining time (in milliseconds).
1135 */
Kumar Galacc334c72017-04-21 10:55:34 -05001136static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001137{
1138 return _timeout_remaining_get(&timer->timeout);
1139}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001140
Allan Stephensc98da842016-11-11 15:45:03 -05001141/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001142 * @brief Associate user-specific data with a timer.
1143 *
1144 * This routine records the @a user_data with the @a timer, to be retrieved
1145 * later.
1146 *
1147 * It can be used e.g. in a timer handler shared across multiple subsystems to
1148 * retrieve data specific to the subsystem this timer is associated with.
1149 *
1150 * @param timer Address of timer.
1151 * @param user_data User data to associate with the timer.
1152 *
1153 * @return N/A
1154 */
1155static inline void k_timer_user_data_set(struct k_timer *timer,
1156 void *user_data)
1157{
1158 timer->user_data = user_data;
1159}
1160
1161/**
1162 * @brief Retrieve the user-specific data from a timer.
1163 *
1164 * @param timer Address of timer.
1165 *
1166 * @return The user data.
1167 */
1168static inline void *k_timer_user_data_get(struct k_timer *timer)
1169{
1170 return timer->user_data;
1171}
1172
1173/**
Allan Stephensc98da842016-11-11 15:45:03 -05001174 * @} end defgroup timer_apis
1175 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001176
Allan Stephensc98da842016-11-11 15:45:03 -05001177/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001178 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001179 * @{
1180 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001181
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001182/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * This routine returns the elapsed time since the system booted,
1186 * in milliseconds.
1187 *
1188 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001189 */
Kumar Galacc334c72017-04-21 10:55:34 -05001190extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001191
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001192#ifdef CONFIG_TICKLESS_KERNEL
1193/**
1194 * @brief Enable clock always on in tickless kernel
1195 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001196 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001197 * there are no timer events programmed in tickless kernel
1198 * scheduling. This is necessary if the clock is used to track
1199 * passage of time.
1200 *
1201 * @retval prev_status Previous status of always on flag
1202 */
1203static inline int k_enable_sys_clock_always_on(void)
1204{
1205 int prev_status = _sys_clock_always_on;
1206
1207 _sys_clock_always_on = 1;
1208 _enable_sys_clock();
1209
1210 return prev_status;
1211}
1212
1213/**
1214 * @brief Disable clock always on in tickless kernel
1215 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001216 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001217 * there are no timer events programmed in tickless kernel
1218 * scheduling. To save power, this routine should be called
1219 * immediately when clock is not used to track time.
1220 */
1221static inline void k_disable_sys_clock_always_on(void)
1222{
1223 _sys_clock_always_on = 0;
1224}
1225#else
1226#define k_enable_sys_clock_always_on() do { } while ((0))
1227#define k_disable_sys_clock_always_on() do { } while ((0))
1228#endif
1229
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * This routine returns the lower 32-bits of the elapsed time since the system
1234 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * This routine can be more efficient than k_uptime_get(), as it reduces the
1237 * need for interrupt locking and 64-bit math. However, the 32-bit result
1238 * cannot hold a system uptime time larger than approximately 50 days, so the
1239 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001242 */
Kumar Galacc334c72017-04-21 10:55:34 -05001243extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001244
1245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001246 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001248 * This routine computes the elapsed time between the current system uptime
1249 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * @param reftime Pointer to a reference time, which is updated to the current
1252 * uptime upon return.
1253 *
1254 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001255 */
Kumar Galacc334c72017-04-21 10:55:34 -05001256extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001257
1258/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001259 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001261 * This routine computes the elapsed time between the current system uptime
1262 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001264 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1265 * need for interrupt locking and 64-bit math. However, the 32-bit result
1266 * cannot hold an elapsed time larger than approximately 50 days, so the
1267 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001269 * @param reftime Pointer to a reference time, which is updated to the current
1270 * uptime upon return.
1271 *
1272 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001273 */
Kumar Galacc334c72017-04-21 10:55:34 -05001274extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001275
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * This routine returns the current time, as measured by the system's hardware
1280 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001284#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001285
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001287 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001288 */
1289
Allan Stephensc98da842016-11-11 15:45:03 -05001290/**
1291 * @cond INTERNAL_HIDDEN
1292 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001293
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001294struct k_queue {
1295 _wait_q_t wait_q;
1296 sys_slist_t data_q;
1297 _POLL_EVENT;
1298
1299 _OBJECT_TRACING_NEXT_PTR(k_queue);
1300};
1301
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001302#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001303 { \
1304 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1305 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1306 _POLL_EVENT_OBJ_INIT \
1307 _OBJECT_TRACING_INIT \
1308 }
1309
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001310#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1311
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001312/**
1313 * INTERNAL_HIDDEN @endcond
1314 */
1315
1316/**
1317 * @defgroup queue_apis Queue APIs
1318 * @ingroup kernel_apis
1319 * @{
1320 */
1321
1322/**
1323 * @brief Initialize a queue.
1324 *
1325 * This routine initializes a queue object, prior to its first use.
1326 *
1327 * @param queue Address of the queue.
1328 *
1329 * @return N/A
1330 */
1331extern void k_queue_init(struct k_queue *queue);
1332
1333/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001334 * @brief Cancel waiting on a queue.
1335 *
1336 * This routine causes first thread pending on @a queue, if any, to
1337 * return from k_queue_get() call with NULL value (as if timeout expired).
1338 *
1339 * @note Can be called by ISRs.
1340 *
1341 * @param queue Address of the queue.
1342 *
1343 * @return N/A
1344 */
1345extern void k_queue_cancel_wait(struct k_queue *queue);
1346
1347/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001348 * @brief Append an element to the end of a queue.
1349 *
1350 * This routine appends a data item to @a queue. A queue data item must be
1351 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1352 * reserved for the kernel's use.
1353 *
1354 * @note Can be called by ISRs.
1355 *
1356 * @param queue Address of the queue.
1357 * @param data Address of the data item.
1358 *
1359 * @return N/A
1360 */
1361extern void k_queue_append(struct k_queue *queue, void *data);
1362
1363/**
1364 * @brief Prepend an element to a queue.
1365 *
1366 * This routine prepends a data item to @a queue. A queue data item must be
1367 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1368 * reserved for the kernel's use.
1369 *
1370 * @note Can be called by ISRs.
1371 *
1372 * @param queue Address of the queue.
1373 * @param data Address of the data item.
1374 *
1375 * @return N/A
1376 */
1377extern void k_queue_prepend(struct k_queue *queue, void *data);
1378
1379/**
1380 * @brief Inserts an element to a queue.
1381 *
1382 * This routine inserts a data item to @a queue after previous item. A queue
1383 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1384 * item are reserved for the kernel's use.
1385 *
1386 * @note Can be called by ISRs.
1387 *
1388 * @param queue Address of the queue.
1389 * @param prev Address of the previous data item.
1390 * @param data Address of the data item.
1391 *
1392 * @return N/A
1393 */
1394extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1395
1396/**
1397 * @brief Atomically append a list of elements to a queue.
1398 *
1399 * This routine adds a list of data items to @a queue in one operation.
1400 * The data items must be in a singly-linked list, with the first 32 bits
1401 * in each data item pointing to the next data item; the list must be
1402 * NULL-terminated.
1403 *
1404 * @note Can be called by ISRs.
1405 *
1406 * @param queue Address of the queue.
1407 * @param head Pointer to first node in singly-linked list.
1408 * @param tail Pointer to last node in singly-linked list.
1409 *
1410 * @return N/A
1411 */
1412extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1413
1414/**
1415 * @brief Atomically add a list of elements to a queue.
1416 *
1417 * This routine adds a list of data items to @a queue in one operation.
1418 * The data items must be in a singly-linked list implemented using a
1419 * sys_slist_t object. Upon completion, the original list is empty.
1420 *
1421 * @note Can be called by ISRs.
1422 *
1423 * @param queue Address of the queue.
1424 * @param list Pointer to sys_slist_t object.
1425 *
1426 * @return N/A
1427 */
1428extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1429
1430/**
1431 * @brief Get an element from a queue.
1432 *
1433 * This routine removes first data item from @a queue. The first 32 bits of the
1434 * data item are reserved for the kernel's use.
1435 *
1436 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1437 *
1438 * @param queue Address of the queue.
1439 * @param timeout Waiting period to obtain a data item (in milliseconds),
1440 * or one of the special values K_NO_WAIT and K_FOREVER.
1441 *
1442 * @return Address of the data item if successful; NULL if returned
1443 * without waiting, or waiting period timed out.
1444 */
Kumar Galacc334c72017-04-21 10:55:34 -05001445extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001446
1447/**
1448 * @brief Query a queue to see if it has data available.
1449 *
1450 * Note that the data might be already gone by the time this function returns
1451 * if other threads are also trying to read from the queue.
1452 *
1453 * @note Can be called by ISRs.
1454 *
1455 * @param queue Address of the queue.
1456 *
1457 * @return Non-zero if the queue is empty.
1458 * @return 0 if data is available.
1459 */
1460static inline int k_queue_is_empty(struct k_queue *queue)
1461{
1462 return (int)sys_slist_is_empty(&queue->data_q);
1463}
1464
1465/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001466 * @brief Peek element at the head of queue.
1467 *
1468 * Return element from the head of queue without removing it.
1469 *
1470 * @param queue Address of the queue.
1471 *
1472 * @return Head element, or NULL if queue is empty.
1473 */
1474static inline void *k_queue_peek_head(struct k_queue *queue)
1475{
1476 return sys_slist_peek_head(&queue->data_q);
1477}
1478
1479/**
1480 * @brief Peek element at the tail of queue.
1481 *
1482 * Return element from the tail of queue without removing it.
1483 *
1484 * @param queue Address of the queue.
1485 *
1486 * @return Tail element, or NULL if queue is empty.
1487 */
1488static inline void *k_queue_peek_tail(struct k_queue *queue)
1489{
1490 return sys_slist_peek_tail(&queue->data_q);
1491}
1492
1493/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001494 * @brief Statically define and initialize a queue.
1495 *
1496 * The queue can be accessed outside the module where it is defined using:
1497 *
1498 * @code extern struct k_queue <name>; @endcode
1499 *
1500 * @param name Name of the queue.
1501 */
1502#define K_QUEUE_DEFINE(name) \
1503 struct k_queue name \
1504 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001505 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001506
1507/**
1508 * @} end defgroup queue_apis
1509 */
1510
1511/**
1512 * @cond INTERNAL_HIDDEN
1513 */
1514
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001516 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001517};
1518
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001519#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001520 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001521 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001522 }
1523
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001524#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1525
Allan Stephensc98da842016-11-11 15:45:03 -05001526/**
1527 * INTERNAL_HIDDEN @endcond
1528 */
1529
1530/**
1531 * @defgroup fifo_apis Fifo APIs
1532 * @ingroup kernel_apis
1533 * @{
1534 */
1535
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001537 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001542 *
1543 * @return N/A
1544 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001545#define k_fifo_init(fifo) \
1546 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001547
1548/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001549 * @brief Cancel waiting on a fifo.
1550 *
1551 * This routine causes first thread pending on @a fifo, if any, to
1552 * return from k_fifo_get() call with NULL value (as if timeout
1553 * expired).
1554 *
1555 * @note Can be called by ISRs.
1556 *
1557 * @param fifo Address of the fifo.
1558 *
1559 * @return N/A
1560 */
1561#define k_fifo_cancel_wait(fifo) \
1562 k_queue_cancel_wait((struct k_queue *) fifo)
1563
1564/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001565 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * This routine adds a data item to @a fifo. A fifo data item must be
1568 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1569 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001571 * @note Can be called by ISRs.
1572 *
1573 * @param fifo Address of the fifo.
1574 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001575 *
1576 * @return N/A
1577 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001578#define k_fifo_put(fifo, data) \
1579 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001580
1581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001582 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001584 * This routine adds a list of data items to @a fifo in one operation.
1585 * The data items must be in a singly-linked list, with the first 32 bits
1586 * each data item pointing to the next data item; the list must be
1587 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * @param fifo Address of the fifo.
1592 * @param head Pointer to first node in singly-linked list.
1593 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001594 *
1595 * @return N/A
1596 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001597#define k_fifo_put_list(fifo, head, tail) \
1598 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001599
1600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001601 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * This routine adds a list of data items to @a fifo in one operation.
1604 * The data items must be in a singly-linked list implemented using a
1605 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001606 * and must be re-initialized via sys_slist_init().
1607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * @note Can be called by ISRs.
1609 *
1610 * @param fifo Address of the fifo.
1611 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001612 *
1613 * @return N/A
1614 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001615#define k_fifo_put_slist(fifo, list) \
1616 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001617
1618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001621 * This routine removes a data item from @a fifo in a "first in, first out"
1622 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1625 *
1626 * @param fifo Address of the fifo.
1627 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001628 * or one of the special values K_NO_WAIT and K_FOREVER.
1629 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001630 * @return Address of the data item if successful; NULL if returned
1631 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001632 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001633#define k_fifo_get(fifo, timeout) \
1634 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001636/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001637 * @brief Query a fifo to see if it has data available.
1638 *
1639 * Note that the data might be already gone by the time this function returns
1640 * if other threads is also trying to read from the fifo.
1641 *
1642 * @note Can be called by ISRs.
1643 *
1644 * @param fifo Address of the fifo.
1645 *
1646 * @return Non-zero if the fifo is empty.
1647 * @return 0 if data is available.
1648 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001649#define k_fifo_is_empty(fifo) \
1650 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001651
1652/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001653 * @brief Peek element at the head of fifo.
1654 *
1655 * Return element from the head of fifo without removing it. A usecase
1656 * for this is if elements of the fifo are themselves containers. Then
1657 * on each iteration of processing, a head container will be peeked,
1658 * and some data processed out of it, and only if the container is empty,
1659 * it will be completely remove from the fifo.
1660 *
1661 * @param fifo Address of the fifo.
1662 *
1663 * @return Head element, or NULL if the fifo is empty.
1664 */
1665#define k_fifo_peek_head(fifo) \
1666 k_queue_peek_head((struct k_queue *) fifo)
1667
1668/**
1669 * @brief Peek element at the tail of fifo.
1670 *
1671 * Return element from the tail of fifo (without removing it). A usecase
1672 * for this is if elements of the fifo are themselves containers. Then
1673 * it may be useful to add more data to the last container in fifo.
1674 *
1675 * @param fifo Address of the fifo.
1676 *
1677 * @return Tail element, or NULL if fifo is empty.
1678 */
1679#define k_fifo_peek_tail(fifo) \
1680 k_queue_peek_tail((struct k_queue *) fifo)
1681
1682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001687 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001690 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001691#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001692 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001693 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001694 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001695
Allan Stephensc98da842016-11-11 15:45:03 -05001696/**
1697 * @} end defgroup fifo_apis
1698 */
1699
1700/**
1701 * @cond INTERNAL_HIDDEN
1702 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001703
1704struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001705 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001706};
1707
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001708#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001709 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001710 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001711 }
1712
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001713#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1714
Allan Stephensc98da842016-11-11 15:45:03 -05001715/**
1716 * INTERNAL_HIDDEN @endcond
1717 */
1718
1719/**
1720 * @defgroup lifo_apis Lifo APIs
1721 * @ingroup kernel_apis
1722 * @{
1723 */
1724
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
1732 * @return N/A
1733 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001734#define k_lifo_init(lifo) \
1735 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736
1737/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * This routine adds a data item to @a lifo. A lifo data item must be
1741 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1742 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * @note Can be called by ISRs.
1745 *
1746 * @param lifo Address of the lifo.
1747 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 *
1749 * @return N/A
1750 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001751#define k_lifo_put(lifo, data) \
1752 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753
1754/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * This routine removes a data item from @a lifo in a "last in, first out"
1758 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1761 *
1762 * @param lifo Address of the lifo.
1763 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 * or one of the special values K_NO_WAIT and K_FOREVER.
1765 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001766 * @return Address of the data item if successful; NULL if returned
1767 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001769#define k_lifo_get(lifo, timeout) \
1770 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001771
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001777 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001779 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001781#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001782 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001783 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001784 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001785
Allan Stephensc98da842016-11-11 15:45:03 -05001786/**
1787 * @} end defgroup lifo_apis
1788 */
1789
1790/**
1791 * @cond INTERNAL_HIDDEN
1792 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001793
1794struct k_stack {
1795 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001796 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001797
Anas Nashif2f203c22016-12-18 06:57:45 -05001798 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001799};
1800
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001801#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001802 { \
1803 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1804 .base = stack_buffer, \
1805 .next = stack_buffer, \
1806 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001807 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001808 }
1809
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001810#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1811
Allan Stephensc98da842016-11-11 15:45:03 -05001812/**
1813 * INTERNAL_HIDDEN @endcond
1814 */
1815
1816/**
1817 * @defgroup stack_apis Stack APIs
1818 * @ingroup kernel_apis
1819 * @{
1820 */
1821
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822/**
1823 * @brief Initialize a stack.
1824 *
1825 * This routine initializes a stack object, prior to its first use.
1826 *
1827 * @param stack Address of the stack.
1828 * @param buffer Address of array used to hold stacked values.
1829 * @param num_entries Maximum number of values that can be stacked.
1830 *
1831 * @return N/A
1832 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001833extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001834 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835
1836/**
1837 * @brief Push an element onto a stack.
1838 *
1839 * This routine adds a 32-bit value @a data to @a stack.
1840 *
1841 * @note Can be called by ISRs.
1842 *
1843 * @param stack Address of the stack.
1844 * @param data Value to push onto the stack.
1845 *
1846 * @return N/A
1847 */
Kumar Galacc334c72017-04-21 10:55:34 -05001848extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849
1850/**
1851 * @brief Pop an element from a stack.
1852 *
1853 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1854 * manner and stores the value in @a data.
1855 *
1856 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1857 *
1858 * @param stack Address of the stack.
1859 * @param data Address of area to hold the value popped from the stack.
1860 * @param timeout Waiting period to obtain a value (in milliseconds),
1861 * or one of the special values K_NO_WAIT and K_FOREVER.
1862 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001863 * @retval 0 Element popped from stack.
1864 * @retval -EBUSY Returned without waiting.
1865 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001866 */
Kumar Galacc334c72017-04-21 10:55:34 -05001867extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001868
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001873 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001874 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @param name Name of the stack.
1877 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001878 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001879#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001880 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001881 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001882 struct k_stack name \
1883 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001884 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001885 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001886
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887/**
Allan Stephensc98da842016-11-11 15:45:03 -05001888 * @} end defgroup stack_apis
1889 */
1890
Allan Stephens6bba9b02016-11-16 14:56:54 -05001891struct k_work;
1892
Allan Stephensc98da842016-11-11 15:45:03 -05001893/**
1894 * @defgroup workqueue_apis Workqueue Thread APIs
1895 * @ingroup kernel_apis
1896 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001897 */
1898
Allan Stephens6bba9b02016-11-16 14:56:54 -05001899/**
1900 * @typedef k_work_handler_t
1901 * @brief Work item handler function type.
1902 *
1903 * A work item's handler function is executed by a workqueue's thread
1904 * when the work item is processed by the workqueue.
1905 *
1906 * @param work Address of the work item.
1907 *
1908 * @return N/A
1909 */
1910typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911
1912/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001913 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001915
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916struct k_work_q {
1917 struct k_fifo fifo;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001918 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001919};
1920
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001921enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001922 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923};
1924
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001925struct k_work {
1926 void *_reserved; /* Used by k_fifo implementation. */
1927 k_work_handler_t handler;
1928 atomic_t flags[1];
1929};
1930
Allan Stephens6bba9b02016-11-16 14:56:54 -05001931struct k_delayed_work {
1932 struct k_work work;
1933 struct _timeout timeout;
1934 struct k_work_q *work_q;
1935};
1936
1937extern struct k_work_q k_sys_work_q;
1938
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001940 * INTERNAL_HIDDEN @endcond
1941 */
1942
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001943#define _K_WORK_INITIALIZER(work_handler) \
1944 { \
1945 ._reserved = NULL, \
1946 .handler = work_handler, \
1947 .flags = { 0 } \
1948 }
1949
1950#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
1951
Allan Stephens6bba9b02016-11-16 14:56:54 -05001952/**
1953 * @brief Initialize a statically-defined work item.
1954 *
1955 * This macro can be used to initialize a statically-defined workqueue work
1956 * item, prior to its first use. For example,
1957 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001958 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05001959 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001960 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05001961 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001963#define K_WORK_DEFINE(work, work_handler) \
1964 struct k_work work \
1965 __in_section(_k_work, static, work) = \
1966 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001967
1968/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001969 * @brief Initialize a work item.
1970 *
1971 * This routine initializes a workqueue work item, prior to its first use.
1972 *
1973 * @param work Address of work item.
1974 * @param handler Function to invoke each time work item is processed.
1975 *
1976 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977 */
1978static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1979{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001980 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001981 work->handler = handler;
1982}
1983
1984/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001985 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001986 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001987 * This routine submits work item @a work to be processed by workqueue
1988 * @a work_q. If the work item is already pending in the workqueue's queue
1989 * as a result of an earlier submission, this routine has no effect on the
1990 * work item. If the work item has already been processed, or is currently
1991 * being processed, its work is considered complete and the work item can be
1992 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001993 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001994 * @warning
1995 * A submitted work item must not be modified until it has been processed
1996 * by the workqueue.
1997 *
1998 * @note Can be called by ISRs.
1999 *
2000 * @param work_q Address of workqueue.
2001 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002002 *
2003 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004 */
2005static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2006 struct k_work *work)
2007{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002008 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009 k_fifo_put(&work_q->fifo, work);
2010 }
2011}
2012
2013/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002014 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002016 * This routine indicates if work item @a work is pending in a workqueue's
2017 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002019 * @note Can be called by ISRs.
2020 *
2021 * @param work Address of work item.
2022 *
2023 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002024 */
2025static inline int k_work_pending(struct k_work *work)
2026{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002027 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002028}
2029
2030/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002031 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002032 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002033 * This routine starts workqueue @a work_q. The workqueue spawns its work
2034 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002036 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002037 * @param stack Pointer to work queue thread's stack space, as defined by
2038 * K_THREAD_STACK_DEFINE()
2039 * @param stack_size Size of the work queue thread's stack (in bytes), which
2040 * should either be the same constant passed to
2041 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002042 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 *
2044 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002045 */
Allan Stephens904cf972016-10-07 13:59:23 -05002046extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002047 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002049/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002050 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002051 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002052 * This routine initializes a workqueue delayed work item, prior to
2053 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002055 * @param work Address of delayed work item.
2056 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
2058 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002060extern void k_delayed_work_init(struct k_delayed_work *work,
2061 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062
2063/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002064 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002065 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002066 * This routine schedules work item @a work to be processed by workqueue
2067 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002068 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002069 * Only when the countdown completes is the work item actually submitted to
2070 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002072 * Submitting a previously submitted delayed work item that is still
2073 * counting down cancels the existing submission and restarts the countdown
2074 * using the new delay. If the work item is currently pending on the
2075 * workqueue's queue because the countdown has completed it is too late to
2076 * resubmit the item, and resubmission fails without impacting the work item.
2077 * If the work item has already been processed, or is currently being processed,
2078 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002080 * @warning
2081 * A delayed work item must not be modified until it has been processed
2082 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002084 * @note Can be called by ISRs.
2085 *
2086 * @param work_q Address of workqueue.
2087 * @param work Address of delayed work item.
2088 * @param delay Delay before submitting the work item (in milliseconds).
2089 *
2090 * @retval 0 Work item countdown started.
2091 * @retval -EINPROGRESS Work item is already pending.
2092 * @retval -EINVAL Work item is being processed or has completed its work.
2093 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002095extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2096 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002097 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098
2099/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002100 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002101 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002102 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002103 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002104 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002105 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002106 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002107 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002108 * @param work Address of delayed work item.
2109 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002110 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002111 * @retval -EINPROGRESS Work item is already pending.
2112 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002114extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002117 * @brief Submit a work item to the system workqueue.
2118 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002119 * This routine submits work item @a work to be processed by the system
2120 * workqueue. If the work item is already pending in the workqueue's queue
2121 * as a result of an earlier submission, this routine has no effect on the
2122 * work item. If the work item has already been processed, or is currently
2123 * being processed, its work is considered complete and the work item can be
2124 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002126 * @warning
2127 * Work items submitted to the system workqueue should avoid using handlers
2128 * that block or yield since this may prevent the system workqueue from
2129 * processing other work items in a timely manner.
2130 *
2131 * @note Can be called by ISRs.
2132 *
2133 * @param work Address of work item.
2134 *
2135 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002136 */
2137static inline void k_work_submit(struct k_work *work)
2138{
2139 k_work_submit_to_queue(&k_sys_work_q, work);
2140}
2141
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002142/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143 * @brief Submit a delayed work item to the system workqueue.
2144 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002145 * This routine schedules work item @a work to be processed by the system
2146 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002147 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148 * Only when the countdown completes is the work item actually submitted to
2149 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002151 * Submitting a previously submitted delayed work item that is still
2152 * counting down cancels the existing submission and restarts the countdown
2153 * using the new delay. If the work item is currently pending on the
2154 * workqueue's queue because the countdown has completed it is too late to
2155 * resubmit the item, and resubmission fails without impacting the work item.
2156 * If the work item has already been processed, or is currently being processed,
2157 * its work is considered complete and the work item can be resubmitted.
2158 *
2159 * @warning
2160 * Work items submitted to the system workqueue should avoid using handlers
2161 * that block or yield since this may prevent the system workqueue from
2162 * processing other work items in a timely manner.
2163 *
2164 * @note Can be called by ISRs.
2165 *
2166 * @param work Address of delayed work item.
2167 * @param delay Delay before submitting the work item (in milliseconds).
2168 *
2169 * @retval 0 Work item countdown started.
2170 * @retval -EINPROGRESS Work item is already pending.
2171 * @retval -EINVAL Work item is being processed or has completed its work.
2172 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173 */
2174static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002175 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002177 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178}
2179
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002181 * @brief Get time remaining before a delayed work gets scheduled.
2182 *
2183 * This routine computes the (approximate) time remaining before a
2184 * delayed work gets executed. If the delayed work is not waiting to be
2185 * schedules, it returns zero.
2186 *
2187 * @param work Delayed work item.
2188 *
2189 * @return Remaining time (in milliseconds).
2190 */
Kumar Galacc334c72017-04-21 10:55:34 -05002191static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002192{
2193 return _timeout_remaining_get(&work->timeout);
2194}
2195
2196/**
Allan Stephensc98da842016-11-11 15:45:03 -05002197 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198 */
2199
Allan Stephensc98da842016-11-11 15:45:03 -05002200/**
2201 * @cond INTERNAL_HIDDEN
2202 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002203
2204struct k_mutex {
2205 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002206 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002207 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002208 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002209
Anas Nashif2f203c22016-12-18 06:57:45 -05002210 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211};
2212
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002213#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214 { \
2215 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2216 .owner = NULL, \
2217 .lock_count = 0, \
2218 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002219 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 }
2221
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002222#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2223
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002224/**
Allan Stephensc98da842016-11-11 15:45:03 -05002225 * INTERNAL_HIDDEN @endcond
2226 */
2227
2228/**
2229 * @defgroup mutex_apis Mutex APIs
2230 * @ingroup kernel_apis
2231 * @{
2232 */
2233
2234/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002237 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002238 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002239 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002241 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002242 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002243#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002244 struct k_mutex name \
2245 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002246 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002247
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002249 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002251 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002253 * Upon completion, the mutex is available and does not have an owner.
2254 *
2255 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 *
2257 * @return N/A
2258 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260
2261/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * This routine locks @a mutex. If the mutex is locked by another thread,
2265 * the calling thread waits until the mutex becomes available or until
2266 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * A thread is permitted to lock a mutex it has already locked. The operation
2269 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002271 * @param mutex Address of the mutex.
2272 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 * or one of the special values K_NO_WAIT and K_FOREVER.
2274 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002275 * @retval 0 Mutex locked.
2276 * @retval -EBUSY Returned without waiting.
2277 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 */
Kumar Galacc334c72017-04-21 10:55:34 -05002279extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280
2281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002282 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002284 * This routine unlocks @a mutex. The mutex must already be locked by the
2285 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
2287 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002288 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 * thread.
2290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002291 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
2293 * @return N/A
2294 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295extern void k_mutex_unlock(struct k_mutex *mutex);
2296
Allan Stephensc98da842016-11-11 15:45:03 -05002297/**
2298 * @} end defgroup mutex_apis
2299 */
2300
2301/**
2302 * @cond INTERNAL_HIDDEN
2303 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304
2305struct k_sem {
2306 _wait_q_t wait_q;
2307 unsigned int count;
2308 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002309 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310
Anas Nashif2f203c22016-12-18 06:57:45 -05002311 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312};
2313
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002314#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002315 { \
2316 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2317 .count = initial_count, \
2318 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002319 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002320 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002321 }
2322
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002323#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2324
Allan Stephensc98da842016-11-11 15:45:03 -05002325/**
2326 * INTERNAL_HIDDEN @endcond
2327 */
2328
2329/**
2330 * @defgroup semaphore_apis Semaphore APIs
2331 * @ingroup kernel_apis
2332 * @{
2333 */
2334
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002336 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002338 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * @param sem Address of the semaphore.
2341 * @param initial_count Initial semaphore count.
2342 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002343 *
2344 * @return N/A
2345 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002346extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2347 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002348
2349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2355 *
2356 * @param sem Address of the semaphore.
2357 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002358 * or one of the special values K_NO_WAIT and K_FOREVER.
2359 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002360 * @note When porting code from the nanokernel legacy API to the new API, be
2361 * careful with the return value of this function. The return value is the
2362 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2363 * non-zero means failure, while the nano_sem_take family returns 1 for success
2364 * and 0 for failure.
2365 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002366 * @retval 0 Semaphore taken.
2367 * @retval -EBUSY Returned without waiting.
2368 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002369 */
Kumar Galacc334c72017-04-21 10:55:34 -05002370extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002371
2372/**
2373 * @brief Give a semaphore.
2374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002375 * This routine gives @a sem, unless the semaphore is already at its maximum
2376 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002378 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002381 *
2382 * @return N/A
2383 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384extern void k_sem_give(struct k_sem *sem);
2385
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002386/**
2387 * @brief Reset a semaphore's count to zero.
2388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002392 *
2393 * @return N/A
2394 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002395static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396{
2397 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398}
2399
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002400/**
2401 * @brief Get a semaphore's count.
2402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002408 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002409static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410{
2411 return sem->count;
2412}
2413
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002417 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002418 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002419 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * @param name Name of the semaphore.
2422 * @param initial_count Initial semaphore count.
2423 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002424 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002425#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002426 struct k_sem name \
2427 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002428 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429
Allan Stephensc98da842016-11-11 15:45:03 -05002430/**
2431 * @} end defgroup semaphore_apis
2432 */
2433
2434/**
2435 * @defgroup alert_apis Alert APIs
2436 * @ingroup kernel_apis
2437 * @{
2438 */
2439
Allan Stephens5eceb852016-11-16 10:16:30 -05002440/**
2441 * @typedef k_alert_handler_t
2442 * @brief Alert handler function type.
2443 *
2444 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002445 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002446 * and is only invoked if the alert has been initialized with one.
2447 *
2448 * @param alert Address of the alert.
2449 *
2450 * @return 0 if alert has been consumed; non-zero if alert should pend.
2451 */
2452typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002453
2454/**
2455 * @} end defgroup alert_apis
2456 */
2457
2458/**
2459 * @cond INTERNAL_HIDDEN
2460 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002462#define K_ALERT_DEFAULT NULL
2463#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002465struct k_alert {
2466 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467 atomic_t send_count;
2468 struct k_work work_item;
2469 struct k_sem sem;
2470
Anas Nashif2f203c22016-12-18 06:57:45 -05002471 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472};
2473
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002474extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002476#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002478 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002480 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2481 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002482 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483 }
2484
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002485#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2486
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487/**
Allan Stephensc98da842016-11-11 15:45:03 -05002488 * INTERNAL_HIDDEN @endcond
2489 */
2490
2491/**
2492 * @addtogroup alert_apis
2493 * @{
2494 */
2495
2496/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002499 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002501 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * @param name Name of the alert.
2504 * @param alert_handler Action to take when alert is sent. Specify either
2505 * the address of a function to be invoked by the system workqueue
2506 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2507 * K_ALERT_DEFAULT (which causes the alert to pend).
2508 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002510#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002511 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002512 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002513 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002514 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @param alert Address of the alert.
2522 * @param handler Action to take when alert is sent. Specify either the address
2523 * of a function to be invoked by the system workqueue thread,
2524 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2525 * K_ALERT_DEFAULT (which causes the alert to pend).
2526 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
2528 * @return N/A
2529 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002530extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2531 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532
2533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2539 *
2540 * @param alert Address of the alert.
2541 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 * or one of the special values K_NO_WAIT and K_FOREVER.
2543 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002544 * @retval 0 Alert received.
2545 * @retval -EBUSY Returned without waiting.
2546 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547 */
Kumar Galacc334c72017-04-21 10:55:34 -05002548extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549
2550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553 * This routine signals @a alert. The action specified for @a alert will
2554 * be taken, which may trigger the execution of an alert handler function
2555 * and/or cause the alert to pend (assuming the alert has not reached its
2556 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * @note Can be called by ISRs.
2559 *
2560 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002561 *
2562 * @return N/A
2563 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002564extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565
2566/**
Allan Stephensc98da842016-11-11 15:45:03 -05002567 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 */
2569
Allan Stephensc98da842016-11-11 15:45:03 -05002570/**
2571 * @cond INTERNAL_HIDDEN
2572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573
2574struct k_msgq {
2575 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002576 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002577 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 char *buffer_start;
2579 char *buffer_end;
2580 char *read_ptr;
2581 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002582 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583
Anas Nashif2f203c22016-12-18 06:57:45 -05002584 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585};
2586
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002587#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002588 { \
2589 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002590 .max_msgs = q_max_msgs, \
2591 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002593 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594 .read_ptr = q_buffer, \
2595 .write_ptr = q_buffer, \
2596 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002597 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598 }
2599
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002600#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2601
Peter Mitsis1da807e2016-10-06 11:36:59 -04002602/**
Allan Stephensc98da842016-11-11 15:45:03 -05002603 * INTERNAL_HIDDEN @endcond
2604 */
2605
2606/**
2607 * @defgroup msgq_apis Message Queue APIs
2608 * @ingroup kernel_apis
2609 * @{
2610 */
2611
2612/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002613 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2616 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002617 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2618 * message is similarly aligned to this boundary, @a q_msg_size must also be
2619 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * The message queue can be accessed outside the module where it is defined
2622 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002623 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002624 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @param q_name Name of the message queue.
2627 * @param q_msg_size Message size (in bytes).
2628 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002629 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002630 */
2631#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2632 static char __noinit __aligned(q_align) \
2633 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002634 struct k_msgq q_name \
2635 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002636 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002637 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638
Peter Mitsisd7a37502016-10-13 11:37:40 -04002639/**
2640 * @brief Initialize a message queue.
2641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642 * This routine initializes a message queue object, prior to its first use.
2643 *
Allan Stephensda827222016-11-09 14:23:58 -06002644 * The message queue's ring buffer must contain space for @a max_msgs messages,
2645 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2646 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2647 * that each message is similarly aligned to this boundary, @a q_msg_size
2648 * must also be a multiple of N.
2649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @param q Address of the message queue.
2651 * @param buffer Pointer to ring buffer that holds queued messages.
2652 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002653 * @param max_msgs Maximum number of messages that can be queued.
2654 *
2655 * @return N/A
2656 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002657extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002658 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002659
2660/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002664 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002665 * @note Can be called by ISRs.
2666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @param q Address of the message queue.
2668 * @param data Pointer to the message.
2669 * @param timeout Waiting period to add the message (in milliseconds),
2670 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002671 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002672 * @retval 0 Message sent.
2673 * @retval -ENOMSG Returned without waiting or queue purged.
2674 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002675 */
Kumar Galacc334c72017-04-21 10:55:34 -05002676extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002677
2678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002681 * This routine receives a message from message queue @a q in a "first in,
2682 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002683 *
Allan Stephensc98da842016-11-11 15:45:03 -05002684 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * @param q Address of the message queue.
2687 * @param data Address of area to hold the received message.
2688 * @param timeout Waiting period to receive the message (in milliseconds),
2689 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002690 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002691 * @retval 0 Message received.
2692 * @retval -ENOMSG Returned without waiting.
2693 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002694 */
Kumar Galacc334c72017-04-21 10:55:34 -05002695extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002696
2697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002698 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * This routine discards all unreceived messages in a message queue's ring
2701 * buffer. Any threads that are blocked waiting to send a message to the
2702 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002705 *
2706 * @return N/A
2707 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708extern void k_msgq_purge(struct k_msgq *q);
2709
Peter Mitsis67be2492016-10-07 11:44:34 -04002710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002713 * This routine returns the number of unused entries in a message queue's
2714 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002716 * @param q Address of the message queue.
2717 *
2718 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002719 */
Kumar Galacc334c72017-04-21 10:55:34 -05002720static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002721{
2722 return q->max_msgs - q->used_msgs;
2723}
2724
Peter Mitsisd7a37502016-10-13 11:37:40 -04002725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002728 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002730 * @param q Address of the message queue.
2731 *
2732 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002733 */
Kumar Galacc334c72017-04-21 10:55:34 -05002734static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735{
2736 return q->used_msgs;
2737}
2738
Allan Stephensc98da842016-11-11 15:45:03 -05002739/**
2740 * @} end defgroup msgq_apis
2741 */
2742
2743/**
2744 * @defgroup mem_pool_apis Memory Pool APIs
2745 * @ingroup kernel_apis
2746 * @{
2747 */
2748
Andy Ross73cb9582017-05-09 10:42:39 -07002749/* Note on sizing: the use of a 20 bit field for block means that,
2750 * assuming a reasonable minimum block size of 16 bytes, we're limited
2751 * to 16M of memory managed by a single pool. Long term it would be
2752 * good to move to a variable bit size based on configuration.
2753 */
2754struct k_mem_block_id {
2755 u32_t pool : 8;
2756 u32_t level : 4;
2757 u32_t block : 20;
2758};
2759
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002762 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763};
2764
Allan Stephensc98da842016-11-11 15:45:03 -05002765/**
2766 * @} end defgroup mem_pool_apis
2767 */
2768
2769/**
2770 * @defgroup mailbox_apis Mailbox APIs
2771 * @ingroup kernel_apis
2772 * @{
2773 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002774
2775struct k_mbox_msg {
2776 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002777 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002779 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002781 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782 /** sender's message data buffer */
2783 void *tx_data;
2784 /** internal use only - needed for legacy API support */
2785 void *_rx_data;
2786 /** message data block descriptor */
2787 struct k_mem_block tx_block;
2788 /** source thread id */
2789 k_tid_t rx_source_thread;
2790 /** target thread id */
2791 k_tid_t tx_target_thread;
2792 /** internal use only - thread waiting on send (may be a dummy) */
2793 k_tid_t _syncing_thread;
2794#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2795 /** internal use only - semaphore used during asynchronous send */
2796 struct k_sem *_async_sem;
2797#endif
2798};
2799
Allan Stephensc98da842016-11-11 15:45:03 -05002800/**
2801 * @cond INTERNAL_HIDDEN
2802 */
2803
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804struct k_mbox {
2805 _wait_q_t tx_msg_queue;
2806 _wait_q_t rx_msg_queue;
2807
Anas Nashif2f203c22016-12-18 06:57:45 -05002808 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809};
2810
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002811#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812 { \
2813 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2814 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002815 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816 }
2817
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002818#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2819
Peter Mitsis12092702016-10-14 12:57:23 -04002820/**
Allan Stephensc98da842016-11-11 15:45:03 -05002821 * INTERNAL_HIDDEN @endcond
2822 */
2823
2824/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002829 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002831 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002832 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002834 struct k_mbox name \
2835 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002836 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837
Peter Mitsis12092702016-10-14 12:57:23 -04002838/**
2839 * @brief Initialize a mailbox.
2840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * This routine initializes a mailbox object, prior to its first use.
2842 *
2843 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002844 *
2845 * @return N/A
2846 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847extern void k_mbox_init(struct k_mbox *mbox);
2848
Peter Mitsis12092702016-10-14 12:57:23 -04002849/**
2850 * @brief Send a mailbox message in a synchronous manner.
2851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * This routine sends a message to @a mbox and waits for a receiver to both
2853 * receive and process it. The message data may be in a buffer, in a memory
2854 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * @param mbox Address of the mailbox.
2857 * @param tx_msg Address of the transmit message descriptor.
2858 * @param timeout Waiting period for the message to be received (in
2859 * milliseconds), or one of the special values K_NO_WAIT
2860 * and K_FOREVER. Once the message has been received,
2861 * this routine waits as long as necessary for the message
2862 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002863 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002864 * @retval 0 Message sent.
2865 * @retval -ENOMSG Returned without waiting.
2866 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002867 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002868extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002869 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002870
Peter Mitsis12092702016-10-14 12:57:23 -04002871/**
2872 * @brief Send a mailbox message in an asynchronous manner.
2873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * This routine sends a message to @a mbox without waiting for a receiver
2875 * to process it. The message data may be in a buffer, in a memory pool block,
2876 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2877 * will be given when the message has been both received and completely
2878 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * @param mbox Address of the mailbox.
2881 * @param tx_msg Address of the transmit message descriptor.
2882 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002883 *
2884 * @return N/A
2885 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002886extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002887 struct k_sem *sem);
2888
Peter Mitsis12092702016-10-14 12:57:23 -04002889/**
2890 * @brief Receive a mailbox message.
2891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * This routine receives a message from @a mbox, then optionally retrieves
2893 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @param mbox Address of the mailbox.
2896 * @param rx_msg Address of the receive message descriptor.
2897 * @param buffer Address of the buffer to receive data, or NULL to defer data
2898 * retrieval and message disposal until later.
2899 * @param timeout Waiting period for a message to be received (in
2900 * milliseconds), or one of the special values K_NO_WAIT
2901 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002902 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002903 * @retval 0 Message received.
2904 * @retval -ENOMSG Returned without waiting.
2905 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002906 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002907extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002908 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002909
2910/**
2911 * @brief Retrieve mailbox message data into a buffer.
2912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * This routine completes the processing of a received message by retrieving
2914 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002915 *
2916 * Alternatively, this routine can be used to dispose of a received message
2917 * without retrieving its data.
2918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @param rx_msg Address of the receive message descriptor.
2920 * @param buffer Address of the buffer to receive data, or NULL to discard
2921 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002922 *
2923 * @return N/A
2924 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002925extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002926
2927/**
2928 * @brief Retrieve mailbox message data into a memory pool block.
2929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * This routine completes the processing of a received message by retrieving
2931 * its data into a memory pool block, then disposing of the message.
2932 * The memory pool block that results from successful retrieval must be
2933 * returned to the pool once the data has been processed, even in cases
2934 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002935 *
2936 * Alternatively, this routine can be used to dispose of a received message
2937 * without retrieving its data. In this case there is no need to return a
2938 * memory pool block to the pool.
2939 *
2940 * This routine allocates a new memory pool block for the data only if the
2941 * data is not already in one. If a new block cannot be allocated, the routine
2942 * returns a failure code and the received message is left unchanged. This
2943 * permits the caller to reattempt data retrieval at a later time or to dispose
2944 * of the received message without retrieving its data.
2945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002946 * @param rx_msg Address of a receive message descriptor.
2947 * @param pool Address of memory pool, or NULL to discard data.
2948 * @param block Address of the area to hold memory pool block info.
2949 * @param timeout Waiting period to wait for a memory pool block (in
2950 * milliseconds), or one of the special values K_NO_WAIT
2951 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002952 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002953 * @retval 0 Data retrieved.
2954 * @retval -ENOMEM Returned without waiting.
2955 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002956 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002957extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002958 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002959 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960
Allan Stephensc98da842016-11-11 15:45:03 -05002961/**
2962 * @} end defgroup mailbox_apis
2963 */
2964
2965/**
2966 * @cond INTERNAL_HIDDEN
2967 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002968
2969struct k_pipe {
2970 unsigned char *buffer; /* Pipe buffer: may be NULL */
2971 size_t size; /* Buffer size */
2972 size_t bytes_used; /* # bytes used in buffer */
2973 size_t read_index; /* Where in buffer to read from */
2974 size_t write_index; /* Where in buffer to write */
2975
2976 struct {
2977 _wait_q_t readers; /* Reader wait queue */
2978 _wait_q_t writers; /* Writer wait queue */
2979 } wait_q;
2980
Anas Nashif2f203c22016-12-18 06:57:45 -05002981 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982};
2983
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002984#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 { \
2986 .buffer = pipe_buffer, \
2987 .size = pipe_buffer_size, \
2988 .bytes_used = 0, \
2989 .read_index = 0, \
2990 .write_index = 0, \
2991 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2992 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002993 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002994 }
2995
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002996#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
2997
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002998/**
Allan Stephensc98da842016-11-11 15:45:03 -05002999 * INTERNAL_HIDDEN @endcond
3000 */
3001
3002/**
3003 * @defgroup pipe_apis Pipe APIs
3004 * @ingroup kernel_apis
3005 * @{
3006 */
3007
3008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003012 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003013 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * @param name Name of the pipe.
3016 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3017 * or zero if no ring buffer is used.
3018 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003019 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003020#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3021 static unsigned char __noinit __aligned(pipe_align) \
3022 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003023 struct k_pipe name \
3024 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003025 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * This routine initializes a pipe object, prior to its first use.
3031 *
3032 * @param pipe Address of the pipe.
3033 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3034 * is used.
3035 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3036 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037 *
3038 * @return N/A
3039 */
3040extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3041 size_t size);
3042
3043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003044 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003048 * @param pipe Address of the pipe.
3049 * @param data Address of data to write.
3050 * @param bytes_to_write Size of data (in bytes).
3051 * @param bytes_written Address of area to hold the number of bytes written.
3052 * @param min_xfer Minimum number of bytes to write.
3053 * @param timeout Waiting period to wait for the data to be written (in
3054 * milliseconds), or one of the special values K_NO_WAIT
3055 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003056 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003057 * @retval 0 At least @a min_xfer bytes of data were written.
3058 * @retval -EIO Returned without waiting; zero data bytes were written.
3059 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003062extern int k_pipe_put(struct k_pipe *pipe, void *data,
3063 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003064 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065
3066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003067 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * @param pipe Address of the pipe.
3072 * @param data Address to place the data read from pipe.
3073 * @param bytes_to_read Maximum number of data bytes to read.
3074 * @param bytes_read Address of area to hold the number of bytes read.
3075 * @param min_xfer Minimum number of data bytes to read.
3076 * @param timeout Waiting period to wait for the data to be read (in
3077 * milliseconds), or one of the special values K_NO_WAIT
3078 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003080 * @retval 0 At least @a min_xfer bytes of data were read.
3081 * @retval -EIO Returned without waiting; zero data bytes were read.
3082 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003085extern int k_pipe_get(struct k_pipe *pipe, void *data,
3086 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003087 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088
3089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * This routine writes the data contained in a memory block to @a pipe.
3093 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 * @param block Memory block containing data to send
3098 * @param size Number of data bytes in memory block to send
3099 * @param sem Semaphore to signal upon completion (else NULL)
3100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 */
3103extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3104 size_t size, struct k_sem *sem);
3105
3106/**
Allan Stephensc98da842016-11-11 15:45:03 -05003107 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108 */
3109
Allan Stephensc98da842016-11-11 15:45:03 -05003110/**
3111 * @cond INTERNAL_HIDDEN
3112 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003114struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003116 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003117 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118 char *buffer;
3119 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003120 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003121
Anas Nashif2f203c22016-12-18 06:57:45 -05003122 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123};
3124
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003125#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003126 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127 { \
3128 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003129 .num_blocks = slab_num_blocks, \
3130 .block_size = slab_block_size, \
3131 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132 .free_list = NULL, \
3133 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003134 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 }
3136
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003137#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3138
3139
Peter Mitsis578f9112016-10-07 13:50:31 -04003140/**
Allan Stephensc98da842016-11-11 15:45:03 -05003141 * INTERNAL_HIDDEN @endcond
3142 */
3143
3144/**
3145 * @defgroup mem_slab_apis Memory Slab APIs
3146 * @ingroup kernel_apis
3147 * @{
3148 */
3149
3150/**
Allan Stephensda827222016-11-09 14:23:58 -06003151 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003152 *
Allan Stephensda827222016-11-09 14:23:58 -06003153 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003154 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003155 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3156 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003157 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003158 *
Allan Stephensda827222016-11-09 14:23:58 -06003159 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003161 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003162 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * @param name Name of the memory slab.
3165 * @param slab_block_size Size of each memory block (in bytes).
3166 * @param slab_num_blocks Number memory blocks.
3167 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003168 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003169#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3170 char __noinit __aligned(slab_align) \
3171 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3172 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003173 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003174 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003175 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003177/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003178 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003180 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003181 *
Allan Stephensda827222016-11-09 14:23:58 -06003182 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3183 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3184 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3185 * To ensure that each memory block is similarly aligned to this boundary,
3186 * @a slab_block_size must also be a multiple of N.
3187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * @param slab Address of the memory slab.
3189 * @param buffer Pointer to buffer used for the memory blocks.
3190 * @param block_size Size of each memory block (in bytes).
3191 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003192 *
3193 * @return N/A
3194 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003195extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003196 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003197
3198/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * @param slab Address of the memory slab.
3204 * @param mem Pointer to block address area.
3205 * @param timeout Maximum time to wait for operation to complete
3206 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3207 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003208 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003209 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003211 * @retval -ENOMEM Returned without waiting.
3212 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003213 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003214extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003215 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003216
3217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * This routine releases a previously allocated memory block back to its
3221 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * @param slab Address of the memory slab.
3224 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003225 *
3226 * @return N/A
3227 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003228extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * This routine gets the number of memory blocks that are currently
3234 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003239 */
Kumar Galacc334c72017-04-21 10:55:34 -05003240static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003242 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003243}
3244
Peter Mitsisc001aa82016-10-13 13:53:37 -04003245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * This routine gets the number of memory blocks that are currently
3249 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003254 */
Kumar Galacc334c72017-04-21 10:55:34 -05003255static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003256{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003257 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003258}
3259
Allan Stephensc98da842016-11-11 15:45:03 -05003260/**
3261 * @} end defgroup mem_slab_apis
3262 */
3263
3264/**
3265 * @cond INTERNAL_HIDDEN
3266 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267
Andy Ross73cb9582017-05-09 10:42:39 -07003268struct k_mem_pool_lvl {
3269 union {
3270 u32_t *bits_p;
3271 u32_t bits;
3272 };
3273 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003274};
3275
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003276struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003277 void *buf;
3278 size_t max_sz;
3279 u16_t n_max;
3280 u8_t n_levels;
3281 u8_t max_inline_level;
3282 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003283 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003284};
3285
Andy Ross73cb9582017-05-09 10:42:39 -07003286#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287
Andy Ross73cb9582017-05-09 10:42:39 -07003288#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3289
3290#define _MPOOL_LVLS(maxsz, minsz) \
3291 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3292 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3293 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3294 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3295 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3296 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3297 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3298 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3299 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3300 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3301 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3302 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3303 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3304 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3305 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3306 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3307
3308/* Rounds the needed bits up to integer multiples of u32_t */
3309#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3310 ((((n_max) << (2*(l))) + 31) / 32)
3311
3312/* One word gets stored free unioned with the pointer, otherwise the
3313 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003314 */
Andy Ross73cb9582017-05-09 10:42:39 -07003315#define _MPOOL_LBIT_WORDS(n_max, l) \
3316 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3317 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003318
Andy Ross73cb9582017-05-09 10:42:39 -07003319/* How many bytes for the bitfields of a single level? */
3320#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3321 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3322 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003323
Andy Ross73cb9582017-05-09 10:42:39 -07003324/* Size of the bitmap array that follows the buffer in allocated memory */
3325#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3326 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3327 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3328 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3329 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3330 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3331 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3332 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3333 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3334 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3335 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3336 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3337 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3338 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3339 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3340 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3341 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003342
3343/**
Allan Stephensc98da842016-11-11 15:45:03 -05003344 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003345 */
3346
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003347/**
Allan Stephensc98da842016-11-11 15:45:03 -05003348 * @addtogroup mem_pool_apis
3349 * @{
3350 */
3351
3352/**
3353 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3356 * long. The memory pool allows blocks to be repeatedly partitioned into
3357 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003358 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003359 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003360 * If the pool is to be accessed outside the module where it is defined, it
3361 * can be declared via
3362 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003363 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003366 * @param minsz Size of the smallest blocks in the pool (in bytes).
3367 * @param maxsz Size of the largest blocks in the pool (in bytes).
3368 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003370 */
Andy Ross73cb9582017-05-09 10:42:39 -07003371#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3372 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3373 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3374 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3375 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3376 .buf = _mpool_buf_##name, \
3377 .max_sz = maxsz, \
3378 .n_max = nmax, \
3379 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3380 .levels = _mpool_lvls_##name, \
3381 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003382
Peter Mitsis937042c2016-10-13 13:18:26 -04003383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @param pool Address of the memory pool.
3389 * @param block Pointer to block descriptor for the allocated memory.
3390 * @param size Amount of memory to allocate (in bytes).
3391 * @param timeout Maximum time to wait for operation to complete
3392 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3393 * or K_FOREVER to wait as long as necessary.
3394 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003395 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003397 * @retval -ENOMEM Returned without waiting.
3398 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003399 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003400extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003401 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003402
3403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * This routine releases a previously allocated memory block back to its
3407 * memory pool.
3408 *
3409 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003410 *
3411 * @return N/A
3412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003414
3415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003417 *
Andy Ross73cb9582017-05-09 10:42:39 -07003418 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 *
Andy Ross73cb9582017-05-09 10:42:39 -07003420 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003421 *
3422 * @return N/A
3423 */
Andy Ross73cb9582017-05-09 10:42:39 -07003424static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003425
3426/**
Allan Stephensc98da842016-11-11 15:45:03 -05003427 * @} end addtogroup mem_pool_apis
3428 */
3429
3430/**
3431 * @defgroup heap_apis Heap Memory Pool APIs
3432 * @ingroup kernel_apis
3433 * @{
3434 */
3435
3436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003439 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003440 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003445 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003446extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003447
3448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003449 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003450 *
3451 * This routine provides traditional free() semantics. The memory being
3452 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003453 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003454 * If @a ptr is NULL, no operation is performed.
3455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003457 *
3458 * @return N/A
3459 */
3460extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003461
Allan Stephensc98da842016-11-11 15:45:03 -05003462/**
3463 * @} end defgroup heap_apis
3464 */
3465
Benjamin Walshacc68c12017-01-29 18:57:45 -05003466/* polling API - PRIVATE */
3467
Benjamin Walshb0179862017-02-02 16:39:57 -05003468#ifdef CONFIG_POLL
3469#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3470#else
3471#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3472#endif
3473
Benjamin Walshacc68c12017-01-29 18:57:45 -05003474/* private - implementation data created as needed, per-type */
3475struct _poller {
3476 struct k_thread *thread;
3477};
3478
3479/* private - types bit positions */
3480enum _poll_types_bits {
3481 /* can be used to ignore an event */
3482 _POLL_TYPE_IGNORE,
3483
3484 /* to be signaled by k_poll_signal() */
3485 _POLL_TYPE_SIGNAL,
3486
3487 /* semaphore availability */
3488 _POLL_TYPE_SEM_AVAILABLE,
3489
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003490 /* queue/fifo/lifo data availability */
3491 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003492
3493 _POLL_NUM_TYPES
3494};
3495
3496#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3497
3498/* private - states bit positions */
3499enum _poll_states_bits {
3500 /* default state when creating event */
3501 _POLL_STATE_NOT_READY,
3502
3503 /* there was another poller already on the object */
3504 _POLL_STATE_EADDRINUSE,
3505
3506 /* signaled by k_poll_signal() */
3507 _POLL_STATE_SIGNALED,
3508
3509 /* semaphore is available */
3510 _POLL_STATE_SEM_AVAILABLE,
3511
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003512 /* data is available to read on queue/fifo/lifo */
3513 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003514
3515 _POLL_NUM_STATES
3516};
3517
3518#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3519
3520#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003521 (32 - (0 \
3522 + 8 /* tag */ \
3523 + _POLL_NUM_TYPES \
3524 + _POLL_NUM_STATES \
3525 + 1 /* modes */ \
3526 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003527
3528#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3529#error overflow of 32-bit word in struct k_poll_event
3530#endif
3531
3532/* end of polling API - PRIVATE */
3533
3534
3535/**
3536 * @defgroup poll_apis Async polling APIs
3537 * @ingroup kernel_apis
3538 * @{
3539 */
3540
3541/* Public polling API */
3542
3543/* public - values for k_poll_event.type bitfield */
3544#define K_POLL_TYPE_IGNORE 0
3545#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3546#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003547#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3548#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003549
3550/* public - polling modes */
3551enum k_poll_modes {
3552 /* polling thread does not take ownership of objects when available */
3553 K_POLL_MODE_NOTIFY_ONLY = 0,
3554
3555 K_POLL_NUM_MODES
3556};
3557
3558/* public - values for k_poll_event.state bitfield */
3559#define K_POLL_STATE_NOT_READY 0
3560#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3561#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3562#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003563#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3564#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003565
3566/* public - poll signal object */
3567struct k_poll_signal {
3568 /* PRIVATE - DO NOT TOUCH */
3569 struct k_poll_event *poll_event;
3570
3571 /*
3572 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3573 * user resets it to 0.
3574 */
3575 unsigned int signaled;
3576
3577 /* custom result value passed to k_poll_signal() if needed */
3578 int result;
3579};
3580
3581#define K_POLL_SIGNAL_INITIALIZER() \
3582 { \
3583 .poll_event = NULL, \
3584 .signaled = 0, \
3585 .result = 0, \
3586 }
3587
3588struct k_poll_event {
3589 /* PRIVATE - DO NOT TOUCH */
3590 struct _poller *poller;
3591
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003592 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003593 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003594
Benjamin Walshacc68c12017-01-29 18:57:45 -05003595 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003596 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003597
3598 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003599 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003600
3601 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003602 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003603
3604 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003605 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003606
3607 /* per-type data */
3608 union {
3609 void *obj;
3610 struct k_poll_signal *signal;
3611 struct k_sem *sem;
3612 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003613 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003614 };
3615};
3616
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003617#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003618 { \
3619 .poller = NULL, \
3620 .type = event_type, \
3621 .state = K_POLL_STATE_NOT_READY, \
3622 .mode = event_mode, \
3623 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003624 { .obj = event_obj }, \
3625 }
3626
3627#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3628 event_tag) \
3629 { \
3630 .type = event_type, \
3631 .tag = event_tag, \
3632 .state = K_POLL_STATE_NOT_READY, \
3633 .mode = event_mode, \
3634 .unused = 0, \
3635 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003636 }
3637
3638/**
3639 * @brief Initialize one struct k_poll_event instance
3640 *
3641 * After this routine is called on a poll event, the event it ready to be
3642 * placed in an event array to be passed to k_poll().
3643 *
3644 * @param event The event to initialize.
3645 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3646 * values. Only values that apply to the same object being polled
3647 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3648 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003649 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003650 * @param obj Kernel object or poll signal.
3651 *
3652 * @return N/A
3653 */
3654
Kumar Galacc334c72017-04-21 10:55:34 -05003655extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003656 int mode, void *obj);
3657
3658/**
3659 * @brief Wait for one or many of multiple poll events to occur
3660 *
3661 * This routine allows a thread to wait concurrently for one or many of
3662 * multiple poll events to have occurred. Such events can be a kernel object
3663 * being available, like a semaphore, or a poll signal event.
3664 *
3665 * When an event notifies that a kernel object is available, the kernel object
3666 * is not "given" to the thread calling k_poll(): it merely signals the fact
3667 * that the object was available when the k_poll() call was in effect. Also,
3668 * all threads trying to acquire an object the regular way, i.e. by pending on
3669 * the object, have precedence over the thread polling on the object. This
3670 * means that the polling thread will never get the poll event on an object
3671 * until the object becomes available and its pend queue is empty. For this
3672 * reason, the k_poll() call is more effective when the objects being polled
3673 * only have one thread, the polling thread, trying to acquire them.
3674 *
3675 * Only one thread can be polling for a particular object at a given time. If
3676 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3677 * and returns as soon as it has finished handling the other events. This means
3678 * that k_poll() can return -EADDRINUSE and have the state value of some events
3679 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3680 * parameter is ignored.
3681 *
3682 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3683 * events that were passed to k_poll() and check the state field for the values
3684 * that were expected and take the associated actions.
3685 *
3686 * Before being reused for another call to k_poll(), the user has to reset the
3687 * state field to K_POLL_STATE_NOT_READY.
3688 *
3689 * @param events An array of pointers to events to be polled for.
3690 * @param num_events The number of events in the array.
3691 * @param timeout Waiting period for an event to be ready (in milliseconds),
3692 * or one of the special values K_NO_WAIT and K_FOREVER.
3693 *
3694 * @retval 0 One or more events are ready.
3695 * @retval -EADDRINUSE One or more objects already had a poller.
3696 * @retval -EAGAIN Waiting period timed out.
3697 */
3698
3699extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003700 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003701
3702/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003703 * @brief Initialize a poll signal object.
3704 *
3705 * Ready a poll signal object to be signaled via k_poll_signal().
3706 *
3707 * @param signal A poll signal.
3708 *
3709 * @return N/A
3710 */
3711
3712extern void k_poll_signal_init(struct k_poll_signal *signal);
3713
3714/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003715 * @brief Signal a poll signal object.
3716 *
3717 * This routine makes ready a poll signal, which is basically a poll event of
3718 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3719 * made ready to run. A @a result value can be specified.
3720 *
3721 * The poll signal contains a 'signaled' field that, when set by
3722 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3723 * be reset by the user before being passed again to k_poll() or k_poll() will
3724 * consider it being signaled, and will return immediately.
3725 *
3726 * @param signal A poll signal.
3727 * @param result The value to store in the result field of the signal.
3728 *
3729 * @retval 0 The signal was delivered successfully.
3730 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3731 */
3732
3733extern int k_poll_signal(struct k_poll_signal *signal, int result);
3734
3735/* private internal function */
3736extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003737 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003738
3739/**
3740 * @} end defgroup poll_apis
3741 */
3742
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003743/**
3744 * @brief Make the CPU idle.
3745 *
3746 * This function makes the CPU idle until an event wakes it up.
3747 *
3748 * In a regular system, the idle thread should be the only thread responsible
3749 * for making the CPU idle and triggering any type of power management.
3750 * However, in some more constrained systems, such as a single-threaded system,
3751 * the only thread would be responsible for this if needed.
3752 *
3753 * @return N/A
3754 */
3755extern void k_cpu_idle(void);
3756
3757/**
3758 * @brief Make the CPU idle in an atomic fashion.
3759 *
3760 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3761 * must be done atomically before making the CPU idle.
3762 *
3763 * @param key Interrupt locking key obtained from irq_lock().
3764 *
3765 * @return N/A
3766 */
3767extern void k_cpu_atomic_idle(unsigned int key);
3768
Kumar Galacc334c72017-04-21 10:55:34 -05003769extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003770
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003771#include <arch/cpu.h>
3772
Andrew Boiecdb94d62017-04-18 15:22:05 -07003773#ifdef _ARCH_EXCEPT
3774/* This archtecture has direct support for triggering a CPU exception */
3775#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3776#else
3777
3778#include <misc/printk.h>
3779
3780/* NOTE: This is the implementation for arches that do not implement
3781 * _ARCH_EXCEPT() to generate a real CPU exception.
3782 *
3783 * We won't have a real exception frame to determine the PC value when
3784 * the oops occurred, so print file and line number before we jump into
3785 * the fatal error handler.
3786 */
3787#define _k_except_reason(reason) do { \
3788 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3789 _NanoFatalErrorHandler(reason, &_default_esf); \
3790 CODE_UNREACHABLE; \
3791 } while (0)
3792
3793#endif /* _ARCH__EXCEPT */
3794
3795/**
3796 * @brief Fatally terminate a thread
3797 *
3798 * This should be called when a thread has encountered an unrecoverable
3799 * runtime condition and needs to terminate. What this ultimately
3800 * means is determined by the _fatal_error_handler() implementation, which
3801 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3802 *
3803 * If this is called from ISR context, the default system fatal error handler
3804 * will treat it as an unrecoverable system error, just like k_panic().
3805 */
3806#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3807
3808/**
3809 * @brief Fatally terminate the system
3810 *
3811 * This should be called when the Zephyr kernel has encountered an
3812 * unrecoverable runtime condition and needs to terminate. What this ultimately
3813 * means is determined by the _fatal_error_handler() implementation, which
3814 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3815 */
3816#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3817
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003818/*
3819 * private APIs that are utilized by one or more public APIs
3820 */
3821
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003822#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003823extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003824#else
3825#define _init_static_threads() do { } while ((0))
3826#endif
3827
3828extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003829extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003830
Andrew Boiedc5d9352017-06-02 12:56:47 -07003831/* arch/cpu.h may declare an architecture or platform-specific macro
3832 * for properly declaring stacks, compatible with MMU/MPU constraints if
3833 * enabled
3834 */
3835#ifdef _ARCH_THREAD_STACK_DEFINE
3836#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3837#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3838 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3839#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3840#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
3841#define K_THREAD_STACK_BUFFER(sym) _ARCH_THREAD_STACK_BUFFER(sym)
3842#else
3843/**
3844 * @brief Declare a toplevel thread stack memory region
3845 *
3846 * This declares a region of memory suitable for use as a thread's stack.
3847 *
3848 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3849 * 'noinit' section so that it isn't zeroed at boot
3850 *
3851 * The declared symbol will always be a character array which can be passed to
3852 * k_thread_create, but should otherwise not be manipulated.
3853 *
3854 * It is legal to precede this definition with the 'static' keyword.
3855 *
3856 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3857 * parameter of k_thread_create(), it may not be the same as the
3858 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3859 *
3860 * @param sym Thread stack symbol name
3861 * @param size Size of the stack memory region
3862 */
3863#define K_THREAD_STACK_DEFINE(sym, size) \
3864 char __noinit __aligned(STACK_ALIGN) sym[size]
3865
3866/**
3867 * @brief Declare a toplevel array of thread stack memory regions
3868 *
3869 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3870 * definition for additional details and constraints.
3871 *
3872 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3873 * 'noinit' section so that it isn't zeroed at boot
3874 *
3875 * @param sym Thread stack symbol name
3876 * @param nmemb Number of stacks to declare
3877 * @param size Size of the stack memory region
3878 */
3879
3880#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3881 char __noinit __aligned(STACK_ALIGN) sym[nmemb][size]
3882
3883/**
3884 * @brief Declare an embedded stack memory region
3885 *
3886 * Used for stacks embedded within other data structures. Use is highly
3887 * discouraged but in some cases necessary. For memory protection scenarios,
3888 * it is very important that any RAM preceding this member not be writable
3889 * by threads else a stack overflow will lead to silent corruption. In other
3890 * words, the containing data structure should live in RAM owned by the kernel.
3891 *
3892 * @param sym Thread stack symbol name
3893 * @param size Size of the stack memory region
3894 */
3895#define K_THREAD_STACK_MEMBER(sym, size) \
3896 char __aligned(STACK_ALIGN) sym[size]
3897
3898/**
3899 * @brief Return the size in bytes of a stack memory region
3900 *
3901 * Convenience macro for passing the desired stack size to k_thread_create()
3902 * since the underlying implementation may actually create something larger
3903 * (for instance a guard area).
3904 *
3905 * The value returned here is guaranteed to match the 'size' parameter
3906 * passed to K_THREAD_STACK_DEFINE and related macros.
3907 *
3908 * @param sym Stack memory symbol
3909 * @return Size of the stack
3910 */
3911#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
3912
3913/**
3914 * @brief Get a pointer to the physical stack buffer
3915 *
3916 * Convenience macro to get at the real underlying stack buffer used by
3917 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
3918 * This is really only intended for diagnostic tools which want to examine
3919 * stack memory contents.
3920 *
3921 * @param sym Declared stack symbol name
3922 * @return The buffer itself, a char *
3923 */
3924#define K_THREAD_STACK_BUFFER(sym) sym
3925
3926#endif /* _ARCH_DECLARE_STACK */
3927
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003928#ifdef __cplusplus
3929}
3930#endif
3931
Andrew Boiee004dec2016-11-07 09:01:19 -08003932#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3933/*
3934 * Define new and delete operators.
3935 * At this moment, the operators do nothing since objects are supposed
3936 * to be statically allocated.
3937 */
3938inline void operator delete(void *ptr)
3939{
3940 (void)ptr;
3941}
3942
3943inline void operator delete[](void *ptr)
3944{
3945 (void)ptr;
3946}
3947
3948inline void *operator new(size_t size)
3949{
3950 (void)size;
3951 return NULL;
3952}
3953
3954inline void *operator new[](size_t size)
3955{
3956 (void)size;
3957 return NULL;
3958}
3959
3960/* Placement versions of operator new and delete */
3961inline void operator delete(void *ptr1, void *ptr2)
3962{
3963 (void)ptr1;
3964 (void)ptr2;
3965}
3966
3967inline void operator delete[](void *ptr1, void *ptr2)
3968{
3969 (void)ptr1;
3970 (void)ptr2;
3971}
3972
3973inline void *operator new(size_t size, void *ptr)
3974{
3975 (void)size;
3976 return ptr;
3977}
3978
3979inline void *operator new[](size_t size, void *ptr)
3980{
3981 (void)size;
3982 return ptr;
3983}
3984
3985#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3986
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003987#endif /* !_ASMLANGUAGE */
3988
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003989#endif /* _kernel__h_ */