blob: 4023f48bf21607c0354076eb8a7e5aa8ecd58f21 [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
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300101#define _POLL_EVENT_OBJ_INIT(obj) \
102 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
103#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500104#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300105#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#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
Andrew Boie507852a2017-07-25 18:47:07 -0700350/* Using typedef deliberately here, this is quite intended to be an opaque
351 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
352 *
353 * The purpose of this data type is to clearly distinguish between the
354 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
355 * buffer which composes the stack data actually used by the underlying
356 * thread; they cannot be used interchangably as some arches precede the
357 * stack buffer region with guard areas that trigger a MPU or MMU fault
358 * if written to.
359 *
360 * APIs that want to work with the buffer inside should continue to use
361 * char *.
362 *
363 * Stacks should always be created with K_THREAD_STACK_DEFINE().
364 */
365struct __packed _k_thread_stack_element {
366 char data;
367};
368typedef struct _k_thread_stack_element *k_thread_stack_t;
369
Allan Stephens5eceb852016-11-16 10:16:30 -0500370/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500371 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500373 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500375 * The new thread may be scheduled for immediate execution or a delayed start.
376 * If the newly spawned thread does not have a delayed start the kernel
377 * scheduler may preempt the current thread to allow the new thread to
378 * execute.
379 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700380 * Kernel data structures for bookkeeping and context storage for this thread
381 * will be placed at the beginning of the thread's stack memory region and may
382 * become corrupted if too much of the stack is used. This function has been
383 * deprecated in favor of k_thread_create() to give the user more control on
384 * where these data structures reside.
385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500386 * Thread options are architecture-specific, and can include K_ESSENTIAL,
387 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
388 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400389 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700390 * The stack itself should be declared with K_THREAD_STACK_DEFINE or variant
391 * macros. The stack size parameter should either be a defined constant
392 * also passed to K_THREAD_STACK_DEFINE, or the value of K_THREAD_STACK_SIZEOF.
393 * Do not use regular C sizeof().
394 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400395 * @param stack Pointer to the stack space.
396 * @param stack_size Stack size in bytes.
397 * @param entry Thread entry function.
398 * @param p1 1st entry point parameter.
399 * @param p2 2nd entry point parameter.
400 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500401 * @param prio Thread priority.
402 * @param options Thread options.
403 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500405 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400406 */
Andrew Boie507852a2017-07-25 18:47:07 -0700407extern __deprecated k_tid_t k_thread_spawn(k_thread_stack_t stack,
408 size_t stack_size, k_thread_entry_t entry,
409 void *p1, void *p2, void *p3,
410 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400411
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400412/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700413 * @brief Create a thread.
414 *
415 * This routine initializes a thread, then schedules it for execution.
416 *
417 * The new thread may be scheduled for immediate execution or a delayed start.
418 * If the newly spawned thread does not have a delayed start the kernel
419 * scheduler may preempt the current thread to allow the new thread to
420 * execute.
421 *
422 * Thread options are architecture-specific, and can include K_ESSENTIAL,
423 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
424 * them using "|" (the logical OR operator).
425 *
426 * Historically, users often would use the beginning of the stack memory region
427 * to store the struct k_thread data, although corruption will occur if the
428 * stack overflows this region and stack protection features may not detect this
429 * situation.
430 *
431 * @param new_thread Pointer to uninitialized struct k_thread
432 * @param stack Pointer to the stack space.
433 * @param stack_size Stack size in bytes.
434 * @param entry Thread entry function.
435 * @param p1 1st entry point parameter.
436 * @param p2 2nd entry point parameter.
437 * @param p3 3rd entry point parameter.
438 * @param prio Thread priority.
439 * @param options Thread options.
440 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
441 *
442 * @return ID of new thread.
443 */
Andrew Boie507852a2017-07-25 18:47:07 -0700444extern k_tid_t k_thread_create(struct k_thread *new_thread,
445 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700446 size_t stack_size,
447 void (*entry)(void *, void *, void*),
448 void *p1, void *p2, void *p3,
449 int prio, u32_t options, s32_t delay);
450
451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500452 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400453 *
Allan Stephensc98da842016-11-11 15:45:03 -0500454 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500455 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
459 * @return N/A
460 */
Kumar Galacc334c72017-04-21 10:55:34 -0500461extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462
463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 *
466 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500467 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400468 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400469 * @return N/A
470 */
Kumar Galacc334c72017-04-21 10:55:34 -0500471extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400472
473/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500474 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500476 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400477 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500478 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400479 *
480 * @return N/A
481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400482extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400483
484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500485 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500487 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * If @a thread is not currently sleeping, the routine has no effect.
490 *
491 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400492 *
493 * @return N/A
494 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400495extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400496
497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500498 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500500 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400502extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400503
504/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500505 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500507 * This routine prevents @a thread from executing if it has not yet started
508 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500510 * @param thread ID of thread to cancel.
511 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700512 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500513 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400515extern int k_thread_cancel(k_tid_t thread);
516
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517/**
Allan Stephensc98da842016-11-11 15:45:03 -0500518 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500520 * This routine permanently stops execution of @a thread. The thread is taken
521 * off all kernel queues it is part of (i.e. the ready queue, the timeout
522 * queue, or a kernel object wait queue). However, any kernel resources the
523 * thread might currently own (such as mutexes or memory blocks) are not
524 * released. It is the responsibility of the caller of this routine to ensure
525 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500527 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528 *
529 * @return N/A
530 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400531extern void k_thread_abort(k_tid_t thread);
532
Allan Stephensc98da842016-11-11 15:45:03 -0500533/**
534 * @cond INTERNAL_HIDDEN
535 */
536
Benjamin Walshd211a522016-12-06 11:44:01 -0500537/* timeout has timed out and is not on _timeout_q anymore */
538#define _EXPIRED (-2)
539
540/* timeout is not in use */
541#define _INACTIVE (-1)
542
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400543struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700544 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700545 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400546 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500547 void (*init_entry)(void *, void *, void *);
548 void *init_p1;
549 void *init_p2;
550 void *init_p3;
551 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500552 u32_t init_options;
553 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500554 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500555 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400556};
557
Andrew Boied26cf2d2017-03-30 13:07:02 -0700558#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400559 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500560 prio, options, delay, abort, groups) \
561 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700562 .init_thread = (thread), \
563 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500564 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400565 .init_entry = (void (*)(void *, void *, void *))entry, \
566 .init_p1 = (void *)p1, \
567 .init_p2 = (void *)p2, \
568 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500569 .init_prio = (prio), \
570 .init_options = (options), \
571 .init_delay = (delay), \
572 .init_abort = (abort), \
573 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400574 }
575
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400576/**
Allan Stephensc98da842016-11-11 15:45:03 -0500577 * INTERNAL_HIDDEN @endcond
578 */
579
580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * @brief Statically define and initialize a thread.
582 *
583 * The thread may be scheduled for immediate execution or a delayed start.
584 *
585 * Thread options are architecture-specific, and can include K_ESSENTIAL,
586 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
587 * them using "|" (the logical OR operator).
588 *
589 * The ID of the thread can be accessed using:
590 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500591 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500592 *
593 * @param name Name of the thread.
594 * @param stack_size Stack size in bytes.
595 * @param entry Thread entry function.
596 * @param p1 1st entry point parameter.
597 * @param p2 2nd entry point parameter.
598 * @param p3 3rd entry point parameter.
599 * @param prio Thread priority.
600 * @param options Thread options.
601 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400602 *
603 * @internal It has been observed that the x86 compiler by default aligns
604 * these _static_thread_data structures to 32-byte boundaries, thereby
605 * wasting space. To work around this, force a 4-byte alignment.
606 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500607#define K_THREAD_DEFINE(name, stack_size, \
608 entry, p1, p2, p3, \
609 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700610 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700611 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500612 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500613 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700614 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
615 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500616 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700617 NULL, 0); \
618 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400619
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400620/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500621 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500623 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500625 * @param thread ID of thread whose priority is needed.
626 *
627 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400628 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500629extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630
631/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500632 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500634 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400635 *
636 * Rescheduling can occur immediately depending on the priority @a thread is
637 * set to:
638 *
639 * - If its priority is raised above the priority of the caller of this
640 * function, and the caller is preemptible, @a thread will be scheduled in.
641 *
642 * - If the caller operates on itself, it lowers its priority below that of
643 * other threads in the system, and the caller is preemptible, the thread of
644 * highest priority will be scheduled in.
645 *
646 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
647 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
648 * highest priority.
649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500650 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400651 * @param prio New priority.
652 *
653 * @warning Changing the priority of a thread currently involved in mutex
654 * priority inheritance may result in undefined behavior.
655 *
656 * @return N/A
657 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400658extern void k_thread_priority_set(k_tid_t thread, int prio);
659
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500663 * This routine prevents the kernel scheduler from making @a thread the
664 * current thread. All other internal operations on @a thread are still
665 * performed; for example, any timeout it is waiting on keeps ticking,
666 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500668 * If @a thread is already suspended, the routine has no effect.
669 *
670 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
672 * @return N/A
673 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400674extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675
676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500677 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500679 * This routine allows the kernel scheduler to make @a thread the current
680 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500682 * If @a thread is not currently suspended, the routine has no effect.
683 *
684 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400685 *
686 * @return N/A
687 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400688extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400689
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500691 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500693 * This routine specifies how the scheduler will perform time slicing of
694 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * To enable time slicing, @a slice must be non-zero. The scheduler
697 * ensures that no thread runs for more than the specified time limit
698 * before other threads of that priority are given a chance to execute.
699 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700700 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500702 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400703 * execute. Once the scheduler selects a thread for execution, there is no
704 * minimum guaranteed time the thread will execute before threads of greater or
705 * equal priority are scheduled.
706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500707 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400708 * for execution, this routine has no effect; the thread is immediately
709 * rescheduled after the slice period expires.
710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500711 * To disable timeslicing, set both @a slice and @a prio to zero.
712 *
713 * @param slice Maximum time slice length (in milliseconds).
714 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400715 *
716 * @return N/A
717 */
Kumar Galacc334c72017-04-21 10:55:34 -0500718extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400719
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400720/**
Allan Stephensc98da842016-11-11 15:45:03 -0500721 * @} end defgroup thread_apis
722 */
723
724/**
725 * @addtogroup isr_apis
726 * @{
727 */
728
729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500730 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400731 *
Allan Stephensc98da842016-11-11 15:45:03 -0500732 * This routine allows the caller to customize its actions, depending on
733 * whether it is a thread or an ISR.
734 *
735 * @note Can be called by ISRs.
736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500737 * @return 0 if invoked by a thread.
738 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500740extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400741
Benjamin Walsh445830d2016-11-10 15:54:27 -0500742/**
743 * @brief Determine if code is running in a preemptible thread.
744 *
Allan Stephensc98da842016-11-11 15:45:03 -0500745 * This routine allows the caller to customize its actions, depending on
746 * whether it can be preempted by another thread. The routine returns a 'true'
747 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500748 *
Allan Stephensc98da842016-11-11 15:45:03 -0500749 * - The code is running in a thread, not at ISR.
750 * - The thread's priority is in the preemptible range.
751 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500752 *
Allan Stephensc98da842016-11-11 15:45:03 -0500753 * @note Can be called by ISRs.
754 *
755 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500756 * @return Non-zero if invoked by a preemptible thread.
757 */
758extern int k_is_preempt_thread(void);
759
Allan Stephensc98da842016-11-11 15:45:03 -0500760/**
761 * @} end addtogroup isr_apis
762 */
763
764/**
765 * @addtogroup thread_apis
766 * @{
767 */
768
769/**
770 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500771 *
Allan Stephensc98da842016-11-11 15:45:03 -0500772 * This routine prevents the current thread from being preempted by another
773 * thread by instructing the scheduler to treat it as a cooperative thread.
774 * If the thread subsequently performs an operation that makes it unready,
775 * it will be context switched out in the normal manner. When the thread
776 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500777 *
Allan Stephensc98da842016-11-11 15:45:03 -0500778 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500779 *
Allan Stephensc98da842016-11-11 15:45:03 -0500780 * @note k_sched_lock() and k_sched_unlock() should normally be used
781 * when the operation being performed can be safely interrupted by ISRs.
782 * However, if the amount of processing involved is very small, better
783 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500784 *
785 * @return N/A
786 */
787extern void k_sched_lock(void);
788
Allan Stephensc98da842016-11-11 15:45:03 -0500789/**
790 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500791 *
Allan Stephensc98da842016-11-11 15:45:03 -0500792 * This routine reverses the effect of a previous call to k_sched_lock().
793 * A thread must call the routine once for each time it called k_sched_lock()
794 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500795 *
796 * @return N/A
797 */
798extern void k_sched_unlock(void);
799
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500803 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * Custom data is not used by the kernel itself, and is freely available
806 * for a thread to use as it sees fit. It can be used as a framework
807 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
811 * @return N/A
812 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400813extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814
815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400822extern void *k_thread_custom_data_get(void);
823
824/**
Allan Stephensc98da842016-11-11 15:45:03 -0500825 * @} end addtogroup thread_apis
826 */
827
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400828#include <sys_clock.h>
829
Allan Stephensc2f15a42016-11-17 12:24:22 -0500830/**
831 * @addtogroup clock_apis
832 * @{
833 */
834
835/**
836 * @brief Generate null timeout delay.
837 *
838 * This macro generates a timeout delay that that instructs a kernel API
839 * not to wait if the requested operation cannot be performed immediately.
840 *
841 * @return Timeout delay value.
842 */
843#define K_NO_WAIT 0
844
845/**
846 * @brief Generate timeout delay from milliseconds.
847 *
848 * This macro generates a timeout delay that that instructs a kernel API
849 * to wait up to @a ms milliseconds to perform the requested operation.
850 *
851 * @param ms Duration in milliseconds.
852 *
853 * @return Timeout delay value.
854 */
Johan Hedberg14471692016-11-13 10:52:15 +0200855#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500856
857/**
858 * @brief Generate timeout delay from seconds.
859 *
860 * This macro generates a timeout delay that that instructs a kernel API
861 * to wait up to @a s seconds to perform the requested operation.
862 *
863 * @param s Duration in seconds.
864 *
865 * @return Timeout delay value.
866 */
Johan Hedberg14471692016-11-13 10:52:15 +0200867#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500868
869/**
870 * @brief Generate timeout delay from minutes.
871 *
872 * This macro generates a timeout delay that that instructs a kernel API
873 * to wait up to @a m minutes to perform the requested operation.
874 *
875 * @param m Duration in minutes.
876 *
877 * @return Timeout delay value.
878 */
Johan Hedberg14471692016-11-13 10:52:15 +0200879#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500880
881/**
882 * @brief Generate timeout delay from hours.
883 *
884 * This macro generates a timeout delay that that instructs a kernel API
885 * to wait up to @a h hours to perform the requested operation.
886 *
887 * @param h Duration in hours.
888 *
889 * @return Timeout delay value.
890 */
Johan Hedberg14471692016-11-13 10:52:15 +0200891#define K_HOURS(h) K_MINUTES((h) * 60)
892
Allan Stephensc98da842016-11-11 15:45:03 -0500893/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500894 * @brief Generate infinite timeout delay.
895 *
896 * This macro generates a timeout delay that that instructs a kernel API
897 * to wait as long as necessary to perform the requested operation.
898 *
899 * @return Timeout delay value.
900 */
901#define K_FOREVER (-1)
902
903/**
904 * @} end addtogroup clock_apis
905 */
906
907/**
Allan Stephensc98da842016-11-11 15:45:03 -0500908 * @cond INTERNAL_HIDDEN
909 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400910
Benjamin Walsh62092182016-12-20 14:39:08 -0500911/* kernel clocks */
912
913#if (sys_clock_ticks_per_sec == 1000) || \
914 (sys_clock_ticks_per_sec == 500) || \
915 (sys_clock_ticks_per_sec == 250) || \
916 (sys_clock_ticks_per_sec == 125) || \
917 (sys_clock_ticks_per_sec == 100) || \
918 (sys_clock_ticks_per_sec == 50) || \
919 (sys_clock_ticks_per_sec == 25) || \
920 (sys_clock_ticks_per_sec == 20) || \
921 (sys_clock_ticks_per_sec == 10) || \
922 (sys_clock_ticks_per_sec == 1)
923
924 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
925#else
926 /* yields horrible 64-bit math on many architectures: try to avoid */
927 #define _NON_OPTIMIZED_TICKS_PER_SEC
928#endif
929
930#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500931extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500932#else
Kumar Galacc334c72017-04-21 10:55:34 -0500933static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500934{
Kumar Galacc334c72017-04-21 10:55:34 -0500935 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500936}
937#endif
938
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500939/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800940#ifdef CONFIG_TICKLESS_KERNEL
941#define _TICK_ALIGN 0
942#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500943#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800944#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500945
Kumar Galacc334c72017-04-21 10:55:34 -0500946static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400947{
Benjamin Walsh62092182016-12-20 14:39:08 -0500948#ifdef CONFIG_SYS_CLOCK_EXISTS
949
950#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500951 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400952#else
Kumar Galacc334c72017-04-21 10:55:34 -0500953 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500954#endif
955
956#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400957 __ASSERT(ticks == 0, "");
958 return 0;
959#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400960}
961
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400962struct k_timer {
963 /*
964 * _timeout structure must be first here if we want to use
965 * dynamic timer allocation. timeout.node is used in the double-linked
966 * list of free timers
967 */
968 struct _timeout timeout;
969
Allan Stephens45bfa372016-10-12 12:39:42 -0500970 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971 _wait_q_t wait_q;
972
973 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500974 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400975
976 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500977 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400978
979 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500980 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400981
Allan Stephens45bfa372016-10-12 12:39:42 -0500982 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500983 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400984
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500985 /* user-specific data, also used to support legacy features */
986 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400987
Anas Nashif2f203c22016-12-18 06:57:45 -0500988 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400989};
990
Andrew Boie65a9d2a2017-06-27 10:51:23 -0700991#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400992 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500993 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500994 .timeout.wait_q = NULL, \
995 .timeout.thread = NULL, \
996 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400997 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500998 .expiry_fn = expiry, \
999 .stop_fn = stop, \
1000 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001001 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001002 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001003 }
1004
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001005#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007/**
Allan Stephensc98da842016-11-11 15:45:03 -05001008 * INTERNAL_HIDDEN @endcond
1009 */
1010
1011/**
1012 * @defgroup timer_apis Timer APIs
1013 * @ingroup kernel_apis
1014 * @{
1015 */
1016
1017/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001018 * @typedef k_timer_expiry_t
1019 * @brief Timer expiry function type.
1020 *
1021 * A timer's expiry function is executed by the system clock interrupt handler
1022 * each time the timer expires. The expiry function is optional, and is only
1023 * invoked if the timer has been initialized with one.
1024 *
1025 * @param timer Address of timer.
1026 *
1027 * @return N/A
1028 */
1029typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1030
1031/**
1032 * @typedef k_timer_stop_t
1033 * @brief Timer stop function type.
1034 *
1035 * A timer's stop function is executed if the timer is stopped prematurely.
1036 * The function runs in the context of the thread that stops the timer.
1037 * The stop function is optional, and is only invoked if the timer has been
1038 * initialized with one.
1039 *
1040 * @param timer Address of timer.
1041 *
1042 * @return N/A
1043 */
1044typedef void (*k_timer_stop_t)(struct k_timer *timer);
1045
1046/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001049 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001051 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 *
1053 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * @param expiry_fn Function to invoke each time the timer expires.
1055 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001056 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001057#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001058 struct k_timer name \
1059 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001060 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001061
Allan Stephens45bfa372016-10-12 12:39:42 -05001062/**
1063 * @brief Initialize a timer.
1064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001066 *
1067 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001068 * @param expiry_fn Function to invoke each time the timer expires.
1069 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001070 *
1071 * @return N/A
1072 */
1073extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001074 k_timer_expiry_t expiry_fn,
1075 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001076
Allan Stephens45bfa372016-10-12 12:39:42 -05001077/**
1078 * @brief Start a timer.
1079 *
1080 * This routine starts a timer, and resets its status to zero. The timer
1081 * begins counting down using the specified duration and period values.
1082 *
1083 * Attempting to start a timer that is already running is permitted.
1084 * The timer's status is reset to zero and the timer begins counting down
1085 * using the new duration and period values.
1086 *
1087 * @param timer Address of timer.
1088 * @param duration Initial timer duration (in milliseconds).
1089 * @param period Timer period (in milliseconds).
1090 *
1091 * @return N/A
1092 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001093extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001094 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001095
1096/**
1097 * @brief Stop a timer.
1098 *
1099 * This routine stops a running timer prematurely. The timer's stop function,
1100 * if one exists, is invoked by the caller.
1101 *
1102 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001104 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001105 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1106 * if @a k_timer_stop is to be called from ISRs.
1107 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001108 * @param timer Address of timer.
1109 *
1110 * @return N/A
1111 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001113
1114/**
1115 * @brief Read timer status.
1116 *
1117 * This routine reads the timer's status, which indicates the number of times
1118 * it has expired since its status was last read.
1119 *
1120 * Calling this routine resets the timer's status to zero.
1121 *
1122 * @param timer Address of timer.
1123 *
1124 * @return Timer status.
1125 */
Kumar Galacc334c72017-04-21 10:55:34 -05001126extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001127
1128/**
1129 * @brief Synchronize thread to timer expiration.
1130 *
1131 * This routine blocks the calling thread until the timer's status is non-zero
1132 * (indicating that it has expired at least once since it was last examined)
1133 * or the timer is stopped. If the timer status is already non-zero,
1134 * or the timer is already stopped, the caller continues without waiting.
1135 *
1136 * Calling this routine resets the timer's status to zero.
1137 *
1138 * This routine must not be used by interrupt handlers, since they are not
1139 * allowed to block.
1140 *
1141 * @param timer Address of timer.
1142 *
1143 * @return Timer status.
1144 */
Kumar Galacc334c72017-04-21 10:55:34 -05001145extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001146
1147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001148 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001149 *
1150 * This routine computes the (approximate) time remaining before a running
1151 * timer next expires. If the timer is not running, it returns zero.
1152 *
1153 * @param timer Address of timer.
1154 *
1155 * @return Remaining time (in milliseconds).
1156 */
Kumar Galacc334c72017-04-21 10:55:34 -05001157static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001158{
1159 return _timeout_remaining_get(&timer->timeout);
1160}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001161
Allan Stephensc98da842016-11-11 15:45:03 -05001162/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001163 * @brief Associate user-specific data with a timer.
1164 *
1165 * This routine records the @a user_data with the @a timer, to be retrieved
1166 * later.
1167 *
1168 * It can be used e.g. in a timer handler shared across multiple subsystems to
1169 * retrieve data specific to the subsystem this timer is associated with.
1170 *
1171 * @param timer Address of timer.
1172 * @param user_data User data to associate with the timer.
1173 *
1174 * @return N/A
1175 */
1176static inline void k_timer_user_data_set(struct k_timer *timer,
1177 void *user_data)
1178{
1179 timer->user_data = user_data;
1180}
1181
1182/**
1183 * @brief Retrieve the user-specific data from a timer.
1184 *
1185 * @param timer Address of timer.
1186 *
1187 * @return The user data.
1188 */
1189static inline void *k_timer_user_data_get(struct k_timer *timer)
1190{
1191 return timer->user_data;
1192}
1193
1194/**
Allan Stephensc98da842016-11-11 15:45:03 -05001195 * @} end defgroup timer_apis
1196 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001197
Allan Stephensc98da842016-11-11 15:45:03 -05001198/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001199 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001200 * @{
1201 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001202
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001204 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * This routine returns the elapsed time since the system booted,
1207 * in milliseconds.
1208 *
1209 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001210 */
Kumar Galacc334c72017-04-21 10:55:34 -05001211extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001212
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001213#ifdef CONFIG_TICKLESS_KERNEL
1214/**
1215 * @brief Enable clock always on in tickless kernel
1216 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001217 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001218 * there are no timer events programmed in tickless kernel
1219 * scheduling. This is necessary if the clock is used to track
1220 * passage of time.
1221 *
1222 * @retval prev_status Previous status of always on flag
1223 */
1224static inline int k_enable_sys_clock_always_on(void)
1225{
1226 int prev_status = _sys_clock_always_on;
1227
1228 _sys_clock_always_on = 1;
1229 _enable_sys_clock();
1230
1231 return prev_status;
1232}
1233
1234/**
1235 * @brief Disable clock always on in tickless kernel
1236 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001237 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001238 * there are no timer events programmed in tickless kernel
1239 * scheduling. To save power, this routine should be called
1240 * immediately when clock is not used to track time.
1241 */
1242static inline void k_disable_sys_clock_always_on(void)
1243{
1244 _sys_clock_always_on = 0;
1245}
1246#else
1247#define k_enable_sys_clock_always_on() do { } while ((0))
1248#define k_disable_sys_clock_always_on() do { } while ((0))
1249#endif
1250
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001251/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254 * This routine returns the lower 32-bits of the elapsed time since the system
1255 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001257 * This routine can be more efficient than k_uptime_get(), as it reduces the
1258 * need for interrupt locking and 64-bit math. However, the 32-bit result
1259 * cannot hold a system uptime time larger than approximately 50 days, so the
1260 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001263 */
Kumar Galacc334c72017-04-21 10:55:34 -05001264extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001265
1266/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001269 * This routine computes the elapsed time between the current system uptime
1270 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * @param reftime Pointer to a reference time, which is updated to the current
1273 * uptime upon return.
1274 *
1275 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001276 */
Kumar Galacc334c72017-04-21 10:55:34 -05001277extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001278
1279/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001280 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * This routine computes the elapsed time between the current system uptime
1283 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001285 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1286 * need for interrupt locking and 64-bit math. However, the 32-bit result
1287 * cannot hold an elapsed time larger than approximately 50 days, so the
1288 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * @param reftime Pointer to a reference time, which is updated to the current
1291 * uptime upon return.
1292 *
1293 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001294 */
Kumar Galacc334c72017-04-21 10:55:34 -05001295extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001296
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001300 * This routine returns the current time, as measured by the system's hardware
1301 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001303 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001305#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001306
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001307/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001308 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001309 */
1310
Allan Stephensc98da842016-11-11 15:45:03 -05001311/**
1312 * @cond INTERNAL_HIDDEN
1313 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001314
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001315struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001316 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001317 union {
1318 _wait_q_t wait_q;
1319
1320 _POLL_EVENT;
1321 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001322
1323 _OBJECT_TRACING_NEXT_PTR(k_queue);
1324};
1325
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001326#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001327 { \
1328 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1329 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001330 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001331 _OBJECT_TRACING_INIT \
1332 }
1333
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001334#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1335
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001336/**
1337 * INTERNAL_HIDDEN @endcond
1338 */
1339
1340/**
1341 * @defgroup queue_apis Queue APIs
1342 * @ingroup kernel_apis
1343 * @{
1344 */
1345
1346/**
1347 * @brief Initialize a queue.
1348 *
1349 * This routine initializes a queue object, prior to its first use.
1350 *
1351 * @param queue Address of the queue.
1352 *
1353 * @return N/A
1354 */
1355extern void k_queue_init(struct k_queue *queue);
1356
1357/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001358 * @brief Cancel waiting on a queue.
1359 *
1360 * This routine causes first thread pending on @a queue, if any, to
1361 * return from k_queue_get() call with NULL value (as if timeout expired).
1362 *
1363 * @note Can be called by ISRs.
1364 *
1365 * @param queue Address of the queue.
1366 *
1367 * @return N/A
1368 */
1369extern void k_queue_cancel_wait(struct k_queue *queue);
1370
1371/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001372 * @brief Append an element to the end of a queue.
1373 *
1374 * This routine appends a data item to @a queue. A queue data item must be
1375 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1376 * reserved for the kernel's use.
1377 *
1378 * @note Can be called by ISRs.
1379 *
1380 * @param queue Address of the queue.
1381 * @param data Address of the data item.
1382 *
1383 * @return N/A
1384 */
1385extern void k_queue_append(struct k_queue *queue, void *data);
1386
1387/**
1388 * @brief Prepend an element to a queue.
1389 *
1390 * This routine prepends a data item to @a queue. A queue data item must be
1391 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1392 * reserved for the kernel's use.
1393 *
1394 * @note Can be called by ISRs.
1395 *
1396 * @param queue Address of the queue.
1397 * @param data Address of the data item.
1398 *
1399 * @return N/A
1400 */
1401extern void k_queue_prepend(struct k_queue *queue, void *data);
1402
1403/**
1404 * @brief Inserts an element to a queue.
1405 *
1406 * This routine inserts a data item to @a queue after previous item. A queue
1407 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1408 * item are reserved for the kernel's use.
1409 *
1410 * @note Can be called by ISRs.
1411 *
1412 * @param queue Address of the queue.
1413 * @param prev Address of the previous data item.
1414 * @param data Address of the data item.
1415 *
1416 * @return N/A
1417 */
1418extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1419
1420/**
1421 * @brief Atomically append a list of elements to a queue.
1422 *
1423 * This routine adds a list of data items to @a queue in one operation.
1424 * The data items must be in a singly-linked list, with the first 32 bits
1425 * in each data item pointing to the next data item; the list must be
1426 * NULL-terminated.
1427 *
1428 * @note Can be called by ISRs.
1429 *
1430 * @param queue Address of the queue.
1431 * @param head Pointer to first node in singly-linked list.
1432 * @param tail Pointer to last node in singly-linked list.
1433 *
1434 * @return N/A
1435 */
1436extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1437
1438/**
1439 * @brief Atomically add a list of elements to a queue.
1440 *
1441 * This routine adds a list of data items to @a queue in one operation.
1442 * The data items must be in a singly-linked list implemented using a
1443 * sys_slist_t object. Upon completion, the original list is empty.
1444 *
1445 * @note Can be called by ISRs.
1446 *
1447 * @param queue Address of the queue.
1448 * @param list Pointer to sys_slist_t object.
1449 *
1450 * @return N/A
1451 */
1452extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1453
1454/**
1455 * @brief Get an element from a queue.
1456 *
1457 * This routine removes first data item from @a queue. The first 32 bits of the
1458 * data item are reserved for the kernel's use.
1459 *
1460 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1461 *
1462 * @param queue Address of the queue.
1463 * @param timeout Waiting period to obtain a data item (in milliseconds),
1464 * or one of the special values K_NO_WAIT and K_FOREVER.
1465 *
1466 * @return Address of the data item if successful; NULL if returned
1467 * without waiting, or waiting period timed out.
1468 */
Kumar Galacc334c72017-04-21 10:55:34 -05001469extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001470
1471/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001472 * @brief Remove an element from a queue.
1473 *
1474 * This routine removes data item from @a queue. The first 32 bits of the
1475 * data item are reserved for the kernel's use. Removing elements from k_queue
1476 * rely on sys_slist_find_and_remove which is not a constant time operation.
1477 *
1478 * @note Can be called by ISRs
1479 *
1480 * @param queue Address of the queue.
1481 * @param data Address of the data item.
1482 *
1483 * @return true if data item was removed
1484 */
1485static inline bool k_queue_remove(struct k_queue *queue, void *data)
1486{
1487 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1488}
1489
1490/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001491 * @brief Query a queue to see if it has data available.
1492 *
1493 * Note that the data might be already gone by the time this function returns
1494 * if other threads are also trying to read from the queue.
1495 *
1496 * @note Can be called by ISRs.
1497 *
1498 * @param queue Address of the queue.
1499 *
1500 * @return Non-zero if the queue is empty.
1501 * @return 0 if data is available.
1502 */
1503static inline int k_queue_is_empty(struct k_queue *queue)
1504{
1505 return (int)sys_slist_is_empty(&queue->data_q);
1506}
1507
1508/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001509 * @brief Peek element at the head of queue.
1510 *
1511 * Return element from the head of queue without removing it.
1512 *
1513 * @param queue Address of the queue.
1514 *
1515 * @return Head element, or NULL if queue is empty.
1516 */
1517static inline void *k_queue_peek_head(struct k_queue *queue)
1518{
1519 return sys_slist_peek_head(&queue->data_q);
1520}
1521
1522/**
1523 * @brief Peek element at the tail of queue.
1524 *
1525 * Return element from the tail of queue without removing it.
1526 *
1527 * @param queue Address of the queue.
1528 *
1529 * @return Tail element, or NULL if queue is empty.
1530 */
1531static inline void *k_queue_peek_tail(struct k_queue *queue)
1532{
1533 return sys_slist_peek_tail(&queue->data_q);
1534}
1535
1536/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001537 * @brief Statically define and initialize a queue.
1538 *
1539 * The queue can be accessed outside the module where it is defined using:
1540 *
1541 * @code extern struct k_queue <name>; @endcode
1542 *
1543 * @param name Name of the queue.
1544 */
1545#define K_QUEUE_DEFINE(name) \
1546 struct k_queue name \
1547 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001548 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001549
1550/**
1551 * @} end defgroup queue_apis
1552 */
1553
1554/**
1555 * @cond INTERNAL_HIDDEN
1556 */
1557
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001558struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001559 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001560};
1561
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001562#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001563 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001564 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001565 }
1566
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001567#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1568
Allan Stephensc98da842016-11-11 15:45:03 -05001569/**
1570 * INTERNAL_HIDDEN @endcond
1571 */
1572
1573/**
1574 * @defgroup fifo_apis Fifo APIs
1575 * @ingroup kernel_apis
1576 * @{
1577 */
1578
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001579/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001580 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001582 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001584 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001585 *
1586 * @return N/A
1587 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001588#define k_fifo_init(fifo) \
1589 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001590
1591/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001592 * @brief Cancel waiting on a fifo.
1593 *
1594 * This routine causes first thread pending on @a fifo, if any, to
1595 * return from k_fifo_get() call with NULL value (as if timeout
1596 * expired).
1597 *
1598 * @note Can be called by ISRs.
1599 *
1600 * @param fifo Address of the fifo.
1601 *
1602 * @return N/A
1603 */
1604#define k_fifo_cancel_wait(fifo) \
1605 k_queue_cancel_wait((struct k_queue *) fifo)
1606
1607/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001610 * This routine adds a data item to @a fifo. A fifo data item must be
1611 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1612 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @note Can be called by ISRs.
1615 *
1616 * @param fifo Address of the fifo.
1617 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001618 *
1619 * @return N/A
1620 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001621#define k_fifo_put(fifo, data) \
1622 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001623
1624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001627 * This routine adds a list of data items to @a fifo in one operation.
1628 * The data items must be in a singly-linked list, with the first 32 bits
1629 * each data item pointing to the next data item; the list must be
1630 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001632 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001634 * @param fifo Address of the fifo.
1635 * @param head Pointer to first node in singly-linked list.
1636 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001637 *
1638 * @return N/A
1639 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001640#define k_fifo_put_list(fifo, head, tail) \
1641 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001642
1643/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001646 * This routine adds a list of data items to @a fifo in one operation.
1647 * The data items must be in a singly-linked list implemented using a
1648 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001649 * and must be re-initialized via sys_slist_init().
1650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * @note Can be called by ISRs.
1652 *
1653 * @param fifo Address of the fifo.
1654 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001655 *
1656 * @return N/A
1657 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001658#define k_fifo_put_slist(fifo, list) \
1659 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001660
1661/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001662 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001664 * This routine removes a data item from @a fifo in a "first in, first out"
1665 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001667 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1668 *
1669 * @param fifo Address of the fifo.
1670 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001671 * or one of the special values K_NO_WAIT and K_FOREVER.
1672 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001673 * @return Address of the data item if successful; NULL if returned
1674 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001675 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001676#define k_fifo_get(fifo, timeout) \
1677 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001678
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001679/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001680 * @brief Query a fifo to see if it has data available.
1681 *
1682 * Note that the data might be already gone by the time this function returns
1683 * if other threads is also trying to read from the fifo.
1684 *
1685 * @note Can be called by ISRs.
1686 *
1687 * @param fifo Address of the fifo.
1688 *
1689 * @return Non-zero if the fifo is empty.
1690 * @return 0 if data is available.
1691 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001692#define k_fifo_is_empty(fifo) \
1693 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001694
1695/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001696 * @brief Peek element at the head of fifo.
1697 *
1698 * Return element from the head of fifo without removing it. A usecase
1699 * for this is if elements of the fifo are themselves containers. Then
1700 * on each iteration of processing, a head container will be peeked,
1701 * and some data processed out of it, and only if the container is empty,
1702 * it will be completely remove from the fifo.
1703 *
1704 * @param fifo Address of the fifo.
1705 *
1706 * @return Head element, or NULL if the fifo is empty.
1707 */
1708#define k_fifo_peek_head(fifo) \
1709 k_queue_peek_head((struct k_queue *) fifo)
1710
1711/**
1712 * @brief Peek element at the tail of fifo.
1713 *
1714 * Return element from the tail of fifo (without removing it). A usecase
1715 * for this is if elements of the fifo are themselves containers. Then
1716 * it may be useful to add more data to the last container in fifo.
1717 *
1718 * @param fifo Address of the fifo.
1719 *
1720 * @return Tail element, or NULL if fifo is empty.
1721 */
1722#define k_fifo_peek_tail(fifo) \
1723 k_queue_peek_tail((struct k_queue *) fifo)
1724
1725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001730 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001735 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001736 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001737 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001738
Allan Stephensc98da842016-11-11 15:45:03 -05001739/**
1740 * @} end defgroup fifo_apis
1741 */
1742
1743/**
1744 * @cond INTERNAL_HIDDEN
1745 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746
1747struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001748 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001749};
1750
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001751#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001752 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001753 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001754 }
1755
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001756#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1757
Allan Stephensc98da842016-11-11 15:45:03 -05001758/**
1759 * INTERNAL_HIDDEN @endcond
1760 */
1761
1762/**
1763 * @defgroup lifo_apis Lifo APIs
1764 * @ingroup kernel_apis
1765 * @{
1766 */
1767
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774 *
1775 * @return N/A
1776 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001777#define k_lifo_init(lifo) \
1778 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779
1780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001783 * This routine adds a data item to @a lifo. A lifo data item must be
1784 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1785 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001787 * @note Can be called by ISRs.
1788 *
1789 * @param lifo Address of the lifo.
1790 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001791 *
1792 * @return N/A
1793 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001794#define k_lifo_put(lifo, data) \
1795 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796
1797/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * This routine removes a data item from @a lifo in a "last in, first out"
1801 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001803 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1804 *
1805 * @param lifo Address of the lifo.
1806 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 * or one of the special values K_NO_WAIT and K_FOREVER.
1808 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001809 * @return Address of the data item if successful; NULL if returned
1810 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001812#define k_lifo_get(lifo, timeout) \
1813 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001814
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001818 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001820 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001823 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001824#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001825 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001826 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001827 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001828
Allan Stephensc98da842016-11-11 15:45:03 -05001829/**
1830 * @} end defgroup lifo_apis
1831 */
1832
1833/**
1834 * @cond INTERNAL_HIDDEN
1835 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001836
1837struct k_stack {
1838 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001839 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001840
Anas Nashif2f203c22016-12-18 06:57:45 -05001841 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001842};
1843
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001844#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001845 { \
1846 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1847 .base = stack_buffer, \
1848 .next = stack_buffer, \
1849 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001850 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001851 }
1852
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001853#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1854
Allan Stephensc98da842016-11-11 15:45:03 -05001855/**
1856 * INTERNAL_HIDDEN @endcond
1857 */
1858
1859/**
1860 * @defgroup stack_apis Stack APIs
1861 * @ingroup kernel_apis
1862 * @{
1863 */
1864
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001865/**
1866 * @brief Initialize a stack.
1867 *
1868 * This routine initializes a stack object, prior to its first use.
1869 *
1870 * @param stack Address of the stack.
1871 * @param buffer Address of array used to hold stacked values.
1872 * @param num_entries Maximum number of values that can be stacked.
1873 *
1874 * @return N/A
1875 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001876extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001877 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878
1879/**
1880 * @brief Push an element onto a stack.
1881 *
1882 * This routine adds a 32-bit value @a data to @a stack.
1883 *
1884 * @note Can be called by ISRs.
1885 *
1886 * @param stack Address of the stack.
1887 * @param data Value to push onto the stack.
1888 *
1889 * @return N/A
1890 */
Kumar Galacc334c72017-04-21 10:55:34 -05001891extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892
1893/**
1894 * @brief Pop an element from a stack.
1895 *
1896 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1897 * manner and stores the value in @a data.
1898 *
1899 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1900 *
1901 * @param stack Address of the stack.
1902 * @param data Address of area to hold the value popped from the stack.
1903 * @param timeout Waiting period to obtain a value (in milliseconds),
1904 * or one of the special values K_NO_WAIT and K_FOREVER.
1905 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001906 * @retval 0 Element popped from stack.
1907 * @retval -EBUSY Returned without waiting.
1908 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001909 */
Kumar Galacc334c72017-04-21 10:55:34 -05001910extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001915 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001916 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001917 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * @param name Name of the stack.
1920 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001921 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001922#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001923 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001924 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001925 struct k_stack name \
1926 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001927 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001928 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930/**
Allan Stephensc98da842016-11-11 15:45:03 -05001931 * @} end defgroup stack_apis
1932 */
1933
Allan Stephens6bba9b02016-11-16 14:56:54 -05001934struct k_work;
1935
Allan Stephensc98da842016-11-11 15:45:03 -05001936/**
1937 * @defgroup workqueue_apis Workqueue Thread APIs
1938 * @ingroup kernel_apis
1939 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940 */
1941
Allan Stephens6bba9b02016-11-16 14:56:54 -05001942/**
1943 * @typedef k_work_handler_t
1944 * @brief Work item handler function type.
1945 *
1946 * A work item's handler function is executed by a workqueue's thread
1947 * when the work item is processed by the workqueue.
1948 *
1949 * @param work Address of the work item.
1950 *
1951 * @return N/A
1952 */
1953typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954
1955/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001956 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001957 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001958
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03001960 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001961 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001962};
1963
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001964enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001965 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001966};
1967
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001968struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03001969 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001970 k_work_handler_t handler;
1971 atomic_t flags[1];
1972};
1973
Allan Stephens6bba9b02016-11-16 14:56:54 -05001974struct k_delayed_work {
1975 struct k_work work;
1976 struct _timeout timeout;
1977 struct k_work_q *work_q;
1978};
1979
1980extern struct k_work_q k_sys_work_q;
1981
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001982/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001983 * INTERNAL_HIDDEN @endcond
1984 */
1985
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001986#define _K_WORK_INITIALIZER(work_handler) \
1987 { \
1988 ._reserved = NULL, \
1989 .handler = work_handler, \
1990 .flags = { 0 } \
1991 }
1992
1993#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
1994
Allan Stephens6bba9b02016-11-16 14:56:54 -05001995/**
1996 * @brief Initialize a statically-defined work item.
1997 *
1998 * This macro can be used to initialize a statically-defined workqueue work
1999 * item, prior to its first use. For example,
2000 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002001 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002002 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002003 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002004 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002005 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002006#define K_WORK_DEFINE(work, work_handler) \
2007 struct k_work work \
2008 __in_section(_k_work, static, work) = \
2009 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002010
2011/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002012 * @brief Initialize a work item.
2013 *
2014 * This routine initializes a workqueue work item, prior to its first use.
2015 *
2016 * @param work Address of work item.
2017 * @param handler Function to invoke each time work item is processed.
2018 *
2019 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002020 */
2021static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2022{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002023 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002024 work->handler = handler;
2025}
2026
2027/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002028 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002029 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002030 * This routine submits work item @a work to be processed by workqueue
2031 * @a work_q. If the work item is already pending in the workqueue's queue
2032 * as a result of an earlier submission, this routine has no effect on the
2033 * work item. If the work item has already been processed, or is currently
2034 * being processed, its work is considered complete and the work item can be
2035 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002036 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002037 * @warning
2038 * A submitted work item must not be modified until it has been processed
2039 * by the workqueue.
2040 *
2041 * @note Can be called by ISRs.
2042 *
2043 * @param work_q Address of workqueue.
2044 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002045 *
2046 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047 */
2048static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2049 struct k_work *work)
2050{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002051 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002052 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053 }
2054}
2055
2056/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002057 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002059 * This routine indicates if work item @a work is pending in a workqueue's
2060 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002062 * @note Can be called by ISRs.
2063 *
2064 * @param work Address of work item.
2065 *
2066 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002067 */
2068static inline int k_work_pending(struct k_work *work)
2069{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002070 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002071}
2072
2073/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002074 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002076 * This routine starts workqueue @a work_q. The workqueue spawns its work
2077 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002079 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002080 * @param stack Pointer to work queue thread's stack space, as defined by
2081 * K_THREAD_STACK_DEFINE()
2082 * @param stack_size Size of the work queue thread's stack (in bytes), which
2083 * should either be the same constant passed to
2084 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002085 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
2087 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002088 */
Andrew Boie507852a2017-07-25 18:47:07 -07002089extern void k_work_q_start(struct k_work_q *work_q,
2090 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002091 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002092
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002093/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002094 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002095 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002096 * This routine initializes a workqueue delayed work item, prior to
2097 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002099 * @param work Address of delayed work item.
2100 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002101 *
2102 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002103 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002104extern void k_delayed_work_init(struct k_delayed_work *work,
2105 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106
2107/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002108 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002109 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002110 * This routine schedules work item @a work to be processed by workqueue
2111 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002112 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002113 * Only when the countdown completes is the work item actually submitted to
2114 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002116 * Submitting a previously submitted delayed work item that is still
2117 * counting down cancels the existing submission and restarts the countdown
2118 * using the new delay. If the work item is currently pending on the
2119 * workqueue's queue because the countdown has completed it is too late to
2120 * resubmit the item, and resubmission fails without impacting the work item.
2121 * If the work item has already been processed, or is currently being processed,
2122 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002123 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002124 * @warning
2125 * A delayed work item must not be modified until it has been processed
2126 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002128 * @note Can be called by ISRs.
2129 *
2130 * @param work_q Address of workqueue.
2131 * @param work Address of delayed work item.
2132 * @param delay Delay before submitting the work item (in milliseconds).
2133 *
2134 * @retval 0 Work item countdown started.
2135 * @retval -EINPROGRESS Work item is already pending.
2136 * @retval -EINVAL Work item is being processed or has completed its work.
2137 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002138 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002139extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2140 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002141 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002142
2143/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002144 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002145 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002146 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002147 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002150 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002151 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002152 * @param work Address of delayed work item.
2153 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002154 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002155 * @retval -EINPROGRESS Work item is already pending.
2156 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002158extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161 * @brief Submit a work item to the system workqueue.
2162 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002163 * This routine submits work item @a work to be processed by the system
2164 * workqueue. If the work item is already pending in the workqueue's queue
2165 * as a result of an earlier submission, this routine has no effect on the
2166 * work item. If the work item has already been processed, or is currently
2167 * being processed, its work is considered complete and the work item can be
2168 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002169 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002170 * @warning
2171 * Work items submitted to the system workqueue should avoid using handlers
2172 * that block or yield since this may prevent the system workqueue from
2173 * processing other work items in a timely manner.
2174 *
2175 * @note Can be called by ISRs.
2176 *
2177 * @param work Address of work item.
2178 *
2179 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180 */
2181static inline void k_work_submit(struct k_work *work)
2182{
2183 k_work_submit_to_queue(&k_sys_work_q, work);
2184}
2185
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002187 * @brief Submit a delayed work item to the system workqueue.
2188 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189 * This routine schedules work item @a work to be processed by the system
2190 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002191 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 * Only when the countdown completes is the work item actually submitted to
2193 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002194 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002195 * Submitting a previously submitted delayed work item that is still
2196 * counting down cancels the existing submission and restarts the countdown
2197 * using the new delay. If the work item is currently pending on the
2198 * workqueue's queue because the countdown has completed it is too late to
2199 * resubmit the item, and resubmission fails without impacting the work item.
2200 * If the work item has already been processed, or is currently being processed,
2201 * its work is considered complete and the work item can be resubmitted.
2202 *
2203 * @warning
2204 * Work items submitted to the system workqueue should avoid using handlers
2205 * that block or yield since this may prevent the system workqueue from
2206 * processing other work items in a timely manner.
2207 *
2208 * @note Can be called by ISRs.
2209 *
2210 * @param work Address of delayed work item.
2211 * @param delay Delay before submitting the work item (in milliseconds).
2212 *
2213 * @retval 0 Work item countdown started.
2214 * @retval -EINPROGRESS Work item is already pending.
2215 * @retval -EINVAL Work item is being processed or has completed its work.
2216 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217 */
2218static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002219 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002221 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222}
2223
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002225 * @brief Get time remaining before a delayed work gets scheduled.
2226 *
2227 * This routine computes the (approximate) time remaining before a
2228 * delayed work gets executed. If the delayed work is not waiting to be
2229 * schedules, it returns zero.
2230 *
2231 * @param work Delayed work item.
2232 *
2233 * @return Remaining time (in milliseconds).
2234 */
Kumar Galacc334c72017-04-21 10:55:34 -05002235static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002236{
2237 return _timeout_remaining_get(&work->timeout);
2238}
2239
2240/**
Allan Stephensc98da842016-11-11 15:45:03 -05002241 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242 */
2243
Allan Stephensc98da842016-11-11 15:45:03 -05002244/**
2245 * @cond INTERNAL_HIDDEN
2246 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002247
2248struct k_mutex {
2249 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002250 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002251 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253
Anas Nashif2f203c22016-12-18 06:57:45 -05002254 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255};
2256
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002257#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258 { \
2259 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2260 .owner = NULL, \
2261 .lock_count = 0, \
2262 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002263 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264 }
2265
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002266#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2267
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268/**
Allan Stephensc98da842016-11-11 15:45:03 -05002269 * INTERNAL_HIDDEN @endcond
2270 */
2271
2272/**
2273 * @defgroup mutex_apis Mutex APIs
2274 * @ingroup kernel_apis
2275 * @{
2276 */
2277
2278/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002279 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002281 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002283 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002285 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002288 struct k_mutex name \
2289 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002290 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002291
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002295 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * Upon completion, the mutex is available and does not have an owner.
2298 *
2299 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 *
2301 * @return N/A
2302 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304
2305/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002306 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002308 * This routine locks @a mutex. If the mutex is locked by another thread,
2309 * the calling thread waits until the mutex becomes available or until
2310 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002312 * A thread is permitted to lock a mutex it has already locked. The operation
2313 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * @param mutex Address of the mutex.
2316 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 * or one of the special values K_NO_WAIT and K_FOREVER.
2318 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002319 * @retval 0 Mutex locked.
2320 * @retval -EBUSY Returned without waiting.
2321 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002322 */
Kumar Galacc334c72017-04-21 10:55:34 -05002323extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324
2325/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002328 * This routine unlocks @a mutex. The mutex must already be locked by the
2329 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330 *
2331 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 * thread.
2334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 *
2337 * @return N/A
2338 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339extern void k_mutex_unlock(struct k_mutex *mutex);
2340
Allan Stephensc98da842016-11-11 15:45:03 -05002341/**
2342 * @} end defgroup mutex_apis
2343 */
2344
2345/**
2346 * @cond INTERNAL_HIDDEN
2347 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348
2349struct k_sem {
2350 _wait_q_t wait_q;
2351 unsigned int count;
2352 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002353 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354
Anas Nashif2f203c22016-12-18 06:57:45 -05002355 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356};
2357
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002358#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002359 { \
2360 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2361 .count = initial_count, \
2362 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002363 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002364 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002365 }
2366
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002367#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2368
Allan Stephensc98da842016-11-11 15:45:03 -05002369/**
2370 * INTERNAL_HIDDEN @endcond
2371 */
2372
2373/**
2374 * @defgroup semaphore_apis Semaphore APIs
2375 * @ingroup kernel_apis
2376 * @{
2377 */
2378
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002379/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * @param sem Address of the semaphore.
2385 * @param initial_count Initial semaphore count.
2386 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002387 *
2388 * @return N/A
2389 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002390extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2391 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002392
2393/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2399 *
2400 * @param sem Address of the semaphore.
2401 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002402 * or one of the special values K_NO_WAIT and K_FOREVER.
2403 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002404 * @note When porting code from the nanokernel legacy API to the new API, be
2405 * careful with the return value of this function. The return value is the
2406 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2407 * non-zero means failure, while the nano_sem_take family returns 1 for success
2408 * and 0 for failure.
2409 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002410 * @retval 0 Semaphore taken.
2411 * @retval -EBUSY Returned without waiting.
2412 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002413 */
Kumar Galacc334c72017-04-21 10:55:34 -05002414extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002415
2416/**
2417 * @brief Give a semaphore.
2418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * This routine gives @a sem, unless the semaphore is already at its maximum
2420 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002424 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002425 *
2426 * @return N/A
2427 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428extern void k_sem_give(struct k_sem *sem);
2429
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002430/**
2431 * @brief Reset a semaphore's count to zero.
2432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002435 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002436 *
2437 * @return N/A
2438 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002439static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440{
2441 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442}
2443
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002444/**
2445 * @brief Get a semaphore's count.
2446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002447 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002449 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002452 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002453static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454{
2455 return sem->count;
2456}
2457
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002462 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002463 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * @param name Name of the semaphore.
2466 * @param initial_count Initial semaphore count.
2467 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002468 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002470 struct k_sem name \
2471 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002472 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473
Allan Stephensc98da842016-11-11 15:45:03 -05002474/**
2475 * @} end defgroup semaphore_apis
2476 */
2477
2478/**
2479 * @defgroup alert_apis Alert APIs
2480 * @ingroup kernel_apis
2481 * @{
2482 */
2483
Allan Stephens5eceb852016-11-16 10:16:30 -05002484/**
2485 * @typedef k_alert_handler_t
2486 * @brief Alert handler function type.
2487 *
2488 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002489 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002490 * and is only invoked if the alert has been initialized with one.
2491 *
2492 * @param alert Address of the alert.
2493 *
2494 * @return 0 if alert has been consumed; non-zero if alert should pend.
2495 */
2496typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002497
2498/**
2499 * @} end defgroup alert_apis
2500 */
2501
2502/**
2503 * @cond INTERNAL_HIDDEN
2504 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002506#define K_ALERT_DEFAULT NULL
2507#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002509struct k_alert {
2510 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511 atomic_t send_count;
2512 struct k_work work_item;
2513 struct k_sem sem;
2514
Anas Nashif2f203c22016-12-18 06:57:45 -05002515 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516};
2517
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002518extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002520#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002522 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002524 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2525 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002526 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527 }
2528
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002529#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2530
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531/**
Allan Stephensc98da842016-11-11 15:45:03 -05002532 * INTERNAL_HIDDEN @endcond
2533 */
2534
2535/**
2536 * @addtogroup alert_apis
2537 * @{
2538 */
2539
2540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002543 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002544 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002545 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002547 * @param name Name of the alert.
2548 * @param alert_handler Action to take when alert is sent. Specify either
2549 * the address of a function to be invoked by the system workqueue
2550 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2551 * K_ALERT_DEFAULT (which causes the alert to pend).
2552 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002553 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002554#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002555 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002556 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002557 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002558 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002561 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002565 * @param alert Address of the alert.
2566 * @param handler Action to take when alert is sent. Specify either the address
2567 * of a function to be invoked by the system workqueue thread,
2568 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2569 * K_ALERT_DEFAULT (which causes the alert to pend).
2570 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002571 *
2572 * @return N/A
2573 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002574extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2575 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002576
2577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2583 *
2584 * @param alert Address of the alert.
2585 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002586 * or one of the special values K_NO_WAIT and K_FOREVER.
2587 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002588 * @retval 0 Alert received.
2589 * @retval -EBUSY Returned without waiting.
2590 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002591 */
Kumar Galacc334c72017-04-21 10:55:34 -05002592extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002593
2594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002595 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * This routine signals @a alert. The action specified for @a alert will
2598 * be taken, which may trigger the execution of an alert handler function
2599 * and/or cause the alert to pend (assuming the alert has not reached its
2600 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @note Can be called by ISRs.
2603 *
2604 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002605 *
2606 * @return N/A
2607 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002608extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609
2610/**
Allan Stephensc98da842016-11-11 15:45:03 -05002611 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612 */
2613
Allan Stephensc98da842016-11-11 15:45:03 -05002614/**
2615 * @cond INTERNAL_HIDDEN
2616 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617
2618struct k_msgq {
2619 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002620 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002621 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002622 char *buffer_start;
2623 char *buffer_end;
2624 char *read_ptr;
2625 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002626 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627
Anas Nashif2f203c22016-12-18 06:57:45 -05002628 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629};
2630
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002631#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632 { \
2633 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002634 .max_msgs = q_max_msgs, \
2635 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002637 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638 .read_ptr = q_buffer, \
2639 .write_ptr = q_buffer, \
2640 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002641 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642 }
2643
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002644#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2645
Peter Mitsis1da807e2016-10-06 11:36:59 -04002646/**
Allan Stephensc98da842016-11-11 15:45:03 -05002647 * INTERNAL_HIDDEN @endcond
2648 */
2649
2650/**
2651 * @defgroup msgq_apis Message Queue APIs
2652 * @ingroup kernel_apis
2653 * @{
2654 */
2655
2656/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002659 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2660 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002661 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2662 * message is similarly aligned to this boundary, @a q_msg_size must also be
2663 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * The message queue can be accessed outside the module where it is defined
2666 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002668 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @param q_name Name of the message queue.
2671 * @param q_msg_size Message size (in bytes).
2672 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002673 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002674 */
2675#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2676 static char __noinit __aligned(q_align) \
2677 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002678 struct k_msgq q_name \
2679 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002680 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002681 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682
Peter Mitsisd7a37502016-10-13 11:37:40 -04002683/**
2684 * @brief Initialize a message queue.
2685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * This routine initializes a message queue object, prior to its first use.
2687 *
Allan Stephensda827222016-11-09 14:23:58 -06002688 * The message queue's ring buffer must contain space for @a max_msgs messages,
2689 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2690 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2691 * that each message is similarly aligned to this boundary, @a q_msg_size
2692 * must also be a multiple of N.
2693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * @param q Address of the message queue.
2695 * @param buffer Pointer to ring buffer that holds queued messages.
2696 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002697 * @param max_msgs Maximum number of messages that can be queued.
2698 *
2699 * @return N/A
2700 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002701extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002702 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002703
2704/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002708 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002709 * @note Can be called by ISRs.
2710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * @param q Address of the message queue.
2712 * @param data Pointer to the message.
2713 * @param timeout Waiting period to add the message (in milliseconds),
2714 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002715 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002716 * @retval 0 Message sent.
2717 * @retval -ENOMSG Returned without waiting or queue purged.
2718 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002719 */
Kumar Galacc334c72017-04-21 10:55:34 -05002720extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002721
2722/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002723 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002725 * This routine receives a message from message queue @a q in a "first in,
2726 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002727 *
Allan Stephensc98da842016-11-11 15:45:03 -05002728 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002730 * @param q Address of the message queue.
2731 * @param data Address of area to hold the received message.
2732 * @param timeout Waiting period to receive the message (in milliseconds),
2733 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002734 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002735 * @retval 0 Message received.
2736 * @retval -ENOMSG Returned without waiting.
2737 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002738 */
Kumar Galacc334c72017-04-21 10:55:34 -05002739extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002740
2741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002744 * This routine discards all unreceived messages in a message queue's ring
2745 * buffer. Any threads that are blocked waiting to send a message to the
2746 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002748 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002749 *
2750 * @return N/A
2751 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752extern void k_msgq_purge(struct k_msgq *q);
2753
Peter Mitsis67be2492016-10-07 11:44:34 -04002754/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * This routine returns the number of unused entries in a message queue's
2758 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * @param q Address of the message queue.
2761 *
2762 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002763 */
Kumar Galacc334c72017-04-21 10:55:34 -05002764static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002765{
2766 return q->max_msgs - q->used_msgs;
2767}
2768
Peter Mitsisd7a37502016-10-13 11:37:40 -04002769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002772 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002774 * @param q Address of the message queue.
2775 *
2776 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002777 */
Kumar Galacc334c72017-04-21 10:55:34 -05002778static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779{
2780 return q->used_msgs;
2781}
2782
Allan Stephensc98da842016-11-11 15:45:03 -05002783/**
2784 * @} end defgroup msgq_apis
2785 */
2786
2787/**
2788 * @defgroup mem_pool_apis Memory Pool APIs
2789 * @ingroup kernel_apis
2790 * @{
2791 */
2792
Andy Ross73cb9582017-05-09 10:42:39 -07002793/* Note on sizing: the use of a 20 bit field for block means that,
2794 * assuming a reasonable minimum block size of 16 bytes, we're limited
2795 * to 16M of memory managed by a single pool. Long term it would be
2796 * good to move to a variable bit size based on configuration.
2797 */
2798struct k_mem_block_id {
2799 u32_t pool : 8;
2800 u32_t level : 4;
2801 u32_t block : 20;
2802};
2803
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002806 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807};
2808
Allan Stephensc98da842016-11-11 15:45:03 -05002809/**
2810 * @} end defgroup mem_pool_apis
2811 */
2812
2813/**
2814 * @defgroup mailbox_apis Mailbox APIs
2815 * @ingroup kernel_apis
2816 * @{
2817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818
2819struct k_mbox_msg {
2820 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002821 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002823 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002825 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826 /** sender's message data buffer */
2827 void *tx_data;
2828 /** internal use only - needed for legacy API support */
2829 void *_rx_data;
2830 /** message data block descriptor */
2831 struct k_mem_block tx_block;
2832 /** source thread id */
2833 k_tid_t rx_source_thread;
2834 /** target thread id */
2835 k_tid_t tx_target_thread;
2836 /** internal use only - thread waiting on send (may be a dummy) */
2837 k_tid_t _syncing_thread;
2838#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2839 /** internal use only - semaphore used during asynchronous send */
2840 struct k_sem *_async_sem;
2841#endif
2842};
2843
Allan Stephensc98da842016-11-11 15:45:03 -05002844/**
2845 * @cond INTERNAL_HIDDEN
2846 */
2847
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848struct k_mbox {
2849 _wait_q_t tx_msg_queue;
2850 _wait_q_t rx_msg_queue;
2851
Anas Nashif2f203c22016-12-18 06:57:45 -05002852 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002853};
2854
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002855#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856 { \
2857 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2858 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002859 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860 }
2861
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002862#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2863
Peter Mitsis12092702016-10-14 12:57:23 -04002864/**
Allan Stephensc98da842016-11-11 15:45:03 -05002865 * INTERNAL_HIDDEN @endcond
2866 */
2867
2868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002873 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002876 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002878 struct k_mbox name \
2879 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002880 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002881
Peter Mitsis12092702016-10-14 12:57:23 -04002882/**
2883 * @brief Initialize a mailbox.
2884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * This routine initializes a mailbox object, prior to its first use.
2886 *
2887 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002888 *
2889 * @return N/A
2890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891extern void k_mbox_init(struct k_mbox *mbox);
2892
Peter Mitsis12092702016-10-14 12:57:23 -04002893/**
2894 * @brief Send a mailbox message in a synchronous manner.
2895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * This routine sends a message to @a mbox and waits for a receiver to both
2897 * receive and process it. The message data may be in a buffer, in a memory
2898 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @param mbox Address of the mailbox.
2901 * @param tx_msg Address of the transmit message descriptor.
2902 * @param timeout Waiting period for the message to be received (in
2903 * milliseconds), or one of the special values K_NO_WAIT
2904 * and K_FOREVER. Once the message has been received,
2905 * this routine waits as long as necessary for the message
2906 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002907 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002908 * @retval 0 Message sent.
2909 * @retval -ENOMSG Returned without waiting.
2910 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002911 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002912extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002913 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002914
Peter Mitsis12092702016-10-14 12:57:23 -04002915/**
2916 * @brief Send a mailbox message in an asynchronous manner.
2917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * This routine sends a message to @a mbox without waiting for a receiver
2919 * to process it. The message data may be in a buffer, in a memory pool block,
2920 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2921 * will be given when the message has been both received and completely
2922 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002924 * @param mbox Address of the mailbox.
2925 * @param tx_msg Address of the transmit message descriptor.
2926 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002927 *
2928 * @return N/A
2929 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002930extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931 struct k_sem *sem);
2932
Peter Mitsis12092702016-10-14 12:57:23 -04002933/**
2934 * @brief Receive a mailbox message.
2935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * This routine receives a message from @a mbox, then optionally retrieves
2937 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * @param mbox Address of the mailbox.
2940 * @param rx_msg Address of the receive message descriptor.
2941 * @param buffer Address of the buffer to receive data, or NULL to defer data
2942 * retrieval and message disposal until later.
2943 * @param timeout Waiting period for a message to be received (in
2944 * milliseconds), or one of the special values K_NO_WAIT
2945 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002946 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002947 * @retval 0 Message received.
2948 * @retval -ENOMSG Returned without waiting.
2949 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002950 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002951extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002952 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002953
2954/**
2955 * @brief Retrieve mailbox message data into a buffer.
2956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * This routine completes the processing of a received message by retrieving
2958 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002959 *
2960 * Alternatively, this routine can be used to dispose of a received message
2961 * without retrieving its data.
2962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * @param rx_msg Address of the receive message descriptor.
2964 * @param buffer Address of the buffer to receive data, or NULL to discard
2965 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002966 *
2967 * @return N/A
2968 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002969extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002970
2971/**
2972 * @brief Retrieve mailbox message data into a memory pool block.
2973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * This routine completes the processing of a received message by retrieving
2975 * its data into a memory pool block, then disposing of the message.
2976 * The memory pool block that results from successful retrieval must be
2977 * returned to the pool once the data has been processed, even in cases
2978 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002979 *
2980 * Alternatively, this routine can be used to dispose of a received message
2981 * without retrieving its data. In this case there is no need to return a
2982 * memory pool block to the pool.
2983 *
2984 * This routine allocates a new memory pool block for the data only if the
2985 * data is not already in one. If a new block cannot be allocated, the routine
2986 * returns a failure code and the received message is left unchanged. This
2987 * permits the caller to reattempt data retrieval at a later time or to dispose
2988 * of the received message without retrieving its data.
2989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @param rx_msg Address of a receive message descriptor.
2991 * @param pool Address of memory pool, or NULL to discard data.
2992 * @param block Address of the area to hold memory pool block info.
2993 * @param timeout Waiting period to wait for a memory pool block (in
2994 * milliseconds), or one of the special values K_NO_WAIT
2995 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002996 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002997 * @retval 0 Data retrieved.
2998 * @retval -ENOMEM Returned without waiting.
2999 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003000 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003001extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003002 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003003 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004
Allan Stephensc98da842016-11-11 15:45:03 -05003005/**
3006 * @} end defgroup mailbox_apis
3007 */
3008
3009/**
3010 * @cond INTERNAL_HIDDEN
3011 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012
3013struct k_pipe {
3014 unsigned char *buffer; /* Pipe buffer: may be NULL */
3015 size_t size; /* Buffer size */
3016 size_t bytes_used; /* # bytes used in buffer */
3017 size_t read_index; /* Where in buffer to read from */
3018 size_t write_index; /* Where in buffer to write */
3019
3020 struct {
3021 _wait_q_t readers; /* Reader wait queue */
3022 _wait_q_t writers; /* Writer wait queue */
3023 } wait_q;
3024
Anas Nashif2f203c22016-12-18 06:57:45 -05003025 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026};
3027
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003028#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029 { \
3030 .buffer = pipe_buffer, \
3031 .size = pipe_buffer_size, \
3032 .bytes_used = 0, \
3033 .read_index = 0, \
3034 .write_index = 0, \
3035 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3036 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003037 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003038 }
3039
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003040#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3041
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003042/**
Allan Stephensc98da842016-11-11 15:45:03 -05003043 * INTERNAL_HIDDEN @endcond
3044 */
3045
3046/**
3047 * @defgroup pipe_apis Pipe APIs
3048 * @ingroup kernel_apis
3049 * @{
3050 */
3051
3052/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003056 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003057 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * @param name Name of the pipe.
3060 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3061 * or zero if no ring buffer is used.
3062 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003063 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003064#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3065 static unsigned char __noinit __aligned(pipe_align) \
3066 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003067 struct k_pipe name \
3068 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003069 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003072 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * This routine initializes a pipe object, prior to its first use.
3075 *
3076 * @param pipe Address of the pipe.
3077 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3078 * is used.
3079 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3080 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081 *
3082 * @return N/A
3083 */
3084extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3085 size_t size);
3086
3087/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003088 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * @param pipe Address of the pipe.
3093 * @param data Address of data to write.
3094 * @param bytes_to_write Size of data (in bytes).
3095 * @param bytes_written Address of area to hold the number of bytes written.
3096 * @param min_xfer Minimum number of bytes to write.
3097 * @param timeout Waiting period to wait for the data to be written (in
3098 * milliseconds), or one of the special values K_NO_WAIT
3099 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003101 * @retval 0 At least @a min_xfer bytes of data were written.
3102 * @retval -EIO Returned without waiting; zero data bytes were written.
3103 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003104 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003106extern int k_pipe_put(struct k_pipe *pipe, void *data,
3107 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003108 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109
3110/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003113 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * @param pipe Address of the pipe.
3116 * @param data Address to place the data read from pipe.
3117 * @param bytes_to_read Maximum number of data bytes to read.
3118 * @param bytes_read Address of area to hold the number of bytes read.
3119 * @param min_xfer Minimum number of data bytes to read.
3120 * @param timeout Waiting period to wait for the data to be read (in
3121 * milliseconds), or one of the special values K_NO_WAIT
3122 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003124 * @retval 0 At least @a min_xfer bytes of data were read.
3125 * @retval -EIO Returned without waiting; zero data bytes were read.
3126 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003128 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003129extern int k_pipe_get(struct k_pipe *pipe, void *data,
3130 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003131 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132
3133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003134 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003136 * This routine writes the data contained in a memory block to @a pipe.
3137 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141 * @param block Memory block containing data to send
3142 * @param size Number of data bytes in memory block to send
3143 * @param sem Semaphore to signal upon completion (else NULL)
3144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003145 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146 */
3147extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3148 size_t size, struct k_sem *sem);
3149
3150/**
Allan Stephensc98da842016-11-11 15:45:03 -05003151 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152 */
3153
Allan Stephensc98da842016-11-11 15:45:03 -05003154/**
3155 * @cond INTERNAL_HIDDEN
3156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003158struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003160 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003161 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162 char *buffer;
3163 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003164 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165
Anas Nashif2f203c22016-12-18 06:57:45 -05003166 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003167};
3168
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003169#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003170 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003171 { \
3172 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003173 .num_blocks = slab_num_blocks, \
3174 .block_size = slab_block_size, \
3175 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176 .free_list = NULL, \
3177 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003178 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179 }
3180
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003181#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3182
3183
Peter Mitsis578f9112016-10-07 13:50:31 -04003184/**
Allan Stephensc98da842016-11-11 15:45:03 -05003185 * INTERNAL_HIDDEN @endcond
3186 */
3187
3188/**
3189 * @defgroup mem_slab_apis Memory Slab APIs
3190 * @ingroup kernel_apis
3191 * @{
3192 */
3193
3194/**
Allan Stephensda827222016-11-09 14:23:58 -06003195 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003196 *
Allan Stephensda827222016-11-09 14:23:58 -06003197 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003199 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3200 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003201 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003202 *
Allan Stephensda827222016-11-09 14:23:58 -06003203 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003205 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003206 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * @param name Name of the memory slab.
3209 * @param slab_block_size Size of each memory block (in bytes).
3210 * @param slab_num_blocks Number memory blocks.
3211 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003212 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003213#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3214 char __noinit __aligned(slab_align) \
3215 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3216 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003217 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003218 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003219 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003221/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003222 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003225 *
Allan Stephensda827222016-11-09 14:23:58 -06003226 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3227 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3228 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3229 * To ensure that each memory block is similarly aligned to this boundary,
3230 * @a slab_block_size must also be a multiple of N.
3231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * @param slab Address of the memory slab.
3233 * @param buffer Pointer to buffer used for the memory blocks.
3234 * @param block_size Size of each memory block (in bytes).
3235 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003236 *
3237 * @return N/A
3238 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003239extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003240 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003241
3242/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003243 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003245 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * @param slab Address of the memory slab.
3248 * @param mem Pointer to block address area.
3249 * @param timeout Maximum time to wait for operation to complete
3250 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3251 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003252 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003253 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003255 * @retval -ENOMEM Returned without waiting.
3256 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003257 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003258extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003259 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003260
3261/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * This routine releases a previously allocated memory block back to its
3265 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @param slab Address of the memory slab.
3268 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003269 *
3270 * @return N/A
3271 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003272extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * This routine gets the number of memory blocks that are currently
3278 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003283 */
Kumar Galacc334c72017-04-21 10:55:34 -05003284static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003286 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287}
3288
Peter Mitsisc001aa82016-10-13 13:53:37 -04003289/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003290 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * This routine gets the number of memory blocks that are currently
3293 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003298 */
Kumar Galacc334c72017-04-21 10:55:34 -05003299static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003300{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003301 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003302}
3303
Allan Stephensc98da842016-11-11 15:45:03 -05003304/**
3305 * @} end defgroup mem_slab_apis
3306 */
3307
3308/**
3309 * @cond INTERNAL_HIDDEN
3310 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311
Andy Ross73cb9582017-05-09 10:42:39 -07003312struct k_mem_pool_lvl {
3313 union {
3314 u32_t *bits_p;
3315 u32_t bits;
3316 };
3317 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003318};
3319
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003320struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003321 void *buf;
3322 size_t max_sz;
3323 u16_t n_max;
3324 u8_t n_levels;
3325 u8_t max_inline_level;
3326 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003327 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328};
3329
Andy Ross73cb9582017-05-09 10:42:39 -07003330#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331
Andy Ross73cb9582017-05-09 10:42:39 -07003332#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3333
3334#define _MPOOL_LVLS(maxsz, minsz) \
3335 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3336 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3337 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3338 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3339 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3340 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3341 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3342 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3343 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3344 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3345 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3346 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3347 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3348 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3349 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3350 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3351
3352/* Rounds the needed bits up to integer multiples of u32_t */
3353#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3354 ((((n_max) << (2*(l))) + 31) / 32)
3355
3356/* One word gets stored free unioned with the pointer, otherwise the
3357 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003358 */
Andy Ross73cb9582017-05-09 10:42:39 -07003359#define _MPOOL_LBIT_WORDS(n_max, l) \
3360 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3361 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003362
Andy Ross73cb9582017-05-09 10:42:39 -07003363/* How many bytes for the bitfields of a single level? */
3364#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3365 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3366 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003367
Andy Ross73cb9582017-05-09 10:42:39 -07003368/* Size of the bitmap array that follows the buffer in allocated memory */
3369#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3370 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3371 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3372 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3373 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3374 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3375 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3376 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3377 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3378 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3379 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3380 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3381 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3382 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3383 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3384 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3385 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003386
3387/**
Allan Stephensc98da842016-11-11 15:45:03 -05003388 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003389 */
3390
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003391/**
Allan Stephensc98da842016-11-11 15:45:03 -05003392 * @addtogroup mem_pool_apis
3393 * @{
3394 */
3395
3396/**
3397 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3400 * long. The memory pool allows blocks to be repeatedly partitioned into
3401 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003402 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003403 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003404 * If the pool is to be accessed outside the module where it is defined, it
3405 * can be declared via
3406 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003407 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003410 * @param minsz Size of the smallest blocks in the pool (in bytes).
3411 * @param maxsz Size of the largest blocks in the pool (in bytes).
3412 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003414 */
Andy Ross73cb9582017-05-09 10:42:39 -07003415#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3416 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3417 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3418 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3419 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3420 .buf = _mpool_buf_##name, \
3421 .max_sz = maxsz, \
3422 .n_max = nmax, \
3423 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3424 .levels = _mpool_lvls_##name, \
3425 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003426
Peter Mitsis937042c2016-10-13 13:18:26 -04003427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * @param pool Address of the memory pool.
3433 * @param block Pointer to block descriptor for the allocated memory.
3434 * @param size Amount of memory to allocate (in bytes).
3435 * @param timeout Maximum time to wait for operation to complete
3436 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3437 * or K_FOREVER to wait as long as necessary.
3438 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003439 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003441 * @retval -ENOMEM Returned without waiting.
3442 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003443 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003444extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003445 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003446
3447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * This routine releases a previously allocated memory block back to its
3451 * memory pool.
3452 *
3453 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003454 *
3455 * @return N/A
3456 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003457extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003458
3459/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003461 *
Andy Ross73cb9582017-05-09 10:42:39 -07003462 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003463 *
Andy Ross73cb9582017-05-09 10:42:39 -07003464 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003465 *
3466 * @return N/A
3467 */
Andy Ross73cb9582017-05-09 10:42:39 -07003468static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003469
3470/**
Allan Stephensc98da842016-11-11 15:45:03 -05003471 * @} end addtogroup mem_pool_apis
3472 */
3473
3474/**
3475 * @defgroup heap_apis Heap Memory Pool APIs
3476 * @ingroup kernel_apis
3477 * @{
3478 */
3479
3480/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003484 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003488 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003489 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003490extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003491
3492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003493 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003494 *
3495 * This routine provides traditional free() semantics. The memory being
3496 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003497 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003498 * If @a ptr is NULL, no operation is performed.
3499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003500 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003501 *
3502 * @return N/A
3503 */
3504extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003505
Allan Stephensc98da842016-11-11 15:45:03 -05003506/**
3507 * @} end defgroup heap_apis
3508 */
3509
Benjamin Walshacc68c12017-01-29 18:57:45 -05003510/* polling API - PRIVATE */
3511
Benjamin Walshb0179862017-02-02 16:39:57 -05003512#ifdef CONFIG_POLL
3513#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3514#else
3515#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3516#endif
3517
Benjamin Walshacc68c12017-01-29 18:57:45 -05003518/* private - implementation data created as needed, per-type */
3519struct _poller {
3520 struct k_thread *thread;
3521};
3522
3523/* private - types bit positions */
3524enum _poll_types_bits {
3525 /* can be used to ignore an event */
3526 _POLL_TYPE_IGNORE,
3527
3528 /* to be signaled by k_poll_signal() */
3529 _POLL_TYPE_SIGNAL,
3530
3531 /* semaphore availability */
3532 _POLL_TYPE_SEM_AVAILABLE,
3533
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003534 /* queue/fifo/lifo data availability */
3535 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003536
3537 _POLL_NUM_TYPES
3538};
3539
3540#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3541
3542/* private - states bit positions */
3543enum _poll_states_bits {
3544 /* default state when creating event */
3545 _POLL_STATE_NOT_READY,
3546
Benjamin Walshacc68c12017-01-29 18:57:45 -05003547 /* signaled by k_poll_signal() */
3548 _POLL_STATE_SIGNALED,
3549
3550 /* semaphore is available */
3551 _POLL_STATE_SEM_AVAILABLE,
3552
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003553 /* data is available to read on queue/fifo/lifo */
3554 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003555
3556 _POLL_NUM_STATES
3557};
3558
3559#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3560
3561#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003562 (32 - (0 \
3563 + 8 /* tag */ \
3564 + _POLL_NUM_TYPES \
3565 + _POLL_NUM_STATES \
3566 + 1 /* modes */ \
3567 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003568
3569#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3570#error overflow of 32-bit word in struct k_poll_event
3571#endif
3572
3573/* end of polling API - PRIVATE */
3574
3575
3576/**
3577 * @defgroup poll_apis Async polling APIs
3578 * @ingroup kernel_apis
3579 * @{
3580 */
3581
3582/* Public polling API */
3583
3584/* public - values for k_poll_event.type bitfield */
3585#define K_POLL_TYPE_IGNORE 0
3586#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3587#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003588#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3589#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003590
3591/* public - polling modes */
3592enum k_poll_modes {
3593 /* polling thread does not take ownership of objects when available */
3594 K_POLL_MODE_NOTIFY_ONLY = 0,
3595
3596 K_POLL_NUM_MODES
3597};
3598
3599/* public - values for k_poll_event.state bitfield */
3600#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003601#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3602#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003603#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3604#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003605
3606/* public - poll signal object */
3607struct k_poll_signal {
3608 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003609 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003610
3611 /*
3612 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3613 * user resets it to 0.
3614 */
3615 unsigned int signaled;
3616
3617 /* custom result value passed to k_poll_signal() if needed */
3618 int result;
3619};
3620
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003621#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003622 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003623 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003624 .signaled = 0, \
3625 .result = 0, \
3626 }
3627
3628struct k_poll_event {
3629 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003630 sys_dnode_t _node;
3631
3632 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003633 struct _poller *poller;
3634
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003635 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003636 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003637
Benjamin Walshacc68c12017-01-29 18:57:45 -05003638 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003639 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003640
3641 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003642 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003643
3644 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003645 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003646
3647 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003648 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003649
3650 /* per-type data */
3651 union {
3652 void *obj;
3653 struct k_poll_signal *signal;
3654 struct k_sem *sem;
3655 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003656 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003657 };
3658};
3659
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003660#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003661 { \
3662 .poller = NULL, \
3663 .type = event_type, \
3664 .state = K_POLL_STATE_NOT_READY, \
3665 .mode = event_mode, \
3666 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003667 { .obj = event_obj }, \
3668 }
3669
3670#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3671 event_tag) \
3672 { \
3673 .type = event_type, \
3674 .tag = event_tag, \
3675 .state = K_POLL_STATE_NOT_READY, \
3676 .mode = event_mode, \
3677 .unused = 0, \
3678 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003679 }
3680
3681/**
3682 * @brief Initialize one struct k_poll_event instance
3683 *
3684 * After this routine is called on a poll event, the event it ready to be
3685 * placed in an event array to be passed to k_poll().
3686 *
3687 * @param event The event to initialize.
3688 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3689 * values. Only values that apply to the same object being polled
3690 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3691 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003692 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003693 * @param obj Kernel object or poll signal.
3694 *
3695 * @return N/A
3696 */
3697
Kumar Galacc334c72017-04-21 10:55:34 -05003698extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003699 int mode, void *obj);
3700
3701/**
3702 * @brief Wait for one or many of multiple poll events to occur
3703 *
3704 * This routine allows a thread to wait concurrently for one or many of
3705 * multiple poll events to have occurred. Such events can be a kernel object
3706 * being available, like a semaphore, or a poll signal event.
3707 *
3708 * When an event notifies that a kernel object is available, the kernel object
3709 * is not "given" to the thread calling k_poll(): it merely signals the fact
3710 * that the object was available when the k_poll() call was in effect. Also,
3711 * all threads trying to acquire an object the regular way, i.e. by pending on
3712 * the object, have precedence over the thread polling on the object. This
3713 * means that the polling thread will never get the poll event on an object
3714 * until the object becomes available and its pend queue is empty. For this
3715 * reason, the k_poll() call is more effective when the objects being polled
3716 * only have one thread, the polling thread, trying to acquire them.
3717 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003718 * When k_poll() returns 0, the caller should loop on all the events that were
3719 * passed to k_poll() and check the state field for the values that were
3720 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003721 *
3722 * Before being reused for another call to k_poll(), the user has to reset the
3723 * state field to K_POLL_STATE_NOT_READY.
3724 *
3725 * @param events An array of pointers to events to be polled for.
3726 * @param num_events The number of events in the array.
3727 * @param timeout Waiting period for an event to be ready (in milliseconds),
3728 * or one of the special values K_NO_WAIT and K_FOREVER.
3729 *
3730 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003731 * @retval -EAGAIN Waiting period timed out.
3732 */
3733
3734extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003735 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003736
3737/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003738 * @brief Initialize a poll signal object.
3739 *
3740 * Ready a poll signal object to be signaled via k_poll_signal().
3741 *
3742 * @param signal A poll signal.
3743 *
3744 * @return N/A
3745 */
3746
3747extern void k_poll_signal_init(struct k_poll_signal *signal);
3748
3749/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003750 * @brief Signal a poll signal object.
3751 *
3752 * This routine makes ready a poll signal, which is basically a poll event of
3753 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3754 * made ready to run. A @a result value can be specified.
3755 *
3756 * The poll signal contains a 'signaled' field that, when set by
3757 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3758 * be reset by the user before being passed again to k_poll() or k_poll() will
3759 * consider it being signaled, and will return immediately.
3760 *
3761 * @param signal A poll signal.
3762 * @param result The value to store in the result field of the signal.
3763 *
3764 * @retval 0 The signal was delivered successfully.
3765 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3766 */
3767
3768extern int k_poll_signal(struct k_poll_signal *signal, int result);
3769
3770/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003771extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003772
3773/**
3774 * @} end defgroup poll_apis
3775 */
3776
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003777/**
3778 * @brief Make the CPU idle.
3779 *
3780 * This function makes the CPU idle until an event wakes it up.
3781 *
3782 * In a regular system, the idle thread should be the only thread responsible
3783 * for making the CPU idle and triggering any type of power management.
3784 * However, in some more constrained systems, such as a single-threaded system,
3785 * the only thread would be responsible for this if needed.
3786 *
3787 * @return N/A
3788 */
3789extern void k_cpu_idle(void);
3790
3791/**
3792 * @brief Make the CPU idle in an atomic fashion.
3793 *
3794 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3795 * must be done atomically before making the CPU idle.
3796 *
3797 * @param key Interrupt locking key obtained from irq_lock().
3798 *
3799 * @return N/A
3800 */
3801extern void k_cpu_atomic_idle(unsigned int key);
3802
Kumar Galacc334c72017-04-21 10:55:34 -05003803extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003804
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003805#include <arch/cpu.h>
3806
Andrew Boiecdb94d62017-04-18 15:22:05 -07003807#ifdef _ARCH_EXCEPT
3808/* This archtecture has direct support for triggering a CPU exception */
3809#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3810#else
3811
3812#include <misc/printk.h>
3813
3814/* NOTE: This is the implementation for arches that do not implement
3815 * _ARCH_EXCEPT() to generate a real CPU exception.
3816 *
3817 * We won't have a real exception frame to determine the PC value when
3818 * the oops occurred, so print file and line number before we jump into
3819 * the fatal error handler.
3820 */
3821#define _k_except_reason(reason) do { \
3822 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3823 _NanoFatalErrorHandler(reason, &_default_esf); \
3824 CODE_UNREACHABLE; \
3825 } while (0)
3826
3827#endif /* _ARCH__EXCEPT */
3828
3829/**
3830 * @brief Fatally terminate a thread
3831 *
3832 * This should be called when a thread has encountered an unrecoverable
3833 * runtime condition and needs to terminate. What this ultimately
3834 * means is determined by the _fatal_error_handler() implementation, which
3835 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3836 *
3837 * If this is called from ISR context, the default system fatal error handler
3838 * will treat it as an unrecoverable system error, just like k_panic().
3839 */
3840#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3841
3842/**
3843 * @brief Fatally terminate the system
3844 *
3845 * This should be called when the Zephyr kernel has encountered an
3846 * unrecoverable runtime condition and needs to terminate. What this ultimately
3847 * means is determined by the _fatal_error_handler() implementation, which
3848 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3849 */
3850#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3851
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852/*
3853 * private APIs that are utilized by one or more public APIs
3854 */
3855
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003856#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003858#else
3859#define _init_static_threads() do { } while ((0))
3860#endif
3861
3862extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003863extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864
Andrew Boiedc5d9352017-06-02 12:56:47 -07003865/* arch/cpu.h may declare an architecture or platform-specific macro
3866 * for properly declaring stacks, compatible with MMU/MPU constraints if
3867 * enabled
3868 */
3869#ifdef _ARCH_THREAD_STACK_DEFINE
3870#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3871#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3872 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3873#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3874#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003875static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3876{
3877 return _ARCH_THREAD_STACK_BUFFER(sym);
3878}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003879#else
3880/**
3881 * @brief Declare a toplevel thread stack memory region
3882 *
3883 * This declares a region of memory suitable for use as a thread's stack.
3884 *
3885 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3886 * 'noinit' section so that it isn't zeroed at boot
3887 *
Andrew Boie507852a2017-07-25 18:47:07 -07003888 * The declared symbol will always be a k_thread_stack_t which can be passed to
3889 * k_thread_create, but should otherwise not be manipulated. If the buffer
3890 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003891 *
3892 * It is legal to precede this definition with the 'static' keyword.
3893 *
3894 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3895 * parameter of k_thread_create(), it may not be the same as the
3896 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3897 *
3898 * @param sym Thread stack symbol name
3899 * @param size Size of the stack memory region
3900 */
3901#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003902 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003903
3904/**
3905 * @brief Declare a toplevel array of thread stack memory regions
3906 *
3907 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3908 * definition for additional details and constraints.
3909 *
3910 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3911 * 'noinit' section so that it isn't zeroed at boot
3912 *
3913 * @param sym Thread stack symbol name
3914 * @param nmemb Number of stacks to declare
3915 * @param size Size of the stack memory region
3916 */
3917
3918#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003919 struct _k_thread_stack_element __noinit \
3920 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003921
3922/**
3923 * @brief Declare an embedded stack memory region
3924 *
3925 * Used for stacks embedded within other data structures. Use is highly
3926 * discouraged but in some cases necessary. For memory protection scenarios,
3927 * it is very important that any RAM preceding this member not be writable
3928 * by threads else a stack overflow will lead to silent corruption. In other
3929 * words, the containing data structure should live in RAM owned by the kernel.
3930 *
3931 * @param sym Thread stack symbol name
3932 * @param size Size of the stack memory region
3933 */
3934#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003935 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003936
3937/**
3938 * @brief Return the size in bytes of a stack memory region
3939 *
3940 * Convenience macro for passing the desired stack size to k_thread_create()
3941 * since the underlying implementation may actually create something larger
3942 * (for instance a guard area).
3943 *
3944 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07003945 * passed to K_THREAD_STACK_DEFINE.
3946 *
3947 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
3948 * it is not guaranteed to return the original value since each array
3949 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07003950 *
3951 * @param sym Stack memory symbol
3952 * @return Size of the stack
3953 */
3954#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
3955
3956/**
3957 * @brief Get a pointer to the physical stack buffer
3958 *
3959 * Convenience macro to get at the real underlying stack buffer used by
3960 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
3961 * This is really only intended for diagnostic tools which want to examine
3962 * stack memory contents.
3963 *
3964 * @param sym Declared stack symbol name
3965 * @return The buffer itself, a char *
3966 */
Andrew Boie507852a2017-07-25 18:47:07 -07003967static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3968{
3969 return (char *)sym;
3970}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003971
3972#endif /* _ARCH_DECLARE_STACK */
3973
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974#ifdef __cplusplus
3975}
3976#endif
3977
Andrew Boiee004dec2016-11-07 09:01:19 -08003978#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3979/*
3980 * Define new and delete operators.
3981 * At this moment, the operators do nothing since objects are supposed
3982 * to be statically allocated.
3983 */
3984inline void operator delete(void *ptr)
3985{
3986 (void)ptr;
3987}
3988
3989inline void operator delete[](void *ptr)
3990{
3991 (void)ptr;
3992}
3993
3994inline void *operator new(size_t size)
3995{
3996 (void)size;
3997 return NULL;
3998}
3999
4000inline void *operator new[](size_t size)
4001{
4002 (void)size;
4003 return NULL;
4004}
4005
4006/* Placement versions of operator new and delete */
4007inline void operator delete(void *ptr1, void *ptr2)
4008{
4009 (void)ptr1;
4010 (void)ptr2;
4011}
4012
4013inline void operator delete[](void *ptr1, void *ptr2)
4014{
4015 (void)ptr1;
4016 (void)ptr2;
4017}
4018
4019inline void *operator new(size_t size, void *ptr)
4020{
4021 (void)size;
4022 return ptr;
4023}
4024
4025inline void *operator new[](size_t size, void *ptr)
4026{
4027 (void)size;
4028 return ptr;
4029}
4030
4031#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4032
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004033#endif /* !_ASMLANGUAGE */
4034
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004035#endif /* _kernel__h_ */