blob: a51b59737a777f53463566f676b5a1f55dad25e3 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
101#define _POLL_EVENT_OBJ_INIT \
102 .poll_event = NULL,
103#define _POLL_EVENT struct k_poll_event *poll_event
104#else
105#define _POLL_EVENT_OBJ_INIT
106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Andrew Boie73abd322017-04-04 13:19:13 -0700126/* timeouts */
127
128struct _timeout;
129typedef void (*_timeout_func_t)(struct _timeout *t);
130
131struct _timeout {
132 sys_dnode_t node;
133 struct k_thread *thread;
134 sys_dlist_t *wait_q;
135 s32_t delta_ticks_from_prev;
136 _timeout_func_t func;
137};
138
139extern s32_t _timeout_remaining_get(struct _timeout *timeout);
140
141/* Threads */
142typedef void (*_thread_entry_t)(void *, void *, void *);
143
144#ifdef CONFIG_THREAD_MONITOR
145struct __thread_entry {
146 _thread_entry_t pEntry;
147 void *parameter1;
148 void *parameter2;
149 void *parameter3;
150};
151#endif
152
153/* can be used for creating 'dummy' threads, e.g. for pending on objects */
154struct _thread_base {
155
156 /* this thread's entry in a ready/wait queue */
157 sys_dnode_t k_q_node;
158
159 /* user facing 'thread options'; values defined in include/kernel.h */
160 u8_t user_options;
161
162 /* thread state */
163 u8_t thread_state;
164
165 /*
166 * scheduler lock count and thread priority
167 *
168 * These two fields control the preemptibility of a thread.
169 *
170 * When the scheduler is locked, sched_locked is decremented, which
171 * means that the scheduler is locked for values from 0xff to 0x01. A
172 * thread is coop if its prio is negative, thus 0x80 to 0xff when
173 * looked at the value as unsigned.
174 *
175 * By putting them end-to-end, this means that a thread is
176 * non-preemptible if the bundled value is greater than or equal to
177 * 0x0080.
178 */
179 union {
180 struct {
181#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
182 u8_t sched_locked;
183 s8_t prio;
184#else /* LITTLE and PDP */
185 s8_t prio;
186 u8_t sched_locked;
187#endif
188 };
189 u16_t preempt;
190 };
191
192 /* data returned by APIs */
193 void *swap_data;
194
195#ifdef CONFIG_SYS_CLOCK_EXISTS
196 /* this thread's entry in a timeout queue */
197 struct _timeout timeout;
198#endif
199
200};
201
202typedef struct _thread_base _thread_base_t;
203
204#if defined(CONFIG_THREAD_STACK_INFO)
205/* Contains the stack information of a thread */
206struct _thread_stack_info {
207 /* Stack Start */
208 u32_t start;
209 /* Stack Size */
210 u32_t size;
211};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700212
213typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700214#endif /* CONFIG_THREAD_STACK_INFO */
215
216struct k_thread {
217
218 struct _thread_base base;
219
220 /* defined by the architecture, but all archs need these */
221 struct _caller_saved caller_saved;
222 struct _callee_saved callee_saved;
223
224 /* static thread init data */
225 void *init_data;
226
227 /* abort function */
228 void (*fn_abort)(void);
229
230#if defined(CONFIG_THREAD_MONITOR)
231 /* thread entry and parameters description */
232 struct __thread_entry *entry;
233
234 /* next item in list of all threads */
235 struct k_thread *next_thread;
236#endif
237
238#ifdef CONFIG_THREAD_CUSTOM_DATA
239 /* crude thread-local storage */
240 void *custom_data;
241#endif
242
243#ifdef CONFIG_ERRNO
244 /* per-thread errno variable */
245 int errno_var;
246#endif
247
248#if defined(CONFIG_THREAD_STACK_INFO)
249 /* Stack Info */
250 struct _thread_stack_info stack_info;
251#endif /* CONFIG_THREAD_STACK_INFO */
252
253 /* arch-specifics: must always be at the end */
254 struct _thread_arch arch;
255};
256
257typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400258typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700259#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261enum execution_context_types {
262 K_ISR = 0,
263 K_COOP_THREAD,
264 K_PREEMPT_THREAD,
265};
266
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100268 * @defgroup profiling_apis Profiling APIs
269 * @ingroup kernel_apis
270 * @{
271 */
272
273/**
274 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
275 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700276 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100277 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
278 *
279 * CONFIG_MAIN_STACK_SIZE
280 * CONFIG_IDLE_STACK_SIZE
281 * CONFIG_ISR_STACK_SIZE
282 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
283 *
284 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
285 * produce output.
286 *
287 * @return N/A
288 */
289extern void k_call_stacks_analyze(void);
290
291/**
292 * @} end defgroup profiling_apis
293 */
294
295/**
Allan Stephensc98da842016-11-11 15:45:03 -0500296 * @defgroup thread_apis Thread APIs
297 * @ingroup kernel_apis
298 * @{
299 */
300
301/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500302 * @typedef k_thread_entry_t
303 * @brief Thread entry point function type.
304 *
305 * A thread's entry point function is invoked when the thread starts executing.
306 * Up to 3 argument values can be passed to the function.
307 *
308 * The thread terminates execution permanently if the entry point function
309 * returns. The thread is responsible for releasing any shared resources
310 * it may own (such as mutexes and dynamically allocated memory), prior to
311 * returning.
312 *
313 * @param p1 First argument.
314 * @param p2 Second argument.
315 * @param p3 Third argument.
316 *
317 * @return N/A
318 */
319typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
320
Benjamin Walshed240f22017-01-22 13:05:08 -0500321#endif /* !_ASMLANGUAGE */
322
323
324/*
325 * Thread user options. May be needed by assembly code. Common part uses low
326 * bits, arch-specific use high bits.
327 */
328
329/* system thread that must not abort */
330#define K_ESSENTIAL (1 << 0)
331
332#if defined(CONFIG_FP_SHARING)
333/* thread uses floating point registers */
334#define K_FP_REGS (1 << 1)
335#endif
336
337#ifdef CONFIG_X86
338/* x86 Bitmask definitions for threads user options */
339
340#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
341/* thread uses SSEx (and also FP) registers */
342#define K_SSE_REGS (1 << 7)
343#endif
344#endif
345
346/* end - thread options */
347
348#if !defined(_ASMLANGUAGE)
349
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 {
1316 _wait_q_t wait_q;
1317 sys_slist_t data_q;
1318 _POLL_EVENT;
1319
1320 _OBJECT_TRACING_NEXT_PTR(k_queue);
1321};
1322
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001323#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001324 { \
1325 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1326 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1327 _POLL_EVENT_OBJ_INIT \
1328 _OBJECT_TRACING_INIT \
1329 }
1330
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001331#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1332
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001333/**
1334 * INTERNAL_HIDDEN @endcond
1335 */
1336
1337/**
1338 * @defgroup queue_apis Queue APIs
1339 * @ingroup kernel_apis
1340 * @{
1341 */
1342
1343/**
1344 * @brief Initialize a queue.
1345 *
1346 * This routine initializes a queue object, prior to its first use.
1347 *
1348 * @param queue Address of the queue.
1349 *
1350 * @return N/A
1351 */
1352extern void k_queue_init(struct k_queue *queue);
1353
1354/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001355 * @brief Cancel waiting on a queue.
1356 *
1357 * This routine causes first thread pending on @a queue, if any, to
1358 * return from k_queue_get() call with NULL value (as if timeout expired).
1359 *
1360 * @note Can be called by ISRs.
1361 *
1362 * @param queue Address of the queue.
1363 *
1364 * @return N/A
1365 */
1366extern void k_queue_cancel_wait(struct k_queue *queue);
1367
1368/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001369 * @brief Append an element to the end of a queue.
1370 *
1371 * This routine appends a data item to @a queue. A queue data item must be
1372 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1373 * reserved for the kernel's use.
1374 *
1375 * @note Can be called by ISRs.
1376 *
1377 * @param queue Address of the queue.
1378 * @param data Address of the data item.
1379 *
1380 * @return N/A
1381 */
1382extern void k_queue_append(struct k_queue *queue, void *data);
1383
1384/**
1385 * @brief Prepend an element to a queue.
1386 *
1387 * This routine prepends a data item to @a queue. A queue data item must be
1388 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1389 * reserved for the kernel's use.
1390 *
1391 * @note Can be called by ISRs.
1392 *
1393 * @param queue Address of the queue.
1394 * @param data Address of the data item.
1395 *
1396 * @return N/A
1397 */
1398extern void k_queue_prepend(struct k_queue *queue, void *data);
1399
1400/**
1401 * @brief Inserts an element to a queue.
1402 *
1403 * This routine inserts a data item to @a queue after previous item. A queue
1404 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1405 * item are reserved for the kernel's use.
1406 *
1407 * @note Can be called by ISRs.
1408 *
1409 * @param queue Address of the queue.
1410 * @param prev Address of the previous data item.
1411 * @param data Address of the data item.
1412 *
1413 * @return N/A
1414 */
1415extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1416
1417/**
1418 * @brief Atomically append a list of elements to a queue.
1419 *
1420 * This routine adds a list of data items to @a queue in one operation.
1421 * The data items must be in a singly-linked list, with the first 32 bits
1422 * in each data item pointing to the next data item; the list must be
1423 * NULL-terminated.
1424 *
1425 * @note Can be called by ISRs.
1426 *
1427 * @param queue Address of the queue.
1428 * @param head Pointer to first node in singly-linked list.
1429 * @param tail Pointer to last node in singly-linked list.
1430 *
1431 * @return N/A
1432 */
1433extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1434
1435/**
1436 * @brief Atomically add a list of elements to a queue.
1437 *
1438 * This routine adds a list of data items to @a queue in one operation.
1439 * The data items must be in a singly-linked list implemented using a
1440 * sys_slist_t object. Upon completion, the original list is empty.
1441 *
1442 * @note Can be called by ISRs.
1443 *
1444 * @param queue Address of the queue.
1445 * @param list Pointer to sys_slist_t object.
1446 *
1447 * @return N/A
1448 */
1449extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1450
1451/**
1452 * @brief Get an element from a queue.
1453 *
1454 * This routine removes first data item from @a queue. The first 32 bits of the
1455 * data item are reserved for the kernel's use.
1456 *
1457 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1458 *
1459 * @param queue Address of the queue.
1460 * @param timeout Waiting period to obtain a data item (in milliseconds),
1461 * or one of the special values K_NO_WAIT and K_FOREVER.
1462 *
1463 * @return Address of the data item if successful; NULL if returned
1464 * without waiting, or waiting period timed out.
1465 */
Kumar Galacc334c72017-04-21 10:55:34 -05001466extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001467
1468/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001469 * @brief Remove an element from a queue.
1470 *
1471 * This routine removes data item from @a queue. The first 32 bits of the
1472 * data item are reserved for the kernel's use. Removing elements from k_queue
1473 * rely on sys_slist_find_and_remove which is not a constant time operation.
1474 *
1475 * @note Can be called by ISRs
1476 *
1477 * @param queue Address of the queue.
1478 * @param data Address of the data item.
1479 *
1480 * @return true if data item was removed
1481 */
1482static inline bool k_queue_remove(struct k_queue *queue, void *data)
1483{
1484 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1485}
1486
1487/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001488 * @brief Query a queue to see if it has data available.
1489 *
1490 * Note that the data might be already gone by the time this function returns
1491 * if other threads are also trying to read from the queue.
1492 *
1493 * @note Can be called by ISRs.
1494 *
1495 * @param queue Address of the queue.
1496 *
1497 * @return Non-zero if the queue is empty.
1498 * @return 0 if data is available.
1499 */
1500static inline int k_queue_is_empty(struct k_queue *queue)
1501{
1502 return (int)sys_slist_is_empty(&queue->data_q);
1503}
1504
1505/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001506 * @brief Peek element at the head of queue.
1507 *
1508 * Return element from the head of queue without removing it.
1509 *
1510 * @param queue Address of the queue.
1511 *
1512 * @return Head element, or NULL if queue is empty.
1513 */
1514static inline void *k_queue_peek_head(struct k_queue *queue)
1515{
1516 return sys_slist_peek_head(&queue->data_q);
1517}
1518
1519/**
1520 * @brief Peek element at the tail of queue.
1521 *
1522 * Return element from the tail of queue without removing it.
1523 *
1524 * @param queue Address of the queue.
1525 *
1526 * @return Tail element, or NULL if queue is empty.
1527 */
1528static inline void *k_queue_peek_tail(struct k_queue *queue)
1529{
1530 return sys_slist_peek_tail(&queue->data_q);
1531}
1532
1533/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001534 * @brief Statically define and initialize a queue.
1535 *
1536 * The queue can be accessed outside the module where it is defined using:
1537 *
1538 * @code extern struct k_queue <name>; @endcode
1539 *
1540 * @param name Name of the queue.
1541 */
1542#define K_QUEUE_DEFINE(name) \
1543 struct k_queue name \
1544 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001545 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001546
1547/**
1548 * @} end defgroup queue_apis
1549 */
1550
1551/**
1552 * @cond INTERNAL_HIDDEN
1553 */
1554
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001555struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001556 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001557};
1558
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001559#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001560 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001561 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001562 }
1563
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001564#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1565
Allan Stephensc98da842016-11-11 15:45:03 -05001566/**
1567 * INTERNAL_HIDDEN @endcond
1568 */
1569
1570/**
1571 * @defgroup fifo_apis Fifo APIs
1572 * @ingroup kernel_apis
1573 * @{
1574 */
1575
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001576/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001577 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001579 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001581 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001582 *
1583 * @return N/A
1584 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001585#define k_fifo_init(fifo) \
1586 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001587
1588/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001589 * @brief Cancel waiting on a fifo.
1590 *
1591 * This routine causes first thread pending on @a fifo, if any, to
1592 * return from k_fifo_get() call with NULL value (as if timeout
1593 * expired).
1594 *
1595 * @note Can be called by ISRs.
1596 *
1597 * @param fifo Address of the fifo.
1598 *
1599 * @return N/A
1600 */
1601#define k_fifo_cancel_wait(fifo) \
1602 k_queue_cancel_wait((struct k_queue *) fifo)
1603
1604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * This routine adds a data item to @a fifo. A fifo data item must be
1608 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1609 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001611 * @note Can be called by ISRs.
1612 *
1613 * @param fifo Address of the fifo.
1614 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001615 *
1616 * @return N/A
1617 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001618#define k_fifo_put(fifo, data) \
1619 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620
1621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * This routine adds a list of data items to @a fifo in one operation.
1625 * The data items must be in a singly-linked list, with the first 32 bits
1626 * each data item pointing to the next data item; the list must be
1627 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * @param fifo Address of the fifo.
1632 * @param head Pointer to first node in singly-linked list.
1633 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001634 *
1635 * @return N/A
1636 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001637#define k_fifo_put_list(fifo, head, tail) \
1638 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001639
1640/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001641 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001643 * This routine adds a list of data items to @a fifo in one operation.
1644 * The data items must be in a singly-linked list implemented using a
1645 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001646 * and must be re-initialized via sys_slist_init().
1647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001648 * @note Can be called by ISRs.
1649 *
1650 * @param fifo Address of the fifo.
1651 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001652 *
1653 * @return N/A
1654 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001655#define k_fifo_put_slist(fifo, list) \
1656 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001657
1658/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001659 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001661 * This routine removes a data item from @a fifo in a "first in, first out"
1662 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001664 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1665 *
1666 * @param fifo Address of the fifo.
1667 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001668 * or one of the special values K_NO_WAIT and K_FOREVER.
1669 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001670 * @return Address of the data item if successful; NULL if returned
1671 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001672 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001673#define k_fifo_get(fifo, timeout) \
1674 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001675
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001676/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001677 * @brief Query a fifo to see if it has data available.
1678 *
1679 * Note that the data might be already gone by the time this function returns
1680 * if other threads is also trying to read from the fifo.
1681 *
1682 * @note Can be called by ISRs.
1683 *
1684 * @param fifo Address of the fifo.
1685 *
1686 * @return Non-zero if the fifo is empty.
1687 * @return 0 if data is available.
1688 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001689#define k_fifo_is_empty(fifo) \
1690 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001691
1692/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001693 * @brief Peek element at the head of fifo.
1694 *
1695 * Return element from the head of fifo without removing it. A usecase
1696 * for this is if elements of the fifo are themselves containers. Then
1697 * on each iteration of processing, a head container will be peeked,
1698 * and some data processed out of it, and only if the container is empty,
1699 * it will be completely remove from the fifo.
1700 *
1701 * @param fifo Address of the fifo.
1702 *
1703 * @return Head element, or NULL if the fifo is empty.
1704 */
1705#define k_fifo_peek_head(fifo) \
1706 k_queue_peek_head((struct k_queue *) fifo)
1707
1708/**
1709 * @brief Peek element at the tail of fifo.
1710 *
1711 * Return element from the tail of fifo (without removing it). A usecase
1712 * for this is if elements of the fifo are themselves containers. Then
1713 * it may be useful to add more data to the last container in fifo.
1714 *
1715 * @param fifo Address of the fifo.
1716 *
1717 * @return Tail element, or NULL if fifo is empty.
1718 */
1719#define k_fifo_peek_tail(fifo) \
1720 k_queue_peek_tail((struct k_queue *) fifo)
1721
1722/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001727 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001731#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001732 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001733 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001734 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001735
Allan Stephensc98da842016-11-11 15:45:03 -05001736/**
1737 * @} end defgroup fifo_apis
1738 */
1739
1740/**
1741 * @cond INTERNAL_HIDDEN
1742 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743
1744struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001745 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746};
1747
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001748#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001749 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001750 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001751 }
1752
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001753#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1754
Allan Stephensc98da842016-11-11 15:45:03 -05001755/**
1756 * INTERNAL_HIDDEN @endcond
1757 */
1758
1759/**
1760 * @defgroup lifo_apis Lifo APIs
1761 * @ingroup kernel_apis
1762 * @{
1763 */
1764
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001770 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771 *
1772 * @return N/A
1773 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001774#define k_lifo_init(lifo) \
1775 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776
1777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * This routine adds a data item to @a lifo. A lifo data item must be
1781 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1782 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001784 * @note Can be called by ISRs.
1785 *
1786 * @param lifo Address of the lifo.
1787 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001788 *
1789 * @return N/A
1790 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001791#define k_lifo_put(lifo, data) \
1792 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793
1794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001795 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * This routine removes a data item from @a lifo in a "last in, first out"
1798 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1801 *
1802 * @param lifo Address of the lifo.
1803 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804 * or one of the special values K_NO_WAIT and K_FOREVER.
1805 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001806 * @return Address of the data item if successful; NULL if returned
1807 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001808 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001809#define k_lifo_get(lifo, timeout) \
1810 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001811
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001813 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001817 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001819 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001820 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001822 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001823 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001824 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825
Allan Stephensc98da842016-11-11 15:45:03 -05001826/**
1827 * @} end defgroup lifo_apis
1828 */
1829
1830/**
1831 * @cond INTERNAL_HIDDEN
1832 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001833
1834struct k_stack {
1835 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001836 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837
Anas Nashif2f203c22016-12-18 06:57:45 -05001838 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001839};
1840
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001841#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001842 { \
1843 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1844 .base = stack_buffer, \
1845 .next = stack_buffer, \
1846 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001847 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001848 }
1849
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001850#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1851
Allan Stephensc98da842016-11-11 15:45:03 -05001852/**
1853 * INTERNAL_HIDDEN @endcond
1854 */
1855
1856/**
1857 * @defgroup stack_apis Stack APIs
1858 * @ingroup kernel_apis
1859 * @{
1860 */
1861
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862/**
1863 * @brief Initialize a stack.
1864 *
1865 * This routine initializes a stack object, prior to its first use.
1866 *
1867 * @param stack Address of the stack.
1868 * @param buffer Address of array used to hold stacked values.
1869 * @param num_entries Maximum number of values that can be stacked.
1870 *
1871 * @return N/A
1872 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001873extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001874 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875
1876/**
1877 * @brief Push an element onto a stack.
1878 *
1879 * This routine adds a 32-bit value @a data to @a stack.
1880 *
1881 * @note Can be called by ISRs.
1882 *
1883 * @param stack Address of the stack.
1884 * @param data Value to push onto the stack.
1885 *
1886 * @return N/A
1887 */
Kumar Galacc334c72017-04-21 10:55:34 -05001888extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889
1890/**
1891 * @brief Pop an element from a stack.
1892 *
1893 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1894 * manner and stores the value in @a data.
1895 *
1896 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1897 *
1898 * @param stack Address of the stack.
1899 * @param data Address of area to hold the value popped from the stack.
1900 * @param timeout Waiting period to obtain a value (in milliseconds),
1901 * or one of the special values K_NO_WAIT and K_FOREVER.
1902 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001903 * @retval 0 Element popped from stack.
1904 * @retval -EBUSY Returned without waiting.
1905 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001906 */
Kumar Galacc334c72017-04-21 10:55:34 -05001907extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001908
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001909/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001910 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001912 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001913 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001914 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001916 * @param name Name of the stack.
1917 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001919#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001920 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001921 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001922 struct k_stack name \
1923 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001924 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001925 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001926
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927/**
Allan Stephensc98da842016-11-11 15:45:03 -05001928 * @} end defgroup stack_apis
1929 */
1930
Allan Stephens6bba9b02016-11-16 14:56:54 -05001931struct k_work;
1932
Allan Stephensc98da842016-11-11 15:45:03 -05001933/**
1934 * @defgroup workqueue_apis Workqueue Thread APIs
1935 * @ingroup kernel_apis
1936 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937 */
1938
Allan Stephens6bba9b02016-11-16 14:56:54 -05001939/**
1940 * @typedef k_work_handler_t
1941 * @brief Work item handler function type.
1942 *
1943 * A work item's handler function is executed by a workqueue's thread
1944 * when the work item is processed by the workqueue.
1945 *
1946 * @param work Address of the work item.
1947 *
1948 * @return N/A
1949 */
1950typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951
1952/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001953 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001954 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001955
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001956struct k_work_q {
1957 struct k_fifo fifo;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001958 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959};
1960
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001962 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963};
1964
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001965struct k_work {
1966 void *_reserved; /* Used by k_fifo implementation. */
1967 k_work_handler_t handler;
1968 atomic_t flags[1];
1969};
1970
Allan Stephens6bba9b02016-11-16 14:56:54 -05001971struct k_delayed_work {
1972 struct k_work work;
1973 struct _timeout timeout;
1974 struct k_work_q *work_q;
1975};
1976
1977extern struct k_work_q k_sys_work_q;
1978
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001979/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001980 * INTERNAL_HIDDEN @endcond
1981 */
1982
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001983#define _K_WORK_INITIALIZER(work_handler) \
1984 { \
1985 ._reserved = NULL, \
1986 .handler = work_handler, \
1987 .flags = { 0 } \
1988 }
1989
1990#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
1991
Allan Stephens6bba9b02016-11-16 14:56:54 -05001992/**
1993 * @brief Initialize a statically-defined work item.
1994 *
1995 * This macro can be used to initialize a statically-defined workqueue work
1996 * item, prior to its first use. For example,
1997 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001998 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05001999 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002000 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002001 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002002 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002003#define K_WORK_DEFINE(work, work_handler) \
2004 struct k_work work \
2005 __in_section(_k_work, static, work) = \
2006 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007
2008/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002009 * @brief Initialize a work item.
2010 *
2011 * This routine initializes a workqueue work item, prior to its first use.
2012 *
2013 * @param work Address of work item.
2014 * @param handler Function to invoke each time work item is processed.
2015 *
2016 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017 */
2018static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2019{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002020 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021 work->handler = handler;
2022}
2023
2024/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002025 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002026 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002027 * This routine submits work item @a work to be processed by workqueue
2028 * @a work_q. If the work item is already pending in the workqueue's queue
2029 * as a result of an earlier submission, this routine has no effect on the
2030 * work item. If the work item has already been processed, or is currently
2031 * being processed, its work is considered complete and the work item can be
2032 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002033 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002034 * @warning
2035 * A submitted work item must not be modified until it has been processed
2036 * by the workqueue.
2037 *
2038 * @note Can be called by ISRs.
2039 *
2040 * @param work_q Address of workqueue.
2041 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002042 *
2043 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044 */
2045static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2046 struct k_work *work)
2047{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002048 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002049 k_fifo_put(&work_q->fifo, work);
2050 }
2051}
2052
2053/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002054 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002056 * This routine indicates if work item @a work is pending in a workqueue's
2057 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002059 * @note Can be called by ISRs.
2060 *
2061 * @param work Address of work item.
2062 *
2063 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002064 */
2065static inline int k_work_pending(struct k_work *work)
2066{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002067 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002068}
2069
2070/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002071 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002073 * This routine starts workqueue @a work_q. The workqueue spawns its work
2074 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002076 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002077 * @param stack Pointer to work queue thread's stack space, as defined by
2078 * K_THREAD_STACK_DEFINE()
2079 * @param stack_size Size of the work queue thread's stack (in bytes), which
2080 * should either be the same constant passed to
2081 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002082 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083 *
2084 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085 */
Andrew Boie507852a2017-07-25 18:47:07 -07002086extern void k_work_q_start(struct k_work_q *work_q,
2087 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002088 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002091 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002093 * This routine initializes a workqueue delayed work item, prior to
2094 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002095 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002096 * @param work Address of delayed work item.
2097 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 *
2099 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002100 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002101extern void k_delayed_work_init(struct k_delayed_work *work,
2102 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002103
2104/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002105 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002106 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002107 * This routine schedules work item @a work to be processed by workqueue
2108 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002109 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002110 * Only when the countdown completes is the work item actually submitted to
2111 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002113 * Submitting a previously submitted delayed work item that is still
2114 * counting down cancels the existing submission and restarts the countdown
2115 * using the new delay. If the work item is currently pending on the
2116 * workqueue's queue because the countdown has completed it is too late to
2117 * resubmit the item, and resubmission fails without impacting the work item.
2118 * If the work item has already been processed, or is currently being processed,
2119 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002121 * @warning
2122 * A delayed work item must not be modified until it has been processed
2123 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002124 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002125 * @note Can be called by ISRs.
2126 *
2127 * @param work_q Address of workqueue.
2128 * @param work Address of delayed work item.
2129 * @param delay Delay before submitting the work item (in milliseconds).
2130 *
2131 * @retval 0 Work item countdown started.
2132 * @retval -EINPROGRESS Work item is already pending.
2133 * @retval -EINVAL Work item is being processed or has completed its work.
2134 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002135 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002136extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2137 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002138 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139
2140/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002141 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002142 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002143 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002144 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002145 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002147 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002149 * @param work Address of delayed work item.
2150 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002151 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002152 * @retval -EINPROGRESS Work item is already pending.
2153 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002155extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002157/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 * @brief Submit a work item to the system workqueue.
2159 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002160 * This routine submits work item @a work to be processed by the system
2161 * workqueue. If the work item is already pending in the workqueue's queue
2162 * as a result of an earlier submission, this routine has no effect on the
2163 * work item. If the work item has already been processed, or is currently
2164 * being processed, its work is considered complete and the work item can be
2165 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002166 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002167 * @warning
2168 * Work items submitted to the system workqueue should avoid using handlers
2169 * that block or yield since this may prevent the system workqueue from
2170 * processing other work items in a timely manner.
2171 *
2172 * @note Can be called by ISRs.
2173 *
2174 * @param work Address of work item.
2175 *
2176 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177 */
2178static inline void k_work_submit(struct k_work *work)
2179{
2180 k_work_submit_to_queue(&k_sys_work_q, work);
2181}
2182
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184 * @brief Submit a delayed work item to the system workqueue.
2185 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002186 * This routine schedules work item @a work to be processed by the system
2187 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002188 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189 * Only when the countdown completes is the work item actually submitted to
2190 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 * Submitting a previously submitted delayed work item that is still
2193 * counting down cancels the existing submission and restarts the countdown
2194 * using the new delay. If the work item is currently pending on the
2195 * workqueue's queue because the countdown has completed it is too late to
2196 * resubmit the item, and resubmission fails without impacting the work item.
2197 * If the work item has already been processed, or is currently being processed,
2198 * its work is considered complete and the work item can be resubmitted.
2199 *
2200 * @warning
2201 * Work items submitted to the system workqueue should avoid using handlers
2202 * that block or yield since this may prevent the system workqueue from
2203 * processing other work items in a timely manner.
2204 *
2205 * @note Can be called by ISRs.
2206 *
2207 * @param work Address of delayed work item.
2208 * @param delay Delay before submitting the work item (in milliseconds).
2209 *
2210 * @retval 0 Work item countdown started.
2211 * @retval -EINPROGRESS Work item is already pending.
2212 * @retval -EINVAL Work item is being processed or has completed its work.
2213 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214 */
2215static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002216 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002218 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219}
2220
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002222 * @brief Get time remaining before a delayed work gets scheduled.
2223 *
2224 * This routine computes the (approximate) time remaining before a
2225 * delayed work gets executed. If the delayed work is not waiting to be
2226 * schedules, it returns zero.
2227 *
2228 * @param work Delayed work item.
2229 *
2230 * @return Remaining time (in milliseconds).
2231 */
Kumar Galacc334c72017-04-21 10:55:34 -05002232static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002233{
2234 return _timeout_remaining_get(&work->timeout);
2235}
2236
2237/**
Allan Stephensc98da842016-11-11 15:45:03 -05002238 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239 */
2240
Allan Stephensc98da842016-11-11 15:45:03 -05002241/**
2242 * @cond INTERNAL_HIDDEN
2243 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244
2245struct k_mutex {
2246 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002247 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002248 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250
Anas Nashif2f203c22016-12-18 06:57:45 -05002251 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252};
2253
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002254#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255 { \
2256 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2257 .owner = NULL, \
2258 .lock_count = 0, \
2259 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002260 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002261 }
2262
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002263#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2264
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002265/**
Allan Stephensc98da842016-11-11 15:45:03 -05002266 * INTERNAL_HIDDEN @endcond
2267 */
2268
2269/**
2270 * @defgroup mutex_apis Mutex APIs
2271 * @ingroup kernel_apis
2272 * @{
2273 */
2274
2275/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002276 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002278 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002280 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002282 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002285 struct k_mutex name \
2286 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002287 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002288
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002292 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002294 * Upon completion, the mutex is available and does not have an owner.
2295 *
2296 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
2298 * @return N/A
2299 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301
2302/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002303 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002305 * This routine locks @a mutex. If the mutex is locked by another thread,
2306 * the calling thread waits until the mutex becomes available or until
2307 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002309 * A thread is permitted to lock a mutex it has already locked. The operation
2310 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002312 * @param mutex Address of the mutex.
2313 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 * or one of the special values K_NO_WAIT and K_FOREVER.
2315 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002316 * @retval 0 Mutex locked.
2317 * @retval -EBUSY Returned without waiting.
2318 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319 */
Kumar Galacc334c72017-04-21 10:55:34 -05002320extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321
2322/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * This routine unlocks @a mutex. The mutex must already be locked by the
2326 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
2328 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330 * thread.
2331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
2334 * @return N/A
2335 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336extern void k_mutex_unlock(struct k_mutex *mutex);
2337
Allan Stephensc98da842016-11-11 15:45:03 -05002338/**
2339 * @} end defgroup mutex_apis
2340 */
2341
2342/**
2343 * @cond INTERNAL_HIDDEN
2344 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345
2346struct k_sem {
2347 _wait_q_t wait_q;
2348 unsigned int count;
2349 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002350 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351
Anas Nashif2f203c22016-12-18 06:57:45 -05002352 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353};
2354
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002355#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002356 { \
2357 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2358 .count = initial_count, \
2359 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002360 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002361 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002362 }
2363
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002364#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2365
Allan Stephensc98da842016-11-11 15:45:03 -05002366/**
2367 * INTERNAL_HIDDEN @endcond
2368 */
2369
2370/**
2371 * @defgroup semaphore_apis Semaphore APIs
2372 * @ingroup kernel_apis
2373 * @{
2374 */
2375
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * @param sem Address of the semaphore.
2382 * @param initial_count Initial semaphore count.
2383 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002384 *
2385 * @return N/A
2386 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2388 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002389
2390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2396 *
2397 * @param sem Address of the semaphore.
2398 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002399 * or one of the special values K_NO_WAIT and K_FOREVER.
2400 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002401 * @note When porting code from the nanokernel legacy API to the new API, be
2402 * careful with the return value of this function. The return value is the
2403 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2404 * non-zero means failure, while the nano_sem_take family returns 1 for success
2405 * and 0 for failure.
2406 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002407 * @retval 0 Semaphore taken.
2408 * @retval -EBUSY Returned without waiting.
2409 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002410 */
Kumar Galacc334c72017-04-21 10:55:34 -05002411extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002412
2413/**
2414 * @brief Give a semaphore.
2415 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * This routine gives @a sem, unless the semaphore is already at its maximum
2417 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002422 *
2423 * @return N/A
2424 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002425extern void k_sem_give(struct k_sem *sem);
2426
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002427/**
2428 * @brief Reset a semaphore's count to zero.
2429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002433 *
2434 * @return N/A
2435 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002436static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437{
2438 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439}
2440
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002441/**
2442 * @brief Get a semaphore's count.
2443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002444 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002449 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002450static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451{
2452 return sem->count;
2453}
2454
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002458 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002459 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002460 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002462 * @param name Name of the semaphore.
2463 * @param initial_count Initial semaphore count.
2464 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002465 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002467 struct k_sem name \
2468 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002469 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470
Allan Stephensc98da842016-11-11 15:45:03 -05002471/**
2472 * @} end defgroup semaphore_apis
2473 */
2474
2475/**
2476 * @defgroup alert_apis Alert APIs
2477 * @ingroup kernel_apis
2478 * @{
2479 */
2480
Allan Stephens5eceb852016-11-16 10:16:30 -05002481/**
2482 * @typedef k_alert_handler_t
2483 * @brief Alert handler function type.
2484 *
2485 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002486 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002487 * and is only invoked if the alert has been initialized with one.
2488 *
2489 * @param alert Address of the alert.
2490 *
2491 * @return 0 if alert has been consumed; non-zero if alert should pend.
2492 */
2493typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002494
2495/**
2496 * @} end defgroup alert_apis
2497 */
2498
2499/**
2500 * @cond INTERNAL_HIDDEN
2501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002503#define K_ALERT_DEFAULT NULL
2504#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002506struct k_alert {
2507 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508 atomic_t send_count;
2509 struct k_work work_item;
2510 struct k_sem sem;
2511
Anas Nashif2f203c22016-12-18 06:57:45 -05002512 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513};
2514
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002515extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002517#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002518 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002519 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002521 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2522 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002523 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524 }
2525
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002526#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2527
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528/**
Allan Stephensc98da842016-11-11 15:45:03 -05002529 * INTERNAL_HIDDEN @endcond
2530 */
2531
2532/**
2533 * @addtogroup alert_apis
2534 * @{
2535 */
2536
2537/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002540 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002542 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * @param name Name of the alert.
2545 * @param alert_handler Action to take when alert is sent. Specify either
2546 * the address of a function to be invoked by the system workqueue
2547 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2548 * K_ALERT_DEFAULT (which causes the alert to pend).
2549 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002550 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002551#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002552 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002553 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002554 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002555 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002562 * @param alert Address of the alert.
2563 * @param handler Action to take when alert is sent. Specify either the address
2564 * of a function to be invoked by the system workqueue thread,
2565 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2566 * K_ALERT_DEFAULT (which causes the alert to pend).
2567 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002568 *
2569 * @return N/A
2570 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002571extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2572 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002573
2574/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2580 *
2581 * @param alert Address of the alert.
2582 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002583 * or one of the special values K_NO_WAIT and K_FOREVER.
2584 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002585 * @retval 0 Alert received.
2586 * @retval -EBUSY Returned without waiting.
2587 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002588 */
Kumar Galacc334c72017-04-21 10:55:34 -05002589extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002590
2591/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002592 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * This routine signals @a alert. The action specified for @a alert will
2595 * be taken, which may trigger the execution of an alert handler function
2596 * and/or cause the alert to pend (assuming the alert has not reached its
2597 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * @note Can be called by ISRs.
2600 *
2601 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002602 *
2603 * @return N/A
2604 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002605extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002606
2607/**
Allan Stephensc98da842016-11-11 15:45:03 -05002608 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609 */
2610
Allan Stephensc98da842016-11-11 15:45:03 -05002611/**
2612 * @cond INTERNAL_HIDDEN
2613 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614
2615struct k_msgq {
2616 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002617 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002618 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002619 char *buffer_start;
2620 char *buffer_end;
2621 char *read_ptr;
2622 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002623 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624
Anas Nashif2f203c22016-12-18 06:57:45 -05002625 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002626};
2627
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002628#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629 { \
2630 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002631 .max_msgs = q_max_msgs, \
2632 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002634 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635 .read_ptr = q_buffer, \
2636 .write_ptr = q_buffer, \
2637 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002638 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639 }
2640
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002641#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2642
Peter Mitsis1da807e2016-10-06 11:36:59 -04002643/**
Allan Stephensc98da842016-11-11 15:45:03 -05002644 * INTERNAL_HIDDEN @endcond
2645 */
2646
2647/**
2648 * @defgroup msgq_apis Message Queue APIs
2649 * @ingroup kernel_apis
2650 * @{
2651 */
2652
2653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2657 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002658 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2659 * message is similarly aligned to this boundary, @a q_msg_size must also be
2660 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 * The message queue can be accessed outside the module where it is defined
2663 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002665 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @param q_name Name of the message queue.
2668 * @param q_msg_size Message size (in bytes).
2669 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002670 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002671 */
2672#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2673 static char __noinit __aligned(q_align) \
2674 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002675 struct k_msgq q_name \
2676 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002677 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002678 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679
Peter Mitsisd7a37502016-10-13 11:37:40 -04002680/**
2681 * @brief Initialize a message queue.
2682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * This routine initializes a message queue object, prior to its first use.
2684 *
Allan Stephensda827222016-11-09 14:23:58 -06002685 * The message queue's ring buffer must contain space for @a max_msgs messages,
2686 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2687 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2688 * that each message is similarly aligned to this boundary, @a q_msg_size
2689 * must also be a multiple of N.
2690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002691 * @param q Address of the message queue.
2692 * @param buffer Pointer to ring buffer that holds queued messages.
2693 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002694 * @param max_msgs Maximum number of messages that can be queued.
2695 *
2696 * @return N/A
2697 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002698extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002699 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002700
2701/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002705 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002706 * @note Can be called by ISRs.
2707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 * @param q Address of the message queue.
2709 * @param data Pointer to the message.
2710 * @param timeout Waiting period to add the message (in milliseconds),
2711 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002712 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002713 * @retval 0 Message sent.
2714 * @retval -ENOMSG Returned without waiting or queue purged.
2715 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002716 */
Kumar Galacc334c72017-04-21 10:55:34 -05002717extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002718
2719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * This routine receives a message from message queue @a q in a "first in,
2723 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002724 *
Allan Stephensc98da842016-11-11 15:45:03 -05002725 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * @param q Address of the message queue.
2728 * @param data Address of area to hold the received message.
2729 * @param timeout Waiting period to receive the message (in milliseconds),
2730 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002731 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002732 * @retval 0 Message received.
2733 * @retval -ENOMSG Returned without waiting.
2734 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002735 */
Kumar Galacc334c72017-04-21 10:55:34 -05002736extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002737
2738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * This routine discards all unreceived messages in a message queue's ring
2742 * buffer. Any threads that are blocked waiting to send a message to the
2743 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002746 *
2747 * @return N/A
2748 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749extern void k_msgq_purge(struct k_msgq *q);
2750
Peter Mitsis67be2492016-10-07 11:44:34 -04002751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 * This routine returns the number of unused entries in a message queue's
2755 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * @param q Address of the message queue.
2758 *
2759 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002760 */
Kumar Galacc334c72017-04-21 10:55:34 -05002761static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002762{
2763 return q->max_msgs - q->used_msgs;
2764}
2765
Peter Mitsisd7a37502016-10-13 11:37:40 -04002766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002767 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002771 * @param q Address of the message queue.
2772 *
2773 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002774 */
Kumar Galacc334c72017-04-21 10:55:34 -05002775static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776{
2777 return q->used_msgs;
2778}
2779
Allan Stephensc98da842016-11-11 15:45:03 -05002780/**
2781 * @} end defgroup msgq_apis
2782 */
2783
2784/**
2785 * @defgroup mem_pool_apis Memory Pool APIs
2786 * @ingroup kernel_apis
2787 * @{
2788 */
2789
Andy Ross73cb9582017-05-09 10:42:39 -07002790/* Note on sizing: the use of a 20 bit field for block means that,
2791 * assuming a reasonable minimum block size of 16 bytes, we're limited
2792 * to 16M of memory managed by a single pool. Long term it would be
2793 * good to move to a variable bit size based on configuration.
2794 */
2795struct k_mem_block_id {
2796 u32_t pool : 8;
2797 u32_t level : 4;
2798 u32_t block : 20;
2799};
2800
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002803 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804};
2805
Allan Stephensc98da842016-11-11 15:45:03 -05002806/**
2807 * @} end defgroup mem_pool_apis
2808 */
2809
2810/**
2811 * @defgroup mailbox_apis Mailbox APIs
2812 * @ingroup kernel_apis
2813 * @{
2814 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815
2816struct k_mbox_msg {
2817 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002818 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002820 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002822 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823 /** sender's message data buffer */
2824 void *tx_data;
2825 /** internal use only - needed for legacy API support */
2826 void *_rx_data;
2827 /** message data block descriptor */
2828 struct k_mem_block tx_block;
2829 /** source thread id */
2830 k_tid_t rx_source_thread;
2831 /** target thread id */
2832 k_tid_t tx_target_thread;
2833 /** internal use only - thread waiting on send (may be a dummy) */
2834 k_tid_t _syncing_thread;
2835#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2836 /** internal use only - semaphore used during asynchronous send */
2837 struct k_sem *_async_sem;
2838#endif
2839};
2840
Allan Stephensc98da842016-11-11 15:45:03 -05002841/**
2842 * @cond INTERNAL_HIDDEN
2843 */
2844
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845struct k_mbox {
2846 _wait_q_t tx_msg_queue;
2847 _wait_q_t rx_msg_queue;
2848
Anas Nashif2f203c22016-12-18 06:57:45 -05002849 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002850};
2851
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002852#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002853 { \
2854 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2855 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002856 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002857 }
2858
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002859#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2860
Peter Mitsis12092702016-10-14 12:57:23 -04002861/**
Allan Stephensc98da842016-11-11 15:45:03 -05002862 * INTERNAL_HIDDEN @endcond
2863 */
2864
2865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002870 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002873 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002875 struct k_mbox name \
2876 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002877 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878
Peter Mitsis12092702016-10-14 12:57:23 -04002879/**
2880 * @brief Initialize a mailbox.
2881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * This routine initializes a mailbox object, prior to its first use.
2883 *
2884 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002885 *
2886 * @return N/A
2887 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888extern void k_mbox_init(struct k_mbox *mbox);
2889
Peter Mitsis12092702016-10-14 12:57:23 -04002890/**
2891 * @brief Send a mailbox message in a synchronous manner.
2892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * This routine sends a message to @a mbox and waits for a receiver to both
2894 * receive and process it. The message data may be in a buffer, in a memory
2895 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * @param mbox Address of the mailbox.
2898 * @param tx_msg Address of the transmit message descriptor.
2899 * @param timeout Waiting period for the message to be received (in
2900 * milliseconds), or one of the special values K_NO_WAIT
2901 * and K_FOREVER. Once the message has been received,
2902 * this routine waits as long as necessary for the message
2903 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002904 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002905 * @retval 0 Message sent.
2906 * @retval -ENOMSG Returned without waiting.
2907 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002908 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002909extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002910 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002911
Peter Mitsis12092702016-10-14 12:57:23 -04002912/**
2913 * @brief Send a mailbox message in an asynchronous manner.
2914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * This routine sends a message to @a mbox without waiting for a receiver
2916 * to process it. The message data may be in a buffer, in a memory pool block,
2917 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2918 * will be given when the message has been both received and completely
2919 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * @param mbox Address of the mailbox.
2922 * @param tx_msg Address of the transmit message descriptor.
2923 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002924 *
2925 * @return N/A
2926 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002927extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928 struct k_sem *sem);
2929
Peter Mitsis12092702016-10-14 12:57:23 -04002930/**
2931 * @brief Receive a mailbox message.
2932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * This routine receives a message from @a mbox, then optionally retrieves
2934 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002936 * @param mbox Address of the mailbox.
2937 * @param rx_msg Address of the receive message descriptor.
2938 * @param buffer Address of the buffer to receive data, or NULL to defer data
2939 * retrieval and message disposal until later.
2940 * @param timeout Waiting period for a message to be received (in
2941 * milliseconds), or one of the special values K_NO_WAIT
2942 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002943 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002944 * @retval 0 Message received.
2945 * @retval -ENOMSG Returned without waiting.
2946 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002947 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002948extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002949 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002950
2951/**
2952 * @brief Retrieve mailbox message data into a buffer.
2953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine completes the processing of a received message by retrieving
2955 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002956 *
2957 * Alternatively, this routine can be used to dispose of a received message
2958 * without retrieving its data.
2959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * @param rx_msg Address of the receive message descriptor.
2961 * @param buffer Address of the buffer to receive data, or NULL to discard
2962 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002963 *
2964 * @return N/A
2965 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002966extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002967
2968/**
2969 * @brief Retrieve mailbox message data into a memory pool block.
2970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * This routine completes the processing of a received message by retrieving
2972 * its data into a memory pool block, then disposing of the message.
2973 * The memory pool block that results from successful retrieval must be
2974 * returned to the pool once the data has been processed, even in cases
2975 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002976 *
2977 * Alternatively, this routine can be used to dispose of a received message
2978 * without retrieving its data. In this case there is no need to return a
2979 * memory pool block to the pool.
2980 *
2981 * This routine allocates a new memory pool block for the data only if the
2982 * data is not already in one. If a new block cannot be allocated, the routine
2983 * returns a failure code and the received message is left unchanged. This
2984 * permits the caller to reattempt data retrieval at a later time or to dispose
2985 * of the received message without retrieving its data.
2986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * @param rx_msg Address of a receive message descriptor.
2988 * @param pool Address of memory pool, or NULL to discard data.
2989 * @param block Address of the area to hold memory pool block info.
2990 * @param timeout Waiting period to wait for a memory pool block (in
2991 * milliseconds), or one of the special values K_NO_WAIT
2992 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002993 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002994 * @retval 0 Data retrieved.
2995 * @retval -ENOMEM Returned without waiting.
2996 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002997 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002998extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002999 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003000 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001
Allan Stephensc98da842016-11-11 15:45:03 -05003002/**
3003 * @} end defgroup mailbox_apis
3004 */
3005
3006/**
3007 * @cond INTERNAL_HIDDEN
3008 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009
3010struct k_pipe {
3011 unsigned char *buffer; /* Pipe buffer: may be NULL */
3012 size_t size; /* Buffer size */
3013 size_t bytes_used; /* # bytes used in buffer */
3014 size_t read_index; /* Where in buffer to read from */
3015 size_t write_index; /* Where in buffer to write */
3016
3017 struct {
3018 _wait_q_t readers; /* Reader wait queue */
3019 _wait_q_t writers; /* Writer wait queue */
3020 } wait_q;
3021
Anas Nashif2f203c22016-12-18 06:57:45 -05003022 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023};
3024
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003025#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026 { \
3027 .buffer = pipe_buffer, \
3028 .size = pipe_buffer_size, \
3029 .bytes_used = 0, \
3030 .read_index = 0, \
3031 .write_index = 0, \
3032 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3033 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003034 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003035 }
3036
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003037#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3038
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003039/**
Allan Stephensc98da842016-11-11 15:45:03 -05003040 * INTERNAL_HIDDEN @endcond
3041 */
3042
3043/**
3044 * @defgroup pipe_apis Pipe APIs
3045 * @ingroup kernel_apis
3046 * @{
3047 */
3048
3049/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003053 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003054 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * @param name Name of the pipe.
3057 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3058 * or zero if no ring buffer is used.
3059 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003060 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003061#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3062 static unsigned char __noinit __aligned(pipe_align) \
3063 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003064 struct k_pipe name \
3065 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003066 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * This routine initializes a pipe object, prior to its first use.
3072 *
3073 * @param pipe Address of the pipe.
3074 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3075 * is used.
3076 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3077 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078 *
3079 * @return N/A
3080 */
3081extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3082 size_t size);
3083
3084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003087 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @param pipe Address of the pipe.
3090 * @param data Address of data to write.
3091 * @param bytes_to_write Size of data (in bytes).
3092 * @param bytes_written Address of area to hold the number of bytes written.
3093 * @param min_xfer Minimum number of bytes to write.
3094 * @param timeout Waiting period to wait for the data to be written (in
3095 * milliseconds), or one of the special values K_NO_WAIT
3096 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003098 * @retval 0 At least @a min_xfer bytes of data were written.
3099 * @retval -EIO Returned without waiting; zero data bytes were written.
3100 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003103extern int k_pipe_put(struct k_pipe *pipe, void *data,
3104 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003105 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106
3107/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003108 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * @param pipe Address of the pipe.
3113 * @param data Address to place the data read from pipe.
3114 * @param bytes_to_read Maximum number of data bytes to read.
3115 * @param bytes_read Address of area to hold the number of bytes read.
3116 * @param min_xfer Minimum number of data bytes to read.
3117 * @param timeout Waiting period to wait for the data to be read (in
3118 * milliseconds), or one of the special values K_NO_WAIT
3119 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003120 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003121 * @retval 0 At least @a min_xfer bytes of data were read.
3122 * @retval -EIO Returned without waiting; zero data bytes were read.
3123 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003125 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003126extern int k_pipe_get(struct k_pipe *pipe, void *data,
3127 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003128 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129
3130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * This routine writes the data contained in a memory block to @a pipe.
3134 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138 * @param block Memory block containing data to send
3139 * @param size Number of data bytes in memory block to send
3140 * @param sem Semaphore to signal upon completion (else NULL)
3141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003143 */
3144extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3145 size_t size, struct k_sem *sem);
3146
3147/**
Allan Stephensc98da842016-11-11 15:45:03 -05003148 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149 */
3150
Allan Stephensc98da842016-11-11 15:45:03 -05003151/**
3152 * @cond INTERNAL_HIDDEN
3153 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003155struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003157 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003158 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159 char *buffer;
3160 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003161 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162
Anas Nashif2f203c22016-12-18 06:57:45 -05003163 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003164};
3165
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003166#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003167 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003168 { \
3169 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003170 .num_blocks = slab_num_blocks, \
3171 .block_size = slab_block_size, \
3172 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173 .free_list = NULL, \
3174 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003175 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176 }
3177
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003178#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3179
3180
Peter Mitsis578f9112016-10-07 13:50:31 -04003181/**
Allan Stephensc98da842016-11-11 15:45:03 -05003182 * INTERNAL_HIDDEN @endcond
3183 */
3184
3185/**
3186 * @defgroup mem_slab_apis Memory Slab APIs
3187 * @ingroup kernel_apis
3188 * @{
3189 */
3190
3191/**
Allan Stephensda827222016-11-09 14:23:58 -06003192 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003193 *
Allan Stephensda827222016-11-09 14:23:58 -06003194 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003196 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3197 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003198 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003199 *
Allan Stephensda827222016-11-09 14:23:58 -06003200 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003202 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003203 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @param name Name of the memory slab.
3206 * @param slab_block_size Size of each memory block (in bytes).
3207 * @param slab_num_blocks Number memory blocks.
3208 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003209 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003210#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3211 char __noinit __aligned(slab_align) \
3212 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3213 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003214 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003215 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003216 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003218/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003219 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003222 *
Allan Stephensda827222016-11-09 14:23:58 -06003223 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3224 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3225 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3226 * To ensure that each memory block is similarly aligned to this boundary,
3227 * @a slab_block_size must also be a multiple of N.
3228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @param slab Address of the memory slab.
3230 * @param buffer Pointer to buffer used for the memory blocks.
3231 * @param block_size Size of each memory block (in bytes).
3232 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003233 *
3234 * @return N/A
3235 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003236extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003237 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003238
3239/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003240 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @param slab Address of the memory slab.
3245 * @param mem Pointer to block address area.
3246 * @param timeout Maximum time to wait for operation to complete
3247 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3248 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003249 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003250 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003252 * @retval -ENOMEM Returned without waiting.
3253 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003254 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003255extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003256 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003257
3258/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003259 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003261 * This routine releases a previously allocated memory block back to its
3262 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * @param slab Address of the memory slab.
3265 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003266 *
3267 * @return N/A
3268 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003269extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003271/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * This routine gets the number of memory blocks that are currently
3275 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003280 */
Kumar Galacc334c72017-04-21 10:55:34 -05003281static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003283 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003284}
3285
Peter Mitsisc001aa82016-10-13 13:53:37 -04003286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003289 * This routine gets the number of memory blocks that are currently
3290 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003295 */
Kumar Galacc334c72017-04-21 10:55:34 -05003296static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003297{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003298 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003299}
3300
Allan Stephensc98da842016-11-11 15:45:03 -05003301/**
3302 * @} end defgroup mem_slab_apis
3303 */
3304
3305/**
3306 * @cond INTERNAL_HIDDEN
3307 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308
Andy Ross73cb9582017-05-09 10:42:39 -07003309struct k_mem_pool_lvl {
3310 union {
3311 u32_t *bits_p;
3312 u32_t bits;
3313 };
3314 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003315};
3316
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003317struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003318 void *buf;
3319 size_t max_sz;
3320 u16_t n_max;
3321 u8_t n_levels;
3322 u8_t max_inline_level;
3323 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003324 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003325};
3326
Andy Ross73cb9582017-05-09 10:42:39 -07003327#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328
Andy Ross73cb9582017-05-09 10:42:39 -07003329#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3330
3331#define _MPOOL_LVLS(maxsz, minsz) \
3332 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3333 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3334 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3335 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3336 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3337 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3338 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3339 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3340 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3341 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3342 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3343 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3344 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3345 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3346 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3347 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3348
3349/* Rounds the needed bits up to integer multiples of u32_t */
3350#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3351 ((((n_max) << (2*(l))) + 31) / 32)
3352
3353/* One word gets stored free unioned with the pointer, otherwise the
3354 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003355 */
Andy Ross73cb9582017-05-09 10:42:39 -07003356#define _MPOOL_LBIT_WORDS(n_max, l) \
3357 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3358 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003359
Andy Ross73cb9582017-05-09 10:42:39 -07003360/* How many bytes for the bitfields of a single level? */
3361#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3362 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3363 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003364
Andy Ross73cb9582017-05-09 10:42:39 -07003365/* Size of the bitmap array that follows the buffer in allocated memory */
3366#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3367 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3368 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3369 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3370 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3371 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3372 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3373 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3374 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3375 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3376 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3377 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3378 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3379 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3380 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3381 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3382 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003383
3384/**
Allan Stephensc98da842016-11-11 15:45:03 -05003385 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003386 */
3387
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003388/**
Allan Stephensc98da842016-11-11 15:45:03 -05003389 * @addtogroup mem_pool_apis
3390 * @{
3391 */
3392
3393/**
3394 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3397 * long. The memory pool allows blocks to be repeatedly partitioned into
3398 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003399 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003400 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003401 * If the pool is to be accessed outside the module where it is defined, it
3402 * can be declared via
3403 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003404 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003407 * @param minsz Size of the smallest blocks in the pool (in bytes).
3408 * @param maxsz Size of the largest blocks in the pool (in bytes).
3409 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003411 */
Andy Ross73cb9582017-05-09 10:42:39 -07003412#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3413 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3414 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3415 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3416 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3417 .buf = _mpool_buf_##name, \
3418 .max_sz = maxsz, \
3419 .n_max = nmax, \
3420 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3421 .levels = _mpool_lvls_##name, \
3422 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003423
Peter Mitsis937042c2016-10-13 13:18:26 -04003424/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003425 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003427 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * @param pool Address of the memory pool.
3430 * @param block Pointer to block descriptor for the allocated memory.
3431 * @param size Amount of memory to allocate (in bytes).
3432 * @param timeout Maximum time to wait for operation to complete
3433 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3434 * or K_FOREVER to wait as long as necessary.
3435 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003436 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003438 * @retval -ENOMEM Returned without waiting.
3439 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003440 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003441extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003442 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003443
3444/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003445 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003447 * This routine releases a previously allocated memory block back to its
3448 * memory pool.
3449 *
3450 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003451 *
3452 * @return N/A
3453 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003455
3456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003458 *
Andy Ross73cb9582017-05-09 10:42:39 -07003459 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 *
Andy Ross73cb9582017-05-09 10:42:39 -07003461 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003462 *
3463 * @return N/A
3464 */
Andy Ross73cb9582017-05-09 10:42:39 -07003465static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003466
3467/**
Allan Stephensc98da842016-11-11 15:45:03 -05003468 * @} end addtogroup mem_pool_apis
3469 */
3470
3471/**
3472 * @defgroup heap_apis Heap Memory Pool APIs
3473 * @ingroup kernel_apis
3474 * @{
3475 */
3476
3477/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003481 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003486 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003487extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003488
3489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003491 *
3492 * This routine provides traditional free() semantics. The memory being
3493 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003494 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003495 * If @a ptr is NULL, no operation is performed.
3496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003498 *
3499 * @return N/A
3500 */
3501extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502
Allan Stephensc98da842016-11-11 15:45:03 -05003503/**
3504 * @} end defgroup heap_apis
3505 */
3506
Benjamin Walshacc68c12017-01-29 18:57:45 -05003507/* polling API - PRIVATE */
3508
Benjamin Walshb0179862017-02-02 16:39:57 -05003509#ifdef CONFIG_POLL
3510#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3511#else
3512#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3513#endif
3514
Benjamin Walshacc68c12017-01-29 18:57:45 -05003515/* private - implementation data created as needed, per-type */
3516struct _poller {
3517 struct k_thread *thread;
3518};
3519
3520/* private - types bit positions */
3521enum _poll_types_bits {
3522 /* can be used to ignore an event */
3523 _POLL_TYPE_IGNORE,
3524
3525 /* to be signaled by k_poll_signal() */
3526 _POLL_TYPE_SIGNAL,
3527
3528 /* semaphore availability */
3529 _POLL_TYPE_SEM_AVAILABLE,
3530
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003531 /* queue/fifo/lifo data availability */
3532 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003533
3534 _POLL_NUM_TYPES
3535};
3536
3537#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3538
3539/* private - states bit positions */
3540enum _poll_states_bits {
3541 /* default state when creating event */
3542 _POLL_STATE_NOT_READY,
3543
3544 /* there was another poller already on the object */
3545 _POLL_STATE_EADDRINUSE,
3546
3547 /* 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
3601#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3602#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3603#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003604#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3605#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003606
3607/* public - poll signal object */
3608struct k_poll_signal {
3609 /* PRIVATE - DO NOT TOUCH */
3610 struct k_poll_event *poll_event;
3611
3612 /*
3613 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3614 * user resets it to 0.
3615 */
3616 unsigned int signaled;
3617
3618 /* custom result value passed to k_poll_signal() if needed */
3619 int result;
3620};
3621
3622#define K_POLL_SIGNAL_INITIALIZER() \
3623 { \
3624 .poll_event = NULL, \
3625 .signaled = 0, \
3626 .result = 0, \
3627 }
3628
3629struct k_poll_event {
3630 /* PRIVATE - DO NOT TOUCH */
3631 struct _poller *poller;
3632
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003633 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003634 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003635
Benjamin Walshacc68c12017-01-29 18:57:45 -05003636 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003637 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003638
3639 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003640 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003641
3642 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003643 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003644
3645 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003646 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003647
3648 /* per-type data */
3649 union {
3650 void *obj;
3651 struct k_poll_signal *signal;
3652 struct k_sem *sem;
3653 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003654 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003655 };
3656};
3657
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003658#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003659 { \
3660 .poller = NULL, \
3661 .type = event_type, \
3662 .state = K_POLL_STATE_NOT_READY, \
3663 .mode = event_mode, \
3664 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003665 { .obj = event_obj }, \
3666 }
3667
3668#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3669 event_tag) \
3670 { \
3671 .type = event_type, \
3672 .tag = event_tag, \
3673 .state = K_POLL_STATE_NOT_READY, \
3674 .mode = event_mode, \
3675 .unused = 0, \
3676 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003677 }
3678
3679/**
3680 * @brief Initialize one struct k_poll_event instance
3681 *
3682 * After this routine is called on a poll event, the event it ready to be
3683 * placed in an event array to be passed to k_poll().
3684 *
3685 * @param event The event to initialize.
3686 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3687 * values. Only values that apply to the same object being polled
3688 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3689 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003690 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003691 * @param obj Kernel object or poll signal.
3692 *
3693 * @return N/A
3694 */
3695
Kumar Galacc334c72017-04-21 10:55:34 -05003696extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003697 int mode, void *obj);
3698
3699/**
3700 * @brief Wait for one or many of multiple poll events to occur
3701 *
3702 * This routine allows a thread to wait concurrently for one or many of
3703 * multiple poll events to have occurred. Such events can be a kernel object
3704 * being available, like a semaphore, or a poll signal event.
3705 *
3706 * When an event notifies that a kernel object is available, the kernel object
3707 * is not "given" to the thread calling k_poll(): it merely signals the fact
3708 * that the object was available when the k_poll() call was in effect. Also,
3709 * all threads trying to acquire an object the regular way, i.e. by pending on
3710 * the object, have precedence over the thread polling on the object. This
3711 * means that the polling thread will never get the poll event on an object
3712 * until the object becomes available and its pend queue is empty. For this
3713 * reason, the k_poll() call is more effective when the objects being polled
3714 * only have one thread, the polling thread, trying to acquire them.
3715 *
3716 * Only one thread can be polling for a particular object at a given time. If
3717 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3718 * and returns as soon as it has finished handling the other events. This means
3719 * that k_poll() can return -EADDRINUSE and have the state value of some events
3720 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3721 * parameter is ignored.
3722 *
3723 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3724 * events that were passed to k_poll() and check the state field for the values
3725 * that were expected and take the associated actions.
3726 *
3727 * Before being reused for another call to k_poll(), the user has to reset the
3728 * state field to K_POLL_STATE_NOT_READY.
3729 *
3730 * @param events An array of pointers to events to be polled for.
3731 * @param num_events The number of events in the array.
3732 * @param timeout Waiting period for an event to be ready (in milliseconds),
3733 * or one of the special values K_NO_WAIT and K_FOREVER.
3734 *
3735 * @retval 0 One or more events are ready.
3736 * @retval -EADDRINUSE One or more objects already had a poller.
3737 * @retval -EAGAIN Waiting period timed out.
3738 */
3739
3740extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003741 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003742
3743/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003744 * @brief Initialize a poll signal object.
3745 *
3746 * Ready a poll signal object to be signaled via k_poll_signal().
3747 *
3748 * @param signal A poll signal.
3749 *
3750 * @return N/A
3751 */
3752
3753extern void k_poll_signal_init(struct k_poll_signal *signal);
3754
3755/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003756 * @brief Signal a poll signal object.
3757 *
3758 * This routine makes ready a poll signal, which is basically a poll event of
3759 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3760 * made ready to run. A @a result value can be specified.
3761 *
3762 * The poll signal contains a 'signaled' field that, when set by
3763 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3764 * be reset by the user before being passed again to k_poll() or k_poll() will
3765 * consider it being signaled, and will return immediately.
3766 *
3767 * @param signal A poll signal.
3768 * @param result The value to store in the result field of the signal.
3769 *
3770 * @retval 0 The signal was delivered successfully.
3771 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3772 */
3773
3774extern int k_poll_signal(struct k_poll_signal *signal, int result);
3775
3776/* private internal function */
3777extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003778 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003779
3780/**
3781 * @} end defgroup poll_apis
3782 */
3783
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003784/**
3785 * @brief Make the CPU idle.
3786 *
3787 * This function makes the CPU idle until an event wakes it up.
3788 *
3789 * In a regular system, the idle thread should be the only thread responsible
3790 * for making the CPU idle and triggering any type of power management.
3791 * However, in some more constrained systems, such as a single-threaded system,
3792 * the only thread would be responsible for this if needed.
3793 *
3794 * @return N/A
3795 */
3796extern void k_cpu_idle(void);
3797
3798/**
3799 * @brief Make the CPU idle in an atomic fashion.
3800 *
3801 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3802 * must be done atomically before making the CPU idle.
3803 *
3804 * @param key Interrupt locking key obtained from irq_lock().
3805 *
3806 * @return N/A
3807 */
3808extern void k_cpu_atomic_idle(unsigned int key);
3809
Kumar Galacc334c72017-04-21 10:55:34 -05003810extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003811
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812#include <arch/cpu.h>
3813
Andrew Boiecdb94d62017-04-18 15:22:05 -07003814#ifdef _ARCH_EXCEPT
3815/* This archtecture has direct support for triggering a CPU exception */
3816#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3817#else
3818
3819#include <misc/printk.h>
3820
3821/* NOTE: This is the implementation for arches that do not implement
3822 * _ARCH_EXCEPT() to generate a real CPU exception.
3823 *
3824 * We won't have a real exception frame to determine the PC value when
3825 * the oops occurred, so print file and line number before we jump into
3826 * the fatal error handler.
3827 */
3828#define _k_except_reason(reason) do { \
3829 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3830 _NanoFatalErrorHandler(reason, &_default_esf); \
3831 CODE_UNREACHABLE; \
3832 } while (0)
3833
3834#endif /* _ARCH__EXCEPT */
3835
3836/**
3837 * @brief Fatally terminate a thread
3838 *
3839 * This should be called when a thread has encountered an unrecoverable
3840 * runtime condition and needs to terminate. What this ultimately
3841 * means is determined by the _fatal_error_handler() implementation, which
3842 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3843 *
3844 * If this is called from ISR context, the default system fatal error handler
3845 * will treat it as an unrecoverable system error, just like k_panic().
3846 */
3847#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3848
3849/**
3850 * @brief Fatally terminate the system
3851 *
3852 * This should be called when the Zephyr kernel has encountered an
3853 * unrecoverable runtime condition and needs to terminate. What this ultimately
3854 * means is determined by the _fatal_error_handler() implementation, which
3855 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3856 */
3857#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3858
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003859/*
3860 * private APIs that are utilized by one or more public APIs
3861 */
3862
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003863#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003865#else
3866#define _init_static_threads() do { } while ((0))
3867#endif
3868
3869extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003870extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003871
Andrew Boiedc5d9352017-06-02 12:56:47 -07003872/* arch/cpu.h may declare an architecture or platform-specific macro
3873 * for properly declaring stacks, compatible with MMU/MPU constraints if
3874 * enabled
3875 */
3876#ifdef _ARCH_THREAD_STACK_DEFINE
3877#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3878#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3879 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3880#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3881#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003882static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3883{
3884 return _ARCH_THREAD_STACK_BUFFER(sym);
3885}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003886#else
3887/**
3888 * @brief Declare a toplevel thread stack memory region
3889 *
3890 * This declares a region of memory suitable for use as a thread's stack.
3891 *
3892 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3893 * 'noinit' section so that it isn't zeroed at boot
3894 *
Andrew Boie507852a2017-07-25 18:47:07 -07003895 * The declared symbol will always be a k_thread_stack_t which can be passed to
3896 * k_thread_create, but should otherwise not be manipulated. If the buffer
3897 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003898 *
3899 * It is legal to precede this definition with the 'static' keyword.
3900 *
3901 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3902 * parameter of k_thread_create(), it may not be the same as the
3903 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3904 *
3905 * @param sym Thread stack symbol name
3906 * @param size Size of the stack memory region
3907 */
3908#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003909 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003910
3911/**
3912 * @brief Declare a toplevel array of thread stack memory regions
3913 *
3914 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3915 * definition for additional details and constraints.
3916 *
3917 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3918 * 'noinit' section so that it isn't zeroed at boot
3919 *
3920 * @param sym Thread stack symbol name
3921 * @param nmemb Number of stacks to declare
3922 * @param size Size of the stack memory region
3923 */
3924
3925#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003926 struct _k_thread_stack_element __noinit \
3927 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003928
3929/**
3930 * @brief Declare an embedded stack memory region
3931 *
3932 * Used for stacks embedded within other data structures. Use is highly
3933 * discouraged but in some cases necessary. For memory protection scenarios,
3934 * it is very important that any RAM preceding this member not be writable
3935 * by threads else a stack overflow will lead to silent corruption. In other
3936 * words, the containing data structure should live in RAM owned by the kernel.
3937 *
3938 * @param sym Thread stack symbol name
3939 * @param size Size of the stack memory region
3940 */
3941#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003942 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003943
3944/**
3945 * @brief Return the size in bytes of a stack memory region
3946 *
3947 * Convenience macro for passing the desired stack size to k_thread_create()
3948 * since the underlying implementation may actually create something larger
3949 * (for instance a guard area).
3950 *
3951 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07003952 * passed to K_THREAD_STACK_DEFINE.
3953 *
3954 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
3955 * it is not guaranteed to return the original value since each array
3956 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07003957 *
3958 * @param sym Stack memory symbol
3959 * @return Size of the stack
3960 */
3961#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
3962
3963/**
3964 * @brief Get a pointer to the physical stack buffer
3965 *
3966 * Convenience macro to get at the real underlying stack buffer used by
3967 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
3968 * This is really only intended for diagnostic tools which want to examine
3969 * stack memory contents.
3970 *
3971 * @param sym Declared stack symbol name
3972 * @return The buffer itself, a char *
3973 */
Andrew Boie507852a2017-07-25 18:47:07 -07003974static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3975{
3976 return (char *)sym;
3977}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003978
3979#endif /* _ARCH_DECLARE_STACK */
3980
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003981#ifdef __cplusplus
3982}
3983#endif
3984
Andrew Boiee004dec2016-11-07 09:01:19 -08003985#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3986/*
3987 * Define new and delete operators.
3988 * At this moment, the operators do nothing since objects are supposed
3989 * to be statically allocated.
3990 */
3991inline void operator delete(void *ptr)
3992{
3993 (void)ptr;
3994}
3995
3996inline void operator delete[](void *ptr)
3997{
3998 (void)ptr;
3999}
4000
4001inline void *operator new(size_t size)
4002{
4003 (void)size;
4004 return NULL;
4005}
4006
4007inline void *operator new[](size_t size)
4008{
4009 (void)size;
4010 return NULL;
4011}
4012
4013/* Placement versions of operator new and delete */
4014inline void operator delete(void *ptr1, void *ptr2)
4015{
4016 (void)ptr1;
4017 (void)ptr2;
4018}
4019
4020inline void operator delete[](void *ptr1, void *ptr2)
4021{
4022 (void)ptr1;
4023 (void)ptr2;
4024}
4025
4026inline void *operator new(size_t size, void *ptr)
4027{
4028 (void)size;
4029 return ptr;
4030}
4031
4032inline void *operator new[](size_t size, void *ptr)
4033{
4034 (void)size;
4035 return ptr;
4036}
4037
4038#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4039
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004040#endif /* !_ASMLANGUAGE */
4041
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004042#endif /* _kernel__h_ */