blob: 4f0583bbc1713ec6a76c00c63b95933bb8bbb0fc [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/**
1469 * @brief Query a queue to see if it has data available.
1470 *
1471 * Note that the data might be already gone by the time this function returns
1472 * if other threads are also trying to read from the queue.
1473 *
1474 * @note Can be called by ISRs.
1475 *
1476 * @param queue Address of the queue.
1477 *
1478 * @return Non-zero if the queue is empty.
1479 * @return 0 if data is available.
1480 */
1481static inline int k_queue_is_empty(struct k_queue *queue)
1482{
1483 return (int)sys_slist_is_empty(&queue->data_q);
1484}
1485
1486/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001487 * @brief Peek element at the head of queue.
1488 *
1489 * Return element from the head of queue without removing it.
1490 *
1491 * @param queue Address of the queue.
1492 *
1493 * @return Head element, or NULL if queue is empty.
1494 */
1495static inline void *k_queue_peek_head(struct k_queue *queue)
1496{
1497 return sys_slist_peek_head(&queue->data_q);
1498}
1499
1500/**
1501 * @brief Peek element at the tail of queue.
1502 *
1503 * Return element from the tail of queue without removing it.
1504 *
1505 * @param queue Address of the queue.
1506 *
1507 * @return Tail element, or NULL if queue is empty.
1508 */
1509static inline void *k_queue_peek_tail(struct k_queue *queue)
1510{
1511 return sys_slist_peek_tail(&queue->data_q);
1512}
1513
1514/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001515 * @brief Statically define and initialize a queue.
1516 *
1517 * The queue can be accessed outside the module where it is defined using:
1518 *
1519 * @code extern struct k_queue <name>; @endcode
1520 *
1521 * @param name Name of the queue.
1522 */
1523#define K_QUEUE_DEFINE(name) \
1524 struct k_queue name \
1525 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001526 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001527
1528/**
1529 * @} end defgroup queue_apis
1530 */
1531
1532/**
1533 * @cond INTERNAL_HIDDEN
1534 */
1535
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001537 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001538};
1539
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001540#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001541 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001542 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001543 }
1544
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001545#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1546
Allan Stephensc98da842016-11-11 15:45:03 -05001547/**
1548 * INTERNAL_HIDDEN @endcond
1549 */
1550
1551/**
1552 * @defgroup fifo_apis Fifo APIs
1553 * @ingroup kernel_apis
1554 * @{
1555 */
1556
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001558 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001560 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001562 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001563 *
1564 * @return N/A
1565 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001566#define k_fifo_init(fifo) \
1567 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001568
1569/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001570 * @brief Cancel waiting on a fifo.
1571 *
1572 * This routine causes first thread pending on @a fifo, if any, to
1573 * return from k_fifo_get() call with NULL value (as if timeout
1574 * expired).
1575 *
1576 * @note Can be called by ISRs.
1577 *
1578 * @param fifo Address of the fifo.
1579 *
1580 * @return N/A
1581 */
1582#define k_fifo_cancel_wait(fifo) \
1583 k_queue_cancel_wait((struct k_queue *) fifo)
1584
1585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001586 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * This routine adds a data item to @a fifo. A fifo data item must be
1589 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1590 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001592 * @note Can be called by ISRs.
1593 *
1594 * @param fifo Address of the fifo.
1595 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001596 *
1597 * @return N/A
1598 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001599#define k_fifo_put(fifo, data) \
1600 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001601
1602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * This routine adds a list of data items to @a fifo in one operation.
1606 * The data items must be in a singly-linked list, with the first 32 bits
1607 * each data item pointing to the next data item; the list must be
1608 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001610 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @param fifo Address of the fifo.
1613 * @param head Pointer to first node in singly-linked list.
1614 * @param tail Pointer to last node in singly-linked list.
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_list(fifo, head, tail) \
1619 k_queue_append_list((struct k_queue *) fifo, head, tail)
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 implemented using a
1626 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001627 * and must be re-initialized via sys_slist_init().
1628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * @note Can be called by ISRs.
1630 *
1631 * @param fifo Address of the fifo.
1632 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001633 *
1634 * @return N/A
1635 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001636#define k_fifo_put_slist(fifo, list) \
1637 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001638
1639/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001640 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001642 * This routine removes a data item from @a fifo in a "first in, first out"
1643 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001645 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1646 *
1647 * @param fifo Address of the fifo.
1648 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001649 * or one of the special values K_NO_WAIT and K_FOREVER.
1650 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001651 * @return Address of the data item if successful; NULL if returned
1652 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001653 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001654#define k_fifo_get(fifo, timeout) \
1655 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001657/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001658 * @brief Query a fifo to see if it has data available.
1659 *
1660 * Note that the data might be already gone by the time this function returns
1661 * if other threads is also trying to read from the fifo.
1662 *
1663 * @note Can be called by ISRs.
1664 *
1665 * @param fifo Address of the fifo.
1666 *
1667 * @return Non-zero if the fifo is empty.
1668 * @return 0 if data is available.
1669 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001670#define k_fifo_is_empty(fifo) \
1671 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001672
1673/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001674 * @brief Peek element at the head of fifo.
1675 *
1676 * Return element from the head of fifo without removing it. A usecase
1677 * for this is if elements of the fifo are themselves containers. Then
1678 * on each iteration of processing, a head container will be peeked,
1679 * and some data processed out of it, and only if the container is empty,
1680 * it will be completely remove from the fifo.
1681 *
1682 * @param fifo Address of the fifo.
1683 *
1684 * @return Head element, or NULL if the fifo is empty.
1685 */
1686#define k_fifo_peek_head(fifo) \
1687 k_queue_peek_head((struct k_queue *) fifo)
1688
1689/**
1690 * @brief Peek element at the tail of fifo.
1691 *
1692 * Return element from the tail of fifo (without removing it). A usecase
1693 * for this is if elements of the fifo are themselves containers. Then
1694 * it may be useful to add more data to the last container in fifo.
1695 *
1696 * @param fifo Address of the fifo.
1697 *
1698 * @return Tail element, or NULL if fifo is empty.
1699 */
1700#define k_fifo_peek_tail(fifo) \
1701 k_queue_peek_tail((struct k_queue *) fifo)
1702
1703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001708 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001712#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001713 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001714 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001715 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001716
Allan Stephensc98da842016-11-11 15:45:03 -05001717/**
1718 * @} end defgroup fifo_apis
1719 */
1720
1721/**
1722 * @cond INTERNAL_HIDDEN
1723 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001724
1725struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001726 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001727};
1728
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001729#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001730 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001731 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001732 }
1733
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001734#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1735
Allan Stephensc98da842016-11-11 15:45:03 -05001736/**
1737 * INTERNAL_HIDDEN @endcond
1738 */
1739
1740/**
1741 * @defgroup lifo_apis Lifo APIs
1742 * @ingroup kernel_apis
1743 * @{
1744 */
1745
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001746/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752 *
1753 * @return N/A
1754 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001755#define k_lifo_init(lifo) \
1756 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757
1758/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001761 * This routine adds a data item to @a lifo. A lifo data item must be
1762 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1763 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001765 * @note Can be called by ISRs.
1766 *
1767 * @param lifo Address of the lifo.
1768 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769 *
1770 * @return N/A
1771 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001772#define k_lifo_put(lifo, data) \
1773 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774
1775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * This routine removes a data item from @a lifo in a "last in, first out"
1779 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1782 *
1783 * @param lifo Address of the lifo.
1784 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001785 * or one of the special values K_NO_WAIT and K_FOREVER.
1786 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001787 * @return Address of the data item if successful; NULL if returned
1788 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001789 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001790#define k_lifo_get(lifo, timeout) \
1791 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001792
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001797 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001798 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001803 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001804 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001805 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001806
Allan Stephensc98da842016-11-11 15:45:03 -05001807/**
1808 * @} end defgroup lifo_apis
1809 */
1810
1811/**
1812 * @cond INTERNAL_HIDDEN
1813 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001814
1815struct k_stack {
1816 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001817 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818
Anas Nashif2f203c22016-12-18 06:57:45 -05001819 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001820};
1821
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001822#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001823 { \
1824 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1825 .base = stack_buffer, \
1826 .next = stack_buffer, \
1827 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001828 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001829 }
1830
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001831#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1832
Allan Stephensc98da842016-11-11 15:45:03 -05001833/**
1834 * INTERNAL_HIDDEN @endcond
1835 */
1836
1837/**
1838 * @defgroup stack_apis Stack APIs
1839 * @ingroup kernel_apis
1840 * @{
1841 */
1842
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001843/**
1844 * @brief Initialize a stack.
1845 *
1846 * This routine initializes a stack object, prior to its first use.
1847 *
1848 * @param stack Address of the stack.
1849 * @param buffer Address of array used to hold stacked values.
1850 * @param num_entries Maximum number of values that can be stacked.
1851 *
1852 * @return N/A
1853 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001854extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001855 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856
1857/**
1858 * @brief Push an element onto a stack.
1859 *
1860 * This routine adds a 32-bit value @a data to @a stack.
1861 *
1862 * @note Can be called by ISRs.
1863 *
1864 * @param stack Address of the stack.
1865 * @param data Value to push onto the stack.
1866 *
1867 * @return N/A
1868 */
Kumar Galacc334c72017-04-21 10:55:34 -05001869extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870
1871/**
1872 * @brief Pop an element from a stack.
1873 *
1874 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1875 * manner and stores the value in @a data.
1876 *
1877 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1878 *
1879 * @param stack Address of the stack.
1880 * @param data Address of area to hold the value popped from the stack.
1881 * @param timeout Waiting period to obtain a value (in milliseconds),
1882 * or one of the special values K_NO_WAIT and K_FOREVER.
1883 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001884 * @retval 0 Element popped from stack.
1885 * @retval -EBUSY Returned without waiting.
1886 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 */
Kumar Galacc334c72017-04-21 10:55:34 -05001888extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001889
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001894 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001895 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * @param name Name of the stack.
1898 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001900#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001901 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001902 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001903 struct k_stack name \
1904 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001905 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001906 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001907
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001908/**
Allan Stephensc98da842016-11-11 15:45:03 -05001909 * @} end defgroup stack_apis
1910 */
1911
Allan Stephens6bba9b02016-11-16 14:56:54 -05001912struct k_work;
1913
Allan Stephensc98da842016-11-11 15:45:03 -05001914/**
1915 * @defgroup workqueue_apis Workqueue Thread APIs
1916 * @ingroup kernel_apis
1917 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001918 */
1919
Allan Stephens6bba9b02016-11-16 14:56:54 -05001920/**
1921 * @typedef k_work_handler_t
1922 * @brief Work item handler function type.
1923 *
1924 * A work item's handler function is executed by a workqueue's thread
1925 * when the work item is processed by the workqueue.
1926 *
1927 * @param work Address of the work item.
1928 *
1929 * @return N/A
1930 */
1931typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001932
1933/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001934 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001936
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937struct k_work_q {
1938 struct k_fifo fifo;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001939 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940};
1941
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001942enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001943 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001944};
1945
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001946struct k_work {
1947 void *_reserved; /* Used by k_fifo implementation. */
1948 k_work_handler_t handler;
1949 atomic_t flags[1];
1950};
1951
Allan Stephens6bba9b02016-11-16 14:56:54 -05001952struct k_delayed_work {
1953 struct k_work work;
1954 struct _timeout timeout;
1955 struct k_work_q *work_q;
1956};
1957
1958extern struct k_work_q k_sys_work_q;
1959
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001960/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001961 * INTERNAL_HIDDEN @endcond
1962 */
1963
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001964#define _K_WORK_INITIALIZER(work_handler) \
1965 { \
1966 ._reserved = NULL, \
1967 .handler = work_handler, \
1968 .flags = { 0 } \
1969 }
1970
1971#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
1972
Allan Stephens6bba9b02016-11-16 14:56:54 -05001973/**
1974 * @brief Initialize a statically-defined work item.
1975 *
1976 * This macro can be used to initialize a statically-defined workqueue work
1977 * item, prior to its first use. For example,
1978 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001979 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05001980 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001981 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05001982 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001984#define K_WORK_DEFINE(work, work_handler) \
1985 struct k_work work \
1986 __in_section(_k_work, static, work) = \
1987 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001988
1989/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001990 * @brief Initialize a work item.
1991 *
1992 * This routine initializes a workqueue work item, prior to its first use.
1993 *
1994 * @param work Address of work item.
1995 * @param handler Function to invoke each time work item is processed.
1996 *
1997 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001998 */
1999static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2000{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002001 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002002 work->handler = handler;
2003}
2004
2005/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002006 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002007 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002008 * This routine submits work item @a work to be processed by workqueue
2009 * @a work_q. If the work item is already pending in the workqueue's queue
2010 * as a result of an earlier submission, this routine has no effect on the
2011 * work item. If the work item has already been processed, or is currently
2012 * being processed, its work is considered complete and the work item can be
2013 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002014 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002015 * @warning
2016 * A submitted work item must not be modified until it has been processed
2017 * by the workqueue.
2018 *
2019 * @note Can be called by ISRs.
2020 *
2021 * @param work_q Address of workqueue.
2022 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002023 *
2024 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002025 */
2026static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2027 struct k_work *work)
2028{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002029 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030 k_fifo_put(&work_q->fifo, work);
2031 }
2032}
2033
2034/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002035 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002037 * This routine indicates if work item @a work is pending in a workqueue's
2038 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002040 * @note Can be called by ISRs.
2041 *
2042 * @param work Address of work item.
2043 *
2044 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002045 */
2046static inline int k_work_pending(struct k_work *work)
2047{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002048 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002049}
2050
2051/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002052 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002054 * This routine starts workqueue @a work_q. The workqueue spawns its work
2055 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002056 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002057 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002058 * @param stack Pointer to work queue thread's stack space, as defined by
2059 * K_THREAD_STACK_DEFINE()
2060 * @param stack_size Size of the work queue thread's stack (in bytes), which
2061 * should either be the same constant passed to
2062 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002063 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 *
2065 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066 */
Andrew Boie507852a2017-07-25 18:47:07 -07002067extern void k_work_q_start(struct k_work_q *work_q,
2068 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002069 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002070
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002071/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002072 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002073 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002074 * This routine initializes a workqueue delayed work item, prior to
2075 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002077 * @param work Address of delayed work item.
2078 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 *
2080 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002081 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002082extern void k_delayed_work_init(struct k_delayed_work *work,
2083 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002084
2085/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002086 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002088 * This routine schedules work item @a work to be processed by workqueue
2089 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002090 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002091 * Only when the countdown completes is the work item actually submitted to
2092 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002094 * Submitting a previously submitted delayed work item that is still
2095 * counting down cancels the existing submission and restarts the countdown
2096 * using the new delay. If the work item is currently pending on the
2097 * workqueue's queue because the countdown has completed it is too late to
2098 * resubmit the item, and resubmission fails without impacting the work item.
2099 * If the work item has already been processed, or is currently being processed,
2100 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002101 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002102 * @warning
2103 * A delayed work item must not be modified until it has been processed
2104 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002105 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002106 * @note Can be called by ISRs.
2107 *
2108 * @param work_q Address of workqueue.
2109 * @param work Address of delayed work item.
2110 * @param delay Delay before submitting the work item (in milliseconds).
2111 *
2112 * @retval 0 Work item countdown started.
2113 * @retval -EINPROGRESS Work item is already pending.
2114 * @retval -EINVAL Work item is being processed or has completed its work.
2115 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002116 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002117extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2118 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002119 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120
2121/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002122 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002123 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002124 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002125 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002126 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002128 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002129 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002130 * @param work Address of delayed work item.
2131 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002132 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002133 * @retval -EINPROGRESS Work item is already pending.
2134 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002135 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002136extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002137
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139 * @brief Submit a work item to the system workqueue.
2140 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002141 * This routine submits work item @a work to be processed by the system
2142 * workqueue. If the work item is already pending in the workqueue's queue
2143 * as a result of an earlier submission, this routine has no effect on the
2144 * work item. If the work item has already been processed, or is currently
2145 * being processed, its work is considered complete and the work item can be
2146 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002147 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148 * @warning
2149 * Work items submitted to the system workqueue should avoid using handlers
2150 * that block or yield since this may prevent the system workqueue from
2151 * processing other work items in a timely manner.
2152 *
2153 * @note Can be called by ISRs.
2154 *
2155 * @param work Address of work item.
2156 *
2157 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158 */
2159static inline void k_work_submit(struct k_work *work)
2160{
2161 k_work_submit_to_queue(&k_sys_work_q, work);
2162}
2163
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002165 * @brief Submit a delayed work item to the system workqueue.
2166 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002167 * This routine schedules work item @a work to be processed by the system
2168 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002169 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002170 * Only when the countdown completes is the work item actually submitted to
2171 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002172 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002173 * Submitting a previously submitted delayed work item that is still
2174 * counting down cancels the existing submission and restarts the countdown
2175 * using the new delay. If the work item is currently pending on the
2176 * workqueue's queue because the countdown has completed it is too late to
2177 * resubmit the item, and resubmission fails without impacting the work item.
2178 * If the work item has already been processed, or is currently being processed,
2179 * its work is considered complete and the work item can be resubmitted.
2180 *
2181 * @warning
2182 * Work items submitted to the system workqueue should avoid using handlers
2183 * that block or yield since this may prevent the system workqueue from
2184 * processing other work items in a timely manner.
2185 *
2186 * @note Can be called by ISRs.
2187 *
2188 * @param work Address of delayed work item.
2189 * @param delay Delay before submitting the work item (in milliseconds).
2190 *
2191 * @retval 0 Work item countdown started.
2192 * @retval -EINPROGRESS Work item is already pending.
2193 * @retval -EINVAL Work item is being processed or has completed its work.
2194 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195 */
2196static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002197 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002199 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002200}
2201
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002203 * @brief Get time remaining before a delayed work gets scheduled.
2204 *
2205 * This routine computes the (approximate) time remaining before a
2206 * delayed work gets executed. If the delayed work is not waiting to be
2207 * schedules, it returns zero.
2208 *
2209 * @param work Delayed work item.
2210 *
2211 * @return Remaining time (in milliseconds).
2212 */
Kumar Galacc334c72017-04-21 10:55:34 -05002213static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002214{
2215 return _timeout_remaining_get(&work->timeout);
2216}
2217
2218/**
Allan Stephensc98da842016-11-11 15:45:03 -05002219 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 */
2221
Allan Stephensc98da842016-11-11 15:45:03 -05002222/**
2223 * @cond INTERNAL_HIDDEN
2224 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225
2226struct k_mutex {
2227 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002228 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002229 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231
Anas Nashif2f203c22016-12-18 06:57:45 -05002232 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233};
2234
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002235#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002236 { \
2237 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2238 .owner = NULL, \
2239 .lock_count = 0, \
2240 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002241 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242 }
2243
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002244#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2245
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246/**
Allan Stephensc98da842016-11-11 15:45:03 -05002247 * INTERNAL_HIDDEN @endcond
2248 */
2249
2250/**
2251 * @defgroup mutex_apis Mutex APIs
2252 * @ingroup kernel_apis
2253 * @{
2254 */
2255
2256/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002257 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002259 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002261 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002263 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002265#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002266 struct k_mutex name \
2267 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002268 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002271 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002273 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002275 * Upon completion, the mutex is available and does not have an owner.
2276 *
2277 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 *
2279 * @return N/A
2280 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282
2283/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002284 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002286 * This routine locks @a mutex. If the mutex is locked by another thread,
2287 * the calling thread waits until the mutex becomes available or until
2288 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * A thread is permitted to lock a mutex it has already locked. The operation
2291 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @param mutex Address of the mutex.
2294 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 * or one of the special values K_NO_WAIT and K_FOREVER.
2296 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002297 * @retval 0 Mutex locked.
2298 * @retval -EBUSY Returned without waiting.
2299 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 */
Kumar Galacc334c72017-04-21 10:55:34 -05002301extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002302
2303/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002304 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002306 * This routine unlocks @a mutex. The mutex must already be locked by the
2307 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 *
2309 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002310 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311 * thread.
2312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
2315 * @return N/A
2316 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317extern void k_mutex_unlock(struct k_mutex *mutex);
2318
Allan Stephensc98da842016-11-11 15:45:03 -05002319/**
2320 * @} end defgroup mutex_apis
2321 */
2322
2323/**
2324 * @cond INTERNAL_HIDDEN
2325 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002326
2327struct k_sem {
2328 _wait_q_t wait_q;
2329 unsigned int count;
2330 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002331 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332
Anas Nashif2f203c22016-12-18 06:57:45 -05002333 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334};
2335
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002336#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002337 { \
2338 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2339 .count = initial_count, \
2340 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002341 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002342 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002343 }
2344
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002345#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2346
Allan Stephensc98da842016-11-11 15:45:03 -05002347/**
2348 * INTERNAL_HIDDEN @endcond
2349 */
2350
2351/**
2352 * @defgroup semaphore_apis Semaphore APIs
2353 * @ingroup kernel_apis
2354 * @{
2355 */
2356
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * @param sem Address of the semaphore.
2363 * @param initial_count Initial semaphore count.
2364 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002365 *
2366 * @return N/A
2367 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2369 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002370
2371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2377 *
2378 * @param sem Address of the semaphore.
2379 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002380 * or one of the special values K_NO_WAIT and K_FOREVER.
2381 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002382 * @note When porting code from the nanokernel legacy API to the new API, be
2383 * careful with the return value of this function. The return value is the
2384 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2385 * non-zero means failure, while the nano_sem_take family returns 1 for success
2386 * and 0 for failure.
2387 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002388 * @retval 0 Semaphore taken.
2389 * @retval -EBUSY Returned without waiting.
2390 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002391 */
Kumar Galacc334c72017-04-21 10:55:34 -05002392extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002393
2394/**
2395 * @brief Give a semaphore.
2396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * This routine gives @a sem, unless the semaphore is already at its maximum
2398 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002400 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002403 *
2404 * @return N/A
2405 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406extern void k_sem_give(struct k_sem *sem);
2407
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002408/**
2409 * @brief Reset a semaphore's count to zero.
2410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002413 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002414 *
2415 * @return N/A
2416 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002417static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418{
2419 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420}
2421
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002422/**
2423 * @brief Get a semaphore's count.
2424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002429 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002430 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002431static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432{
2433 return sem->count;
2434}
2435
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002437 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002440 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002441 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 * @param name Name of the semaphore.
2444 * @param initial_count Initial semaphore count.
2445 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002446 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002448 struct k_sem name \
2449 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002450 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451
Allan Stephensc98da842016-11-11 15:45:03 -05002452/**
2453 * @} end defgroup semaphore_apis
2454 */
2455
2456/**
2457 * @defgroup alert_apis Alert APIs
2458 * @ingroup kernel_apis
2459 * @{
2460 */
2461
Allan Stephens5eceb852016-11-16 10:16:30 -05002462/**
2463 * @typedef k_alert_handler_t
2464 * @brief Alert handler function type.
2465 *
2466 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002467 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002468 * and is only invoked if the alert has been initialized with one.
2469 *
2470 * @param alert Address of the alert.
2471 *
2472 * @return 0 if alert has been consumed; non-zero if alert should pend.
2473 */
2474typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002475
2476/**
2477 * @} end defgroup alert_apis
2478 */
2479
2480/**
2481 * @cond INTERNAL_HIDDEN
2482 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002484#define K_ALERT_DEFAULT NULL
2485#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002487struct k_alert {
2488 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489 atomic_t send_count;
2490 struct k_work work_item;
2491 struct k_sem sem;
2492
Anas Nashif2f203c22016-12-18 06:57:45 -05002493 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494};
2495
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002496extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002498#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002500 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002502 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2503 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002504 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 }
2506
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002507#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2508
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509/**
Allan Stephensc98da842016-11-11 15:45:03 -05002510 * INTERNAL_HIDDEN @endcond
2511 */
2512
2513/**
2514 * @addtogroup alert_apis
2515 * @{
2516 */
2517
2518/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002521 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002522 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002523 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * @param name Name of the alert.
2526 * @param alert_handler Action to take when alert is sent. Specify either
2527 * the address of a function to be invoked by the system workqueue
2528 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2529 * K_ALERT_DEFAULT (which causes the alert to pend).
2530 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002532#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002533 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002534 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002535 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002536 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002543 * @param alert Address of the alert.
2544 * @param handler Action to take when alert is sent. Specify either the address
2545 * of a function to be invoked by the system workqueue thread,
2546 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2547 * K_ALERT_DEFAULT (which causes the alert to pend).
2548 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549 *
2550 * @return N/A
2551 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002552extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2553 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002554
2555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2561 *
2562 * @param alert Address of the alert.
2563 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002564 * or one of the special values K_NO_WAIT and K_FOREVER.
2565 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002566 * @retval 0 Alert received.
2567 * @retval -EBUSY Returned without waiting.
2568 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002569 */
Kumar Galacc334c72017-04-21 10:55:34 -05002570extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002571
2572/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * This routine signals @a alert. The action specified for @a alert will
2576 * be taken, which may trigger the execution of an alert handler function
2577 * and/or cause the alert to pend (assuming the alert has not reached its
2578 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * @note Can be called by ISRs.
2581 *
2582 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002583 *
2584 * @return N/A
2585 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002586extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587
2588/**
Allan Stephensc98da842016-11-11 15:45:03 -05002589 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590 */
2591
Allan Stephensc98da842016-11-11 15:45:03 -05002592/**
2593 * @cond INTERNAL_HIDDEN
2594 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595
2596struct k_msgq {
2597 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002598 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002599 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600 char *buffer_start;
2601 char *buffer_end;
2602 char *read_ptr;
2603 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002604 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002605
Anas Nashif2f203c22016-12-18 06:57:45 -05002606 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607};
2608
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002609#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610 { \
2611 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002612 .max_msgs = q_max_msgs, \
2613 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002615 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 .read_ptr = q_buffer, \
2617 .write_ptr = q_buffer, \
2618 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002619 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620 }
2621
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002622#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2623
Peter Mitsis1da807e2016-10-06 11:36:59 -04002624/**
Allan Stephensc98da842016-11-11 15:45:03 -05002625 * INTERNAL_HIDDEN @endcond
2626 */
2627
2628/**
2629 * @defgroup msgq_apis Message Queue APIs
2630 * @ingroup kernel_apis
2631 * @{
2632 */
2633
2634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2638 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002639 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2640 * message is similarly aligned to this boundary, @a q_msg_size must also be
2641 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 * The message queue can be accessed outside the module where it is defined
2644 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002645 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002646 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * @param q_name Name of the message queue.
2649 * @param q_msg_size Message size (in bytes).
2650 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002651 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002652 */
2653#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2654 static char __noinit __aligned(q_align) \
2655 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002656 struct k_msgq q_name \
2657 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002658 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002659 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660
Peter Mitsisd7a37502016-10-13 11:37:40 -04002661/**
2662 * @brief Initialize a message queue.
2663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002664 * This routine initializes a message queue object, prior to its first use.
2665 *
Allan Stephensda827222016-11-09 14:23:58 -06002666 * The message queue's ring buffer must contain space for @a max_msgs messages,
2667 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2668 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2669 * that each message is similarly aligned to this boundary, @a q_msg_size
2670 * must also be a multiple of N.
2671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * @param q Address of the message queue.
2673 * @param buffer Pointer to ring buffer that holds queued messages.
2674 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002675 * @param max_msgs Maximum number of messages that can be queued.
2676 *
2677 * @return N/A
2678 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002679extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002680 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002681
2682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002686 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002687 * @note Can be called by ISRs.
2688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002689 * @param q Address of the message queue.
2690 * @param data Pointer to the message.
2691 * @param timeout Waiting period to add the message (in milliseconds),
2692 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002693 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002694 * @retval 0 Message sent.
2695 * @retval -ENOMSG Returned without waiting or queue purged.
2696 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002697 */
Kumar Galacc334c72017-04-21 10:55:34 -05002698extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002699
2700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002703 * This routine receives a message from message queue @a q in a "first in,
2704 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002705 *
Allan Stephensc98da842016-11-11 15:45:03 -05002706 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 * @param q Address of the message queue.
2709 * @param data Address of area to hold the received message.
2710 * @param timeout Waiting period to receive 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 received.
2714 * @retval -ENOMSG Returned without waiting.
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_get(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 Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * This routine discards all unreceived messages in a message queue's ring
2723 * buffer. Any threads that are blocked waiting to send a message to the
2724 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002727 *
2728 * @return N/A
2729 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730extern void k_msgq_purge(struct k_msgq *q);
2731
Peter Mitsis67be2492016-10-07 11:44:34 -04002732/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * This routine returns the number of unused entries in a message queue's
2736 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * @param q Address of the message queue.
2739 *
2740 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002741 */
Kumar Galacc334c72017-04-21 10:55:34 -05002742static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002743{
2744 return q->max_msgs - q->used_msgs;
2745}
2746
Peter Mitsisd7a37502016-10-13 11:37:40 -04002747/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002748 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002750 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * @param q Address of the message queue.
2753 *
2754 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002755 */
Kumar Galacc334c72017-04-21 10:55:34 -05002756static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757{
2758 return q->used_msgs;
2759}
2760
Allan Stephensc98da842016-11-11 15:45:03 -05002761/**
2762 * @} end defgroup msgq_apis
2763 */
2764
2765/**
2766 * @defgroup mem_pool_apis Memory Pool APIs
2767 * @ingroup kernel_apis
2768 * @{
2769 */
2770
Andy Ross73cb9582017-05-09 10:42:39 -07002771/* Note on sizing: the use of a 20 bit field for block means that,
2772 * assuming a reasonable minimum block size of 16 bytes, we're limited
2773 * to 16M of memory managed by a single pool. Long term it would be
2774 * good to move to a variable bit size based on configuration.
2775 */
2776struct k_mem_block_id {
2777 u32_t pool : 8;
2778 u32_t level : 4;
2779 u32_t block : 20;
2780};
2781
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002784 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002785};
2786
Allan Stephensc98da842016-11-11 15:45:03 -05002787/**
2788 * @} end defgroup mem_pool_apis
2789 */
2790
2791/**
2792 * @defgroup mailbox_apis Mailbox APIs
2793 * @ingroup kernel_apis
2794 * @{
2795 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796
2797struct k_mbox_msg {
2798 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002799 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002801 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002803 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804 /** sender's message data buffer */
2805 void *tx_data;
2806 /** internal use only - needed for legacy API support */
2807 void *_rx_data;
2808 /** message data block descriptor */
2809 struct k_mem_block tx_block;
2810 /** source thread id */
2811 k_tid_t rx_source_thread;
2812 /** target thread id */
2813 k_tid_t tx_target_thread;
2814 /** internal use only - thread waiting on send (may be a dummy) */
2815 k_tid_t _syncing_thread;
2816#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2817 /** internal use only - semaphore used during asynchronous send */
2818 struct k_sem *_async_sem;
2819#endif
2820};
2821
Allan Stephensc98da842016-11-11 15:45:03 -05002822/**
2823 * @cond INTERNAL_HIDDEN
2824 */
2825
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826struct k_mbox {
2827 _wait_q_t tx_msg_queue;
2828 _wait_q_t rx_msg_queue;
2829
Anas Nashif2f203c22016-12-18 06:57:45 -05002830 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831};
2832
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002833#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 { \
2835 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2836 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002837 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 }
2839
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002840#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2841
Peter Mitsis12092702016-10-14 12:57:23 -04002842/**
Allan Stephensc98da842016-11-11 15:45:03 -05002843 * INTERNAL_HIDDEN @endcond
2844 */
2845
2846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002851 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002854 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002855#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002856 struct k_mbox name \
2857 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002858 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859
Peter Mitsis12092702016-10-14 12:57:23 -04002860/**
2861 * @brief Initialize a mailbox.
2862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * This routine initializes a mailbox object, prior to its first use.
2864 *
2865 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002866 *
2867 * @return N/A
2868 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002869extern void k_mbox_init(struct k_mbox *mbox);
2870
Peter Mitsis12092702016-10-14 12:57:23 -04002871/**
2872 * @brief Send a mailbox message in a synchronous manner.
2873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * This routine sends a message to @a mbox and waits for a receiver to both
2875 * receive and process it. The message data may be in a buffer, in a memory
2876 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002878 * @param mbox Address of the mailbox.
2879 * @param tx_msg Address of the transmit message descriptor.
2880 * @param timeout Waiting period for the message to be received (in
2881 * milliseconds), or one of the special values K_NO_WAIT
2882 * and K_FOREVER. Once the message has been received,
2883 * this routine waits as long as necessary for the message
2884 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002885 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002886 * @retval 0 Message sent.
2887 * @retval -ENOMSG Returned without waiting.
2888 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002889 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002890extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002891 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002892
Peter Mitsis12092702016-10-14 12:57:23 -04002893/**
2894 * @brief Send a mailbox message in an asynchronous manner.
2895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * This routine sends a message to @a mbox without waiting for a receiver
2897 * to process it. The message data may be in a buffer, in a memory pool block,
2898 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2899 * will be given when the message has been both received and completely
2900 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @param mbox Address of the mailbox.
2903 * @param tx_msg Address of the transmit message descriptor.
2904 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002905 *
2906 * @return N/A
2907 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002908extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002909 struct k_sem *sem);
2910
Peter Mitsis12092702016-10-14 12:57:23 -04002911/**
2912 * @brief Receive a mailbox message.
2913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * This routine receives a message from @a mbox, then optionally retrieves
2915 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * @param mbox Address of the mailbox.
2918 * @param rx_msg Address of the receive message descriptor.
2919 * @param buffer Address of the buffer to receive data, or NULL to defer data
2920 * retrieval and message disposal until later.
2921 * @param timeout Waiting period for a message to be received (in
2922 * milliseconds), or one of the special values K_NO_WAIT
2923 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002924 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002925 * @retval 0 Message received.
2926 * @retval -ENOMSG Returned without waiting.
2927 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002928 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002929extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002930 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002931
2932/**
2933 * @brief Retrieve mailbox message data into a buffer.
2934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * This routine completes the processing of a received message by retrieving
2936 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002937 *
2938 * Alternatively, this routine can be used to dispose of a received message
2939 * without retrieving its data.
2940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @param rx_msg Address of the receive message descriptor.
2942 * @param buffer Address of the buffer to receive data, or NULL to discard
2943 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002944 *
2945 * @return N/A
2946 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002947extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002948
2949/**
2950 * @brief Retrieve mailbox message data into a memory pool block.
2951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * This routine completes the processing of a received message by retrieving
2953 * its data into a memory pool block, then disposing of the message.
2954 * The memory pool block that results from successful retrieval must be
2955 * returned to the pool once the data has been processed, even in cases
2956 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002957 *
2958 * Alternatively, this routine can be used to dispose of a received message
2959 * without retrieving its data. In this case there is no need to return a
2960 * memory pool block to the pool.
2961 *
2962 * This routine allocates a new memory pool block for the data only if the
2963 * data is not already in one. If a new block cannot be allocated, the routine
2964 * returns a failure code and the received message is left unchanged. This
2965 * permits the caller to reattempt data retrieval at a later time or to dispose
2966 * of the received message without retrieving its data.
2967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * @param rx_msg Address of a receive message descriptor.
2969 * @param pool Address of memory pool, or NULL to discard data.
2970 * @param block Address of the area to hold memory pool block info.
2971 * @param timeout Waiting period to wait for a memory pool block (in
2972 * milliseconds), or one of the special values K_NO_WAIT
2973 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002974 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002975 * @retval 0 Data retrieved.
2976 * @retval -ENOMEM Returned without waiting.
2977 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002978 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002979extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002980 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002981 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982
Allan Stephensc98da842016-11-11 15:45:03 -05002983/**
2984 * @} end defgroup mailbox_apis
2985 */
2986
2987/**
2988 * @cond INTERNAL_HIDDEN
2989 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990
2991struct k_pipe {
2992 unsigned char *buffer; /* Pipe buffer: may be NULL */
2993 size_t size; /* Buffer size */
2994 size_t bytes_used; /* # bytes used in buffer */
2995 size_t read_index; /* Where in buffer to read from */
2996 size_t write_index; /* Where in buffer to write */
2997
2998 struct {
2999 _wait_q_t readers; /* Reader wait queue */
3000 _wait_q_t writers; /* Writer wait queue */
3001 } wait_q;
3002
Anas Nashif2f203c22016-12-18 06:57:45 -05003003 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004};
3005
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003006#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007 { \
3008 .buffer = pipe_buffer, \
3009 .size = pipe_buffer_size, \
3010 .bytes_used = 0, \
3011 .read_index = 0, \
3012 .write_index = 0, \
3013 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3014 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003015 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016 }
3017
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003018#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3019
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003020/**
Allan Stephensc98da842016-11-11 15:45:03 -05003021 * INTERNAL_HIDDEN @endcond
3022 */
3023
3024/**
3025 * @defgroup pipe_apis Pipe APIs
3026 * @ingroup kernel_apis
3027 * @{
3028 */
3029
3030/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003034 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003035 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @param name Name of the pipe.
3038 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3039 * or zero if no ring buffer is used.
3040 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003041 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003042#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3043 static unsigned char __noinit __aligned(pipe_align) \
3044 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003045 struct k_pipe name \
3046 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003047 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * This routine initializes a pipe object, prior to its first use.
3053 *
3054 * @param pipe Address of the pipe.
3055 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3056 * is used.
3057 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3058 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059 *
3060 * @return N/A
3061 */
3062extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3063 size_t size);
3064
3065/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003066 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003068 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003070 * @param pipe Address of the pipe.
3071 * @param data Address of data to write.
3072 * @param bytes_to_write Size of data (in bytes).
3073 * @param bytes_written Address of area to hold the number of bytes written.
3074 * @param min_xfer Minimum number of bytes to write.
3075 * @param timeout Waiting period to wait for the data to be written (in
3076 * milliseconds), or one of the special values K_NO_WAIT
3077 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003079 * @retval 0 At least @a min_xfer bytes of data were written.
3080 * @retval -EIO Returned without waiting; zero data bytes were written.
3081 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003082 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003084extern int k_pipe_put(struct k_pipe *pipe, void *data,
3085 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003086 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087
3088/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003091 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003093 * @param pipe Address of the pipe.
3094 * @param data Address to place the data read from pipe.
3095 * @param bytes_to_read Maximum number of data bytes to read.
3096 * @param bytes_read Address of area to hold the number of bytes read.
3097 * @param min_xfer Minimum number of data bytes to read.
3098 * @param timeout Waiting period to wait for the data to be read (in
3099 * milliseconds), or one of the special values K_NO_WAIT
3100 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003102 * @retval 0 At least @a min_xfer bytes of data were read.
3103 * @retval -EIO Returned without waiting; zero data bytes were read.
3104 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003105 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003107extern int k_pipe_get(struct k_pipe *pipe, void *data,
3108 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003109 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110
3111/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003114 * This routine writes the data contained in a memory block to @a pipe.
3115 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119 * @param block Memory block containing data to send
3120 * @param size Number of data bytes in memory block to send
3121 * @param sem Semaphore to signal upon completion (else NULL)
3122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003124 */
3125extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3126 size_t size, struct k_sem *sem);
3127
3128/**
Allan Stephensc98da842016-11-11 15:45:03 -05003129 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003130 */
3131
Allan Stephensc98da842016-11-11 15:45:03 -05003132/**
3133 * @cond INTERNAL_HIDDEN
3134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003136struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003137 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003138 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003139 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140 char *buffer;
3141 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003142 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003143
Anas Nashif2f203c22016-12-18 06:57:45 -05003144 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003145};
3146
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003147#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003148 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149 { \
3150 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003151 .num_blocks = slab_num_blocks, \
3152 .block_size = slab_block_size, \
3153 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154 .free_list = NULL, \
3155 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003156 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157 }
3158
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003159#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3160
3161
Peter Mitsis578f9112016-10-07 13:50:31 -04003162/**
Allan Stephensc98da842016-11-11 15:45:03 -05003163 * INTERNAL_HIDDEN @endcond
3164 */
3165
3166/**
3167 * @defgroup mem_slab_apis Memory Slab APIs
3168 * @ingroup kernel_apis
3169 * @{
3170 */
3171
3172/**
Allan Stephensda827222016-11-09 14:23:58 -06003173 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003174 *
Allan Stephensda827222016-11-09 14:23:58 -06003175 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003177 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3178 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003179 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003180 *
Allan Stephensda827222016-11-09 14:23:58 -06003181 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003183 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003184 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * @param name Name of the memory slab.
3187 * @param slab_block_size Size of each memory block (in bytes).
3188 * @param slab_num_blocks Number memory blocks.
3189 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003190 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003191#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3192 char __noinit __aligned(slab_align) \
3193 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3194 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003195 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003196 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003197 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003198
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003199/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003200 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003203 *
Allan Stephensda827222016-11-09 14:23:58 -06003204 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3205 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3206 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3207 * To ensure that each memory block is similarly aligned to this boundary,
3208 * @a slab_block_size must also be a multiple of N.
3209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @param slab Address of the memory slab.
3211 * @param buffer Pointer to buffer used for the memory blocks.
3212 * @param block_size Size of each memory block (in bytes).
3213 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003214 *
3215 * @return N/A
3216 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003217extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003218 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003219
3220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @param slab Address of the memory slab.
3226 * @param mem Pointer to block address area.
3227 * @param timeout Maximum time to wait for operation to complete
3228 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3229 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003230 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003231 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003233 * @retval -ENOMEM Returned without waiting.
3234 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003235 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003236extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003237 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003238
3239/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003240 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * This routine releases a previously allocated memory block back to its
3243 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003245 * @param slab Address of the memory slab.
3246 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003247 *
3248 * @return N/A
3249 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003250extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003252/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * This routine gets the number of memory blocks that are currently
3256 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003261 */
Kumar Galacc334c72017-04-21 10:55:34 -05003262static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003263{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003264 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265}
3266
Peter Mitsisc001aa82016-10-13 13:53:37 -04003267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * This routine gets the number of memory blocks that are currently
3271 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003276 */
Kumar Galacc334c72017-04-21 10:55:34 -05003277static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003278{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003279 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003280}
3281
Allan Stephensc98da842016-11-11 15:45:03 -05003282/**
3283 * @} end defgroup mem_slab_apis
3284 */
3285
3286/**
3287 * @cond INTERNAL_HIDDEN
3288 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289
Andy Ross73cb9582017-05-09 10:42:39 -07003290struct k_mem_pool_lvl {
3291 union {
3292 u32_t *bits_p;
3293 u32_t bits;
3294 };
3295 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003296};
3297
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003298struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003299 void *buf;
3300 size_t max_sz;
3301 u16_t n_max;
3302 u8_t n_levels;
3303 u8_t max_inline_level;
3304 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003305 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306};
3307
Andy Ross73cb9582017-05-09 10:42:39 -07003308#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309
Andy Ross73cb9582017-05-09 10:42:39 -07003310#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3311
3312#define _MPOOL_LVLS(maxsz, minsz) \
3313 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3314 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3315 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3316 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3317 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3318 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3319 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3320 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3321 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3322 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3323 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3324 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3325 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3326 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3327 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3328 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3329
3330/* Rounds the needed bits up to integer multiples of u32_t */
3331#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3332 ((((n_max) << (2*(l))) + 31) / 32)
3333
3334/* One word gets stored free unioned with the pointer, otherwise the
3335 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003336 */
Andy Ross73cb9582017-05-09 10:42:39 -07003337#define _MPOOL_LBIT_WORDS(n_max, l) \
3338 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3339 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003340
Andy Ross73cb9582017-05-09 10:42:39 -07003341/* How many bytes for the bitfields of a single level? */
3342#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3343 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3344 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003345
Andy Ross73cb9582017-05-09 10:42:39 -07003346/* Size of the bitmap array that follows the buffer in allocated memory */
3347#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3348 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3349 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3350 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3351 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3352 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3353 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3354 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3355 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3356 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3357 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3358 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3359 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3360 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3361 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3362 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3363 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003364
3365/**
Allan Stephensc98da842016-11-11 15:45:03 -05003366 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003367 */
3368
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003369/**
Allan Stephensc98da842016-11-11 15:45:03 -05003370 * @addtogroup mem_pool_apis
3371 * @{
3372 */
3373
3374/**
3375 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3378 * long. The memory pool allows blocks to be repeatedly partitioned into
3379 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003380 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003381 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003382 * If the pool is to be accessed outside the module where it is defined, it
3383 * can be declared via
3384 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003385 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003388 * @param minsz Size of the smallest blocks in the pool (in bytes).
3389 * @param maxsz Size of the largest blocks in the pool (in bytes).
3390 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003392 */
Andy Ross73cb9582017-05-09 10:42:39 -07003393#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3394 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3395 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3396 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3397 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3398 .buf = _mpool_buf_##name, \
3399 .max_sz = maxsz, \
3400 .n_max = nmax, \
3401 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3402 .levels = _mpool_lvls_##name, \
3403 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003404
Peter Mitsis937042c2016-10-13 13:18:26 -04003405/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * @param pool Address of the memory pool.
3411 * @param block Pointer to block descriptor for the allocated memory.
3412 * @param size Amount of memory to allocate (in bytes).
3413 * @param timeout Maximum time to wait for operation to complete
3414 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3415 * or K_FOREVER to wait as long as necessary.
3416 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003417 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003419 * @retval -ENOMEM Returned without waiting.
3420 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003421 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003422extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003423 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003424
3425/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * This routine releases a previously allocated memory block back to its
3429 * memory pool.
3430 *
3431 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003432 *
3433 * @return N/A
3434 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003435extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003436
3437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003439 *
Andy Ross73cb9582017-05-09 10:42:39 -07003440 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003441 *
Andy Ross73cb9582017-05-09 10:42:39 -07003442 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003443 *
3444 * @return N/A
3445 */
Andy Ross73cb9582017-05-09 10:42:39 -07003446static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003447
3448/**
Allan Stephensc98da842016-11-11 15:45:03 -05003449 * @} end addtogroup mem_pool_apis
3450 */
3451
3452/**
3453 * @defgroup heap_apis Heap Memory Pool APIs
3454 * @ingroup kernel_apis
3455 * @{
3456 */
3457
3458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003462 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003464 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003466 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003467 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003468extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003469
3470/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003472 *
3473 * This routine provides traditional free() semantics. The memory being
3474 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003475 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003476 * If @a ptr is NULL, no operation is performed.
3477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003479 *
3480 * @return N/A
3481 */
3482extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483
Allan Stephensc98da842016-11-11 15:45:03 -05003484/**
3485 * @} end defgroup heap_apis
3486 */
3487
Benjamin Walshacc68c12017-01-29 18:57:45 -05003488/* polling API - PRIVATE */
3489
Benjamin Walshb0179862017-02-02 16:39:57 -05003490#ifdef CONFIG_POLL
3491#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3492#else
3493#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3494#endif
3495
Benjamin Walshacc68c12017-01-29 18:57:45 -05003496/* private - implementation data created as needed, per-type */
3497struct _poller {
3498 struct k_thread *thread;
3499};
3500
3501/* private - types bit positions */
3502enum _poll_types_bits {
3503 /* can be used to ignore an event */
3504 _POLL_TYPE_IGNORE,
3505
3506 /* to be signaled by k_poll_signal() */
3507 _POLL_TYPE_SIGNAL,
3508
3509 /* semaphore availability */
3510 _POLL_TYPE_SEM_AVAILABLE,
3511
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003512 /* queue/fifo/lifo data availability */
3513 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003514
3515 _POLL_NUM_TYPES
3516};
3517
3518#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3519
3520/* private - states bit positions */
3521enum _poll_states_bits {
3522 /* default state when creating event */
3523 _POLL_STATE_NOT_READY,
3524
3525 /* there was another poller already on the object */
3526 _POLL_STATE_EADDRINUSE,
3527
3528 /* signaled by k_poll_signal() */
3529 _POLL_STATE_SIGNALED,
3530
3531 /* semaphore is available */
3532 _POLL_STATE_SEM_AVAILABLE,
3533
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003534 /* data is available to read on queue/fifo/lifo */
3535 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003536
3537 _POLL_NUM_STATES
3538};
3539
3540#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3541
3542#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003543 (32 - (0 \
3544 + 8 /* tag */ \
3545 + _POLL_NUM_TYPES \
3546 + _POLL_NUM_STATES \
3547 + 1 /* modes */ \
3548 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003549
3550#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3551#error overflow of 32-bit word in struct k_poll_event
3552#endif
3553
3554/* end of polling API - PRIVATE */
3555
3556
3557/**
3558 * @defgroup poll_apis Async polling APIs
3559 * @ingroup kernel_apis
3560 * @{
3561 */
3562
3563/* Public polling API */
3564
3565/* public - values for k_poll_event.type bitfield */
3566#define K_POLL_TYPE_IGNORE 0
3567#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3568#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003569#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3570#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003571
3572/* public - polling modes */
3573enum k_poll_modes {
3574 /* polling thread does not take ownership of objects when available */
3575 K_POLL_MODE_NOTIFY_ONLY = 0,
3576
3577 K_POLL_NUM_MODES
3578};
3579
3580/* public - values for k_poll_event.state bitfield */
3581#define K_POLL_STATE_NOT_READY 0
3582#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3583#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3584#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003585#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3586#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003587
3588/* public - poll signal object */
3589struct k_poll_signal {
3590 /* PRIVATE - DO NOT TOUCH */
3591 struct k_poll_event *poll_event;
3592
3593 /*
3594 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3595 * user resets it to 0.
3596 */
3597 unsigned int signaled;
3598
3599 /* custom result value passed to k_poll_signal() if needed */
3600 int result;
3601};
3602
3603#define K_POLL_SIGNAL_INITIALIZER() \
3604 { \
3605 .poll_event = NULL, \
3606 .signaled = 0, \
3607 .result = 0, \
3608 }
3609
3610struct k_poll_event {
3611 /* PRIVATE - DO NOT TOUCH */
3612 struct _poller *poller;
3613
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003614 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003615 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003616
Benjamin Walshacc68c12017-01-29 18:57:45 -05003617 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003618 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003619
3620 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003621 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003622
3623 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003624 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003625
3626 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003627 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003628
3629 /* per-type data */
3630 union {
3631 void *obj;
3632 struct k_poll_signal *signal;
3633 struct k_sem *sem;
3634 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003635 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003636 };
3637};
3638
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003639#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003640 { \
3641 .poller = NULL, \
3642 .type = event_type, \
3643 .state = K_POLL_STATE_NOT_READY, \
3644 .mode = event_mode, \
3645 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003646 { .obj = event_obj }, \
3647 }
3648
3649#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3650 event_tag) \
3651 { \
3652 .type = event_type, \
3653 .tag = event_tag, \
3654 .state = K_POLL_STATE_NOT_READY, \
3655 .mode = event_mode, \
3656 .unused = 0, \
3657 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003658 }
3659
3660/**
3661 * @brief Initialize one struct k_poll_event instance
3662 *
3663 * After this routine is called on a poll event, the event it ready to be
3664 * placed in an event array to be passed to k_poll().
3665 *
3666 * @param event The event to initialize.
3667 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3668 * values. Only values that apply to the same object being polled
3669 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3670 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003671 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003672 * @param obj Kernel object or poll signal.
3673 *
3674 * @return N/A
3675 */
3676
Kumar Galacc334c72017-04-21 10:55:34 -05003677extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003678 int mode, void *obj);
3679
3680/**
3681 * @brief Wait for one or many of multiple poll events to occur
3682 *
3683 * This routine allows a thread to wait concurrently for one or many of
3684 * multiple poll events to have occurred. Such events can be a kernel object
3685 * being available, like a semaphore, or a poll signal event.
3686 *
3687 * When an event notifies that a kernel object is available, the kernel object
3688 * is not "given" to the thread calling k_poll(): it merely signals the fact
3689 * that the object was available when the k_poll() call was in effect. Also,
3690 * all threads trying to acquire an object the regular way, i.e. by pending on
3691 * the object, have precedence over the thread polling on the object. This
3692 * means that the polling thread will never get the poll event on an object
3693 * until the object becomes available and its pend queue is empty. For this
3694 * reason, the k_poll() call is more effective when the objects being polled
3695 * only have one thread, the polling thread, trying to acquire them.
3696 *
3697 * Only one thread can be polling for a particular object at a given time. If
3698 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3699 * and returns as soon as it has finished handling the other events. This means
3700 * that k_poll() can return -EADDRINUSE and have the state value of some events
3701 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3702 * parameter is ignored.
3703 *
3704 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3705 * events that were passed to k_poll() and check the state field for the values
3706 * that were expected and take the associated actions.
3707 *
3708 * Before being reused for another call to k_poll(), the user has to reset the
3709 * state field to K_POLL_STATE_NOT_READY.
3710 *
3711 * @param events An array of pointers to events to be polled for.
3712 * @param num_events The number of events in the array.
3713 * @param timeout Waiting period for an event to be ready (in milliseconds),
3714 * or one of the special values K_NO_WAIT and K_FOREVER.
3715 *
3716 * @retval 0 One or more events are ready.
3717 * @retval -EADDRINUSE One or more objects already had a poller.
3718 * @retval -EAGAIN Waiting period timed out.
3719 */
3720
3721extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003722 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003723
3724/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003725 * @brief Initialize a poll signal object.
3726 *
3727 * Ready a poll signal object to be signaled via k_poll_signal().
3728 *
3729 * @param signal A poll signal.
3730 *
3731 * @return N/A
3732 */
3733
3734extern void k_poll_signal_init(struct k_poll_signal *signal);
3735
3736/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003737 * @brief Signal a poll signal object.
3738 *
3739 * This routine makes ready a poll signal, which is basically a poll event of
3740 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3741 * made ready to run. A @a result value can be specified.
3742 *
3743 * The poll signal contains a 'signaled' field that, when set by
3744 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3745 * be reset by the user before being passed again to k_poll() or k_poll() will
3746 * consider it being signaled, and will return immediately.
3747 *
3748 * @param signal A poll signal.
3749 * @param result The value to store in the result field of the signal.
3750 *
3751 * @retval 0 The signal was delivered successfully.
3752 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3753 */
3754
3755extern int k_poll_signal(struct k_poll_signal *signal, int result);
3756
3757/* private internal function */
3758extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003759 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003760
3761/**
3762 * @} end defgroup poll_apis
3763 */
3764
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003765/**
3766 * @brief Make the CPU idle.
3767 *
3768 * This function makes the CPU idle until an event wakes it up.
3769 *
3770 * In a regular system, the idle thread should be the only thread responsible
3771 * for making the CPU idle and triggering any type of power management.
3772 * However, in some more constrained systems, such as a single-threaded system,
3773 * the only thread would be responsible for this if needed.
3774 *
3775 * @return N/A
3776 */
3777extern void k_cpu_idle(void);
3778
3779/**
3780 * @brief Make the CPU idle in an atomic fashion.
3781 *
3782 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3783 * must be done atomically before making the CPU idle.
3784 *
3785 * @param key Interrupt locking key obtained from irq_lock().
3786 *
3787 * @return N/A
3788 */
3789extern void k_cpu_atomic_idle(unsigned int key);
3790
Kumar Galacc334c72017-04-21 10:55:34 -05003791extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003792
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003793#include <arch/cpu.h>
3794
Andrew Boiecdb94d62017-04-18 15:22:05 -07003795#ifdef _ARCH_EXCEPT
3796/* This archtecture has direct support for triggering a CPU exception */
3797#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3798#else
3799
3800#include <misc/printk.h>
3801
3802/* NOTE: This is the implementation for arches that do not implement
3803 * _ARCH_EXCEPT() to generate a real CPU exception.
3804 *
3805 * We won't have a real exception frame to determine the PC value when
3806 * the oops occurred, so print file and line number before we jump into
3807 * the fatal error handler.
3808 */
3809#define _k_except_reason(reason) do { \
3810 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3811 _NanoFatalErrorHandler(reason, &_default_esf); \
3812 CODE_UNREACHABLE; \
3813 } while (0)
3814
3815#endif /* _ARCH__EXCEPT */
3816
3817/**
3818 * @brief Fatally terminate a thread
3819 *
3820 * This should be called when a thread has encountered an unrecoverable
3821 * runtime condition and needs to terminate. What this ultimately
3822 * means is determined by the _fatal_error_handler() implementation, which
3823 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3824 *
3825 * If this is called from ISR context, the default system fatal error handler
3826 * will treat it as an unrecoverable system error, just like k_panic().
3827 */
3828#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3829
3830/**
3831 * @brief Fatally terminate the system
3832 *
3833 * This should be called when the Zephyr kernel has encountered an
3834 * unrecoverable runtime condition and needs to terminate. What this ultimately
3835 * means is determined by the _fatal_error_handler() implementation, which
3836 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3837 */
3838#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3839
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003840/*
3841 * private APIs that are utilized by one or more public APIs
3842 */
3843
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003844#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003846#else
3847#define _init_static_threads() do { } while ((0))
3848#endif
3849
3850extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003851extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852
Andrew Boiedc5d9352017-06-02 12:56:47 -07003853/* arch/cpu.h may declare an architecture or platform-specific macro
3854 * for properly declaring stacks, compatible with MMU/MPU constraints if
3855 * enabled
3856 */
3857#ifdef _ARCH_THREAD_STACK_DEFINE
3858#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3859#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3860 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3861#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3862#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003863static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3864{
3865 return _ARCH_THREAD_STACK_BUFFER(sym);
3866}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003867#else
3868/**
3869 * @brief Declare a toplevel thread stack memory region
3870 *
3871 * This declares a region of memory suitable for use as a thread's stack.
3872 *
3873 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3874 * 'noinit' section so that it isn't zeroed at boot
3875 *
Andrew Boie507852a2017-07-25 18:47:07 -07003876 * The declared symbol will always be a k_thread_stack_t which can be passed to
3877 * k_thread_create, but should otherwise not be manipulated. If the buffer
3878 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003879 *
3880 * It is legal to precede this definition with the 'static' keyword.
3881 *
3882 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3883 * parameter of k_thread_create(), it may not be the same as the
3884 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3885 *
3886 * @param sym Thread stack symbol name
3887 * @param size Size of the stack memory region
3888 */
3889#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003890 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003891
3892/**
3893 * @brief Declare a toplevel array of thread stack memory regions
3894 *
3895 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3896 * definition for additional details and constraints.
3897 *
3898 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3899 * 'noinit' section so that it isn't zeroed at boot
3900 *
3901 * @param sym Thread stack symbol name
3902 * @param nmemb Number of stacks to declare
3903 * @param size Size of the stack memory region
3904 */
3905
3906#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003907 struct _k_thread_stack_element __noinit \
3908 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003909
3910/**
3911 * @brief Declare an embedded stack memory region
3912 *
3913 * Used for stacks embedded within other data structures. Use is highly
3914 * discouraged but in some cases necessary. For memory protection scenarios,
3915 * it is very important that any RAM preceding this member not be writable
3916 * by threads else a stack overflow will lead to silent corruption. In other
3917 * words, the containing data structure should live in RAM owned by the kernel.
3918 *
3919 * @param sym Thread stack symbol name
3920 * @param size Size of the stack memory region
3921 */
3922#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003923 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003924
3925/**
3926 * @brief Return the size in bytes of a stack memory region
3927 *
3928 * Convenience macro for passing the desired stack size to k_thread_create()
3929 * since the underlying implementation may actually create something larger
3930 * (for instance a guard area).
3931 *
3932 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07003933 * passed to K_THREAD_STACK_DEFINE.
3934 *
3935 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
3936 * it is not guaranteed to return the original value since each array
3937 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07003938 *
3939 * @param sym Stack memory symbol
3940 * @return Size of the stack
3941 */
3942#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
3943
3944/**
3945 * @brief Get a pointer to the physical stack buffer
3946 *
3947 * Convenience macro to get at the real underlying stack buffer used by
3948 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
3949 * This is really only intended for diagnostic tools which want to examine
3950 * stack memory contents.
3951 *
3952 * @param sym Declared stack symbol name
3953 * @return The buffer itself, a char *
3954 */
Andrew Boie507852a2017-07-25 18:47:07 -07003955static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3956{
3957 return (char *)sym;
3958}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003959
3960#endif /* _ARCH_DECLARE_STACK */
3961
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003962#ifdef __cplusplus
3963}
3964#endif
3965
Andrew Boiee004dec2016-11-07 09:01:19 -08003966#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3967/*
3968 * Define new and delete operators.
3969 * At this moment, the operators do nothing since objects are supposed
3970 * to be statically allocated.
3971 */
3972inline void operator delete(void *ptr)
3973{
3974 (void)ptr;
3975}
3976
3977inline void operator delete[](void *ptr)
3978{
3979 (void)ptr;
3980}
3981
3982inline void *operator new(size_t size)
3983{
3984 (void)size;
3985 return NULL;
3986}
3987
3988inline void *operator new[](size_t size)
3989{
3990 (void)size;
3991 return NULL;
3992}
3993
3994/* Placement versions of operator new and delete */
3995inline void operator delete(void *ptr1, void *ptr2)
3996{
3997 (void)ptr1;
3998 (void)ptr2;
3999}
4000
4001inline void operator delete[](void *ptr1, void *ptr2)
4002{
4003 (void)ptr1;
4004 (void)ptr2;
4005}
4006
4007inline void *operator new(size_t size, void *ptr)
4008{
4009 (void)size;
4010 return ptr;
4011}
4012
4013inline void *operator new[](size_t size, void *ptr)
4014{
4015 (void)size;
4016 return ptr;
4017}
4018
4019#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4020
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004021#endif /* !_ASMLANGUAGE */
4022
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004023#endif /* _kernel__h_ */