blob: 667bbd6a026b1b5d5106511db5306eb4cde96ee1 [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>
21#include <sections.h>
22#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
101#define _POLL_EVENT_OBJ_INIT \
102 .poll_event = NULL,
103#define _POLL_EVENT struct k_poll_event *poll_event
104#else
105#define _POLL_EVENT_OBJ_INIT
106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Andrew Boie73abd322017-04-04 13:19:13 -0700126/* timeouts */
127
128struct _timeout;
129typedef void (*_timeout_func_t)(struct _timeout *t);
130
131struct _timeout {
132 sys_dnode_t node;
133 struct k_thread *thread;
134 sys_dlist_t *wait_q;
135 s32_t delta_ticks_from_prev;
136 _timeout_func_t func;
137};
138
139extern s32_t _timeout_remaining_get(struct _timeout *timeout);
140
141/* Threads */
142typedef void (*_thread_entry_t)(void *, void *, void *);
143
144#ifdef CONFIG_THREAD_MONITOR
145struct __thread_entry {
146 _thread_entry_t pEntry;
147 void *parameter1;
148 void *parameter2;
149 void *parameter3;
150};
151#endif
152
153/* can be used for creating 'dummy' threads, e.g. for pending on objects */
154struct _thread_base {
155
156 /* this thread's entry in a ready/wait queue */
157 sys_dnode_t k_q_node;
158
159 /* user facing 'thread options'; values defined in include/kernel.h */
160 u8_t user_options;
161
162 /* thread state */
163 u8_t thread_state;
164
165 /*
166 * scheduler lock count and thread priority
167 *
168 * These two fields control the preemptibility of a thread.
169 *
170 * When the scheduler is locked, sched_locked is decremented, which
171 * means that the scheduler is locked for values from 0xff to 0x01. A
172 * thread is coop if its prio is negative, thus 0x80 to 0xff when
173 * looked at the value as unsigned.
174 *
175 * By putting them end-to-end, this means that a thread is
176 * non-preemptible if the bundled value is greater than or equal to
177 * 0x0080.
178 */
179 union {
180 struct {
181#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
182 u8_t sched_locked;
183 s8_t prio;
184#else /* LITTLE and PDP */
185 s8_t prio;
186 u8_t sched_locked;
187#endif
188 };
189 u16_t preempt;
190 };
191
192 /* data returned by APIs */
193 void *swap_data;
194
195#ifdef CONFIG_SYS_CLOCK_EXISTS
196 /* this thread's entry in a timeout queue */
197 struct _timeout timeout;
198#endif
199
200};
201
202typedef struct _thread_base _thread_base_t;
203
204#if defined(CONFIG_THREAD_STACK_INFO)
205/* Contains the stack information of a thread */
206struct _thread_stack_info {
207 /* Stack Start */
208 u32_t start;
209 /* Stack Size */
210 u32_t size;
211};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700212
213typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700214#endif /* CONFIG_THREAD_STACK_INFO */
215
216struct k_thread {
217
218 struct _thread_base base;
219
220 /* defined by the architecture, but all archs need these */
221 struct _caller_saved caller_saved;
222 struct _callee_saved callee_saved;
223
224 /* static thread init data */
225 void *init_data;
226
227 /* abort function */
228 void (*fn_abort)(void);
229
230#if defined(CONFIG_THREAD_MONITOR)
231 /* thread entry and parameters description */
232 struct __thread_entry *entry;
233
234 /* next item in list of all threads */
235 struct k_thread *next_thread;
236#endif
237
238#ifdef CONFIG_THREAD_CUSTOM_DATA
239 /* crude thread-local storage */
240 void *custom_data;
241#endif
242
243#ifdef CONFIG_ERRNO
244 /* per-thread errno variable */
245 int errno_var;
246#endif
247
248#if defined(CONFIG_THREAD_STACK_INFO)
249 /* Stack Info */
250 struct _thread_stack_info stack_info;
251#endif /* CONFIG_THREAD_STACK_INFO */
252
253 /* arch-specifics: must always be at the end */
254 struct _thread_arch arch;
255};
256
257typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400258typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700259#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261enum execution_context_types {
262 K_ISR = 0,
263 K_COOP_THREAD,
264 K_PREEMPT_THREAD,
265};
266
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100268 * @defgroup profiling_apis Profiling APIs
269 * @ingroup kernel_apis
270 * @{
271 */
272
273/**
274 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
275 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700276 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100277 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
278 *
279 * CONFIG_MAIN_STACK_SIZE
280 * CONFIG_IDLE_STACK_SIZE
281 * CONFIG_ISR_STACK_SIZE
282 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
283 *
284 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
285 * produce output.
286 *
287 * @return N/A
288 */
289extern void k_call_stacks_analyze(void);
290
291/**
292 * @} end defgroup profiling_apis
293 */
294
295/**
Allan Stephensc98da842016-11-11 15:45:03 -0500296 * @defgroup thread_apis Thread APIs
297 * @ingroup kernel_apis
298 * @{
299 */
300
301/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500302 * @typedef k_thread_entry_t
303 * @brief Thread entry point function type.
304 *
305 * A thread's entry point function is invoked when the thread starts executing.
306 * Up to 3 argument values can be passed to the function.
307 *
308 * The thread terminates execution permanently if the entry point function
309 * returns. The thread is responsible for releasing any shared resources
310 * it may own (such as mutexes and dynamically allocated memory), prior to
311 * returning.
312 *
313 * @param p1 First argument.
314 * @param p2 Second argument.
315 * @param p3 Third argument.
316 *
317 * @return N/A
318 */
319typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
320
Benjamin Walshed240f22017-01-22 13:05:08 -0500321#endif /* !_ASMLANGUAGE */
322
323
324/*
325 * Thread user options. May be needed by assembly code. Common part uses low
326 * bits, arch-specific use high bits.
327 */
328
329/* system thread that must not abort */
330#define K_ESSENTIAL (1 << 0)
331
332#if defined(CONFIG_FP_SHARING)
333/* thread uses floating point registers */
334#define K_FP_REGS (1 << 1)
335#endif
336
337#ifdef CONFIG_X86
338/* x86 Bitmask definitions for threads user options */
339
340#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
341/* thread uses SSEx (and also FP) registers */
342#define K_SSE_REGS (1 << 7)
343#endif
344#endif
345
346/* end - thread options */
347
348#if !defined(_ASMLANGUAGE)
349
Allan Stephens5eceb852016-11-16 10:16:30 -0500350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500353 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500355 * The new thread may be scheduled for immediate execution or a delayed start.
356 * If the newly spawned thread does not have a delayed start the kernel
357 * scheduler may preempt the current thread to allow the new thread to
358 * execute.
359 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700360 * Kernel data structures for bookkeeping and context storage for this thread
361 * will be placed at the beginning of the thread's stack memory region and may
362 * become corrupted if too much of the stack is used. This function has been
363 * deprecated in favor of k_thread_create() to give the user more control on
364 * where these data structures reside.
365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500366 * Thread options are architecture-specific, and can include K_ESSENTIAL,
367 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
368 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400369 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700370 * The stack itself should be declared with K_THREAD_STACK_DEFINE or variant
371 * macros. The stack size parameter should either be a defined constant
372 * also passed to K_THREAD_STACK_DEFINE, or the value of K_THREAD_STACK_SIZEOF.
373 * Do not use regular C sizeof().
374 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400375 * @param stack Pointer to the stack space.
376 * @param stack_size Stack size in bytes.
377 * @param entry Thread entry function.
378 * @param p1 1st entry point parameter.
379 * @param p2 2nd entry point parameter.
380 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @param prio Thread priority.
382 * @param options Thread options.
383 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500385 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386 */
Andrew Boied26cf2d2017-03-30 13:07:02 -0700387extern __deprecated k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500388 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400389 void *p1, void *p2, void *p3,
Kumar Galacc334c72017-04-21 10:55:34 -0500390 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400391
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400392/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700393 * @brief Create a thread.
394 *
395 * This routine initializes a thread, then schedules it for execution.
396 *
397 * The new thread may be scheduled for immediate execution or a delayed start.
398 * If the newly spawned thread does not have a delayed start the kernel
399 * scheduler may preempt the current thread to allow the new thread to
400 * execute.
401 *
402 * Thread options are architecture-specific, and can include K_ESSENTIAL,
403 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
404 * them using "|" (the logical OR operator).
405 *
406 * Historically, users often would use the beginning of the stack memory region
407 * to store the struct k_thread data, although corruption will occur if the
408 * stack overflows this region and stack protection features may not detect this
409 * situation.
410 *
411 * @param new_thread Pointer to uninitialized struct k_thread
412 * @param stack Pointer to the stack space.
413 * @param stack_size Stack size in bytes.
414 * @param entry Thread entry function.
415 * @param p1 1st entry point parameter.
416 * @param p2 2nd entry point parameter.
417 * @param p3 3rd entry point parameter.
418 * @param prio Thread priority.
419 * @param options Thread options.
420 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
421 *
422 * @return ID of new thread.
423 */
424extern k_tid_t k_thread_create(struct k_thread *new_thread, char *stack,
425 size_t stack_size,
426 void (*entry)(void *, void *, void*),
427 void *p1, void *p2, void *p3,
428 int prio, u32_t options, s32_t delay);
429
430/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432 *
Allan Stephensc98da842016-11-11 15:45:03 -0500433 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
438 * @return N/A
439 */
Kumar Galacc334c72017-04-21 10:55:34 -0500440extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441
442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500443 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444 *
445 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500446 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 * @return N/A
449 */
Kumar Galacc334c72017-04-21 10:55:34 -0500450extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451
452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500453 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500455 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
459 * @return N/A
460 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400461extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462
463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500466 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500468 * If @a thread is not currently sleeping, the routine has no effect.
469 *
470 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
472 * @return N/A
473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475
476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500477 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400481extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400482
483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500486 * This routine prevents @a thread from executing if it has not yet started
487 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * @param thread ID of thread to cancel.
490 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700491 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500492 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400493 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400494extern int k_thread_cancel(k_tid_t thread);
495
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400496/**
Allan Stephensc98da842016-11-11 15:45:03 -0500497 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500499 * This routine permanently stops execution of @a thread. The thread is taken
500 * off all kernel queues it is part of (i.e. the ready queue, the timeout
501 * queue, or a kernel object wait queue). However, any kernel resources the
502 * thread might currently own (such as mutexes or memory blocks) are not
503 * released. It is the responsibility of the caller of this routine to ensure
504 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500506 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400507 *
508 * @return N/A
509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400510extern void k_thread_abort(k_tid_t thread);
511
Allan Stephensc98da842016-11-11 15:45:03 -0500512/**
513 * @cond INTERNAL_HIDDEN
514 */
515
Benjamin Walshd211a522016-12-06 11:44:01 -0500516/* timeout has timed out and is not on _timeout_q anymore */
517#define _EXPIRED (-2)
518
519/* timeout is not in use */
520#define _INACTIVE (-1)
521
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400522struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700523 struct k_thread *init_thread;
524 char *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400525 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500526 void (*init_entry)(void *, void *, void *);
527 void *init_p1;
528 void *init_p2;
529 void *init_p3;
530 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500531 u32_t init_options;
532 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500533 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500534 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400535};
536
Andrew Boied26cf2d2017-03-30 13:07:02 -0700537#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400538 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500539 prio, options, delay, abort, groups) \
540 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700541 .init_thread = (thread), \
542 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500543 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400544 .init_entry = (void (*)(void *, void *, void *))entry, \
545 .init_p1 = (void *)p1, \
546 .init_p2 = (void *)p2, \
547 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500548 .init_prio = (prio), \
549 .init_options = (options), \
550 .init_delay = (delay), \
551 .init_abort = (abort), \
552 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400553 }
554
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400555/**
Allan Stephensc98da842016-11-11 15:45:03 -0500556 * INTERNAL_HIDDEN @endcond
557 */
558
559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * @brief Statically define and initialize a thread.
561 *
562 * The thread may be scheduled for immediate execution or a delayed start.
563 *
564 * Thread options are architecture-specific, and can include K_ESSENTIAL,
565 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
566 * them using "|" (the logical OR operator).
567 *
568 * The ID of the thread can be accessed using:
569 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500570 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500571 *
572 * @param name Name of the thread.
573 * @param stack_size Stack size in bytes.
574 * @param entry Thread entry function.
575 * @param p1 1st entry point parameter.
576 * @param p2 2nd entry point parameter.
577 * @param p3 3rd entry point parameter.
578 * @param prio Thread priority.
579 * @param options Thread options.
580 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400581 *
582 * @internal It has been observed that the x86 compiler by default aligns
583 * these _static_thread_data structures to 32-byte boundaries, thereby
584 * wasting space. To work around this, force a 4-byte alignment.
585 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500586#define K_THREAD_DEFINE(name, stack_size, \
587 entry, p1, p2, p3, \
588 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700589 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700590 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500591 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500592 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700593 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
594 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500595 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700596 NULL, 0); \
597 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @param thread ID of thread whose priority is needed.
605 *
606 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500608extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609
610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500613 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 *
615 * Rescheduling can occur immediately depending on the priority @a thread is
616 * set to:
617 *
618 * - If its priority is raised above the priority of the caller of this
619 * function, and the caller is preemptible, @a thread will be scheduled in.
620 *
621 * - If the caller operates on itself, it lowers its priority below that of
622 * other threads in the system, and the caller is preemptible, the thread of
623 * highest priority will be scheduled in.
624 *
625 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
626 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
627 * highest priority.
628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500629 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630 * @param prio New priority.
631 *
632 * @warning Changing the priority of a thread currently involved in mutex
633 * priority inheritance may result in undefined behavior.
634 *
635 * @return N/A
636 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400637extern void k_thread_priority_set(k_tid_t thread, int prio);
638
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500640 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * This routine prevents the kernel scheduler from making @a thread the
643 * current thread. All other internal operations on @a thread are still
644 * performed; for example, any timeout it is waiting on keeps ticking,
645 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500647 * If @a thread is already suspended, the routine has no effect.
648 *
649 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 *
651 * @return N/A
652 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400653extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400654
655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500658 * This routine allows the kernel scheduler to make @a thread the current
659 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * If @a thread is not currently suspended, the routine has no effect.
662 *
663 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
665 * @return N/A
666 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400667extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * This routine specifies how the scheduler will perform time slicing of
673 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500675 * To enable time slicing, @a slice must be non-zero. The scheduler
676 * ensures that no thread runs for more than the specified time limit
677 * before other threads of that priority are given a chance to execute.
678 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700679 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400682 * execute. Once the scheduler selects a thread for execution, there is no
683 * minimum guaranteed time the thread will execute before threads of greater or
684 * equal priority are scheduled.
685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500686 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687 * for execution, this routine has no effect; the thread is immediately
688 * rescheduled after the slice period expires.
689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * To disable timeslicing, set both @a slice and @a prio to zero.
691 *
692 * @param slice Maximum time slice length (in milliseconds).
693 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694 *
695 * @return N/A
696 */
Kumar Galacc334c72017-04-21 10:55:34 -0500697extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400698
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699/**
Allan Stephensc98da842016-11-11 15:45:03 -0500700 * @} end defgroup thread_apis
701 */
702
703/**
704 * @addtogroup isr_apis
705 * @{
706 */
707
708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
Allan Stephensc98da842016-11-11 15:45:03 -0500711 * This routine allows the caller to customize its actions, depending on
712 * whether it is a thread or an ISR.
713 *
714 * @note Can be called by ISRs.
715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500716 * @return 0 if invoked by a thread.
717 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500719extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400720
Benjamin Walsh445830d2016-11-10 15:54:27 -0500721/**
722 * @brief Determine if code is running in a preemptible thread.
723 *
Allan Stephensc98da842016-11-11 15:45:03 -0500724 * This routine allows the caller to customize its actions, depending on
725 * whether it can be preempted by another thread. The routine returns a 'true'
726 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500727 *
Allan Stephensc98da842016-11-11 15:45:03 -0500728 * - The code is running in a thread, not at ISR.
729 * - The thread's priority is in the preemptible range.
730 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500731 *
Allan Stephensc98da842016-11-11 15:45:03 -0500732 * @note Can be called by ISRs.
733 *
734 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500735 * @return Non-zero if invoked by a preemptible thread.
736 */
737extern int k_is_preempt_thread(void);
738
Allan Stephensc98da842016-11-11 15:45:03 -0500739/**
740 * @} end addtogroup isr_apis
741 */
742
743/**
744 * @addtogroup thread_apis
745 * @{
746 */
747
748/**
749 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500750 *
Allan Stephensc98da842016-11-11 15:45:03 -0500751 * This routine prevents the current thread from being preempted by another
752 * thread by instructing the scheduler to treat it as a cooperative thread.
753 * If the thread subsequently performs an operation that makes it unready,
754 * it will be context switched out in the normal manner. When the thread
755 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500756 *
Allan Stephensc98da842016-11-11 15:45:03 -0500757 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500758 *
Allan Stephensc98da842016-11-11 15:45:03 -0500759 * @note k_sched_lock() and k_sched_unlock() should normally be used
760 * when the operation being performed can be safely interrupted by ISRs.
761 * However, if the amount of processing involved is very small, better
762 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500763 *
764 * @return N/A
765 */
766extern void k_sched_lock(void);
767
Allan Stephensc98da842016-11-11 15:45:03 -0500768/**
769 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500770 *
Allan Stephensc98da842016-11-11 15:45:03 -0500771 * This routine reverses the effect of a previous call to k_sched_lock().
772 * A thread must call the routine once for each time it called k_sched_lock()
773 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500774 *
775 * @return N/A
776 */
777extern void k_sched_unlock(void);
778
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * Custom data is not used by the kernel itself, and is freely available
785 * for a thread to use as it sees fit. It can be used as a framework
786 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
790 * @return N/A
791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400801extern void *k_thread_custom_data_get(void);
802
803/**
Allan Stephensc98da842016-11-11 15:45:03 -0500804 * @} end addtogroup thread_apis
805 */
806
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400807#include <sys_clock.h>
808
Allan Stephensc2f15a42016-11-17 12:24:22 -0500809/**
810 * @addtogroup clock_apis
811 * @{
812 */
813
814/**
815 * @brief Generate null timeout delay.
816 *
817 * This macro generates a timeout delay that that instructs a kernel API
818 * not to wait if the requested operation cannot be performed immediately.
819 *
820 * @return Timeout delay value.
821 */
822#define K_NO_WAIT 0
823
824/**
825 * @brief Generate timeout delay from milliseconds.
826 *
827 * This macro generates a timeout delay that that instructs a kernel API
828 * to wait up to @a ms milliseconds to perform the requested operation.
829 *
830 * @param ms Duration in milliseconds.
831 *
832 * @return Timeout delay value.
833 */
Johan Hedberg14471692016-11-13 10:52:15 +0200834#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500835
836/**
837 * @brief Generate timeout delay from seconds.
838 *
839 * This macro generates a timeout delay that that instructs a kernel API
840 * to wait up to @a s seconds to perform the requested operation.
841 *
842 * @param s Duration in seconds.
843 *
844 * @return Timeout delay value.
845 */
Johan Hedberg14471692016-11-13 10:52:15 +0200846#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500847
848/**
849 * @brief Generate timeout delay from minutes.
850 *
851 * This macro generates a timeout delay that that instructs a kernel API
852 * to wait up to @a m minutes to perform the requested operation.
853 *
854 * @param m Duration in minutes.
855 *
856 * @return Timeout delay value.
857 */
Johan Hedberg14471692016-11-13 10:52:15 +0200858#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500859
860/**
861 * @brief Generate timeout delay from hours.
862 *
863 * This macro generates a timeout delay that that instructs a kernel API
864 * to wait up to @a h hours to perform the requested operation.
865 *
866 * @param h Duration in hours.
867 *
868 * @return Timeout delay value.
869 */
Johan Hedberg14471692016-11-13 10:52:15 +0200870#define K_HOURS(h) K_MINUTES((h) * 60)
871
Allan Stephensc98da842016-11-11 15:45:03 -0500872/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500873 * @brief Generate infinite timeout delay.
874 *
875 * This macro generates a timeout delay that that instructs a kernel API
876 * to wait as long as necessary to perform the requested operation.
877 *
878 * @return Timeout delay value.
879 */
880#define K_FOREVER (-1)
881
882/**
883 * @} end addtogroup clock_apis
884 */
885
886/**
Allan Stephensc98da842016-11-11 15:45:03 -0500887 * @cond INTERNAL_HIDDEN
888 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400889
Benjamin Walsh62092182016-12-20 14:39:08 -0500890/* kernel clocks */
891
892#if (sys_clock_ticks_per_sec == 1000) || \
893 (sys_clock_ticks_per_sec == 500) || \
894 (sys_clock_ticks_per_sec == 250) || \
895 (sys_clock_ticks_per_sec == 125) || \
896 (sys_clock_ticks_per_sec == 100) || \
897 (sys_clock_ticks_per_sec == 50) || \
898 (sys_clock_ticks_per_sec == 25) || \
899 (sys_clock_ticks_per_sec == 20) || \
900 (sys_clock_ticks_per_sec == 10) || \
901 (sys_clock_ticks_per_sec == 1)
902
903 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
904#else
905 /* yields horrible 64-bit math on many architectures: try to avoid */
906 #define _NON_OPTIMIZED_TICKS_PER_SEC
907#endif
908
909#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500910extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500911#else
Kumar Galacc334c72017-04-21 10:55:34 -0500912static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500913{
Kumar Galacc334c72017-04-21 10:55:34 -0500914 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500915}
916#endif
917
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500918/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800919#ifdef CONFIG_TICKLESS_KERNEL
920#define _TICK_ALIGN 0
921#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500922#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800923#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500924
Kumar Galacc334c72017-04-21 10:55:34 -0500925static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400926{
Benjamin Walsh62092182016-12-20 14:39:08 -0500927#ifdef CONFIG_SYS_CLOCK_EXISTS
928
929#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500930 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400931#else
Kumar Galacc334c72017-04-21 10:55:34 -0500932 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500933#endif
934
935#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400936 __ASSERT(ticks == 0, "");
937 return 0;
938#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400939}
940
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941struct k_timer {
942 /*
943 * _timeout structure must be first here if we want to use
944 * dynamic timer allocation. timeout.node is used in the double-linked
945 * list of free timers
946 */
947 struct _timeout timeout;
948
Allan Stephens45bfa372016-10-12 12:39:42 -0500949 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950 _wait_q_t wait_q;
951
952 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500953 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400954
955 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500956 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400957
958 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500959 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400960
Allan Stephens45bfa372016-10-12 12:39:42 -0500961 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500962 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400963
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500964 /* user-specific data, also used to support legacy features */
965 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966
Anas Nashif2f203c22016-12-18 06:57:45 -0500967 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400968};
969
Allan Stephens1342adb2016-11-03 13:54:53 -0500970#define K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971 { \
Benjamin Walshd211a522016-12-06 11:44:01 -0500972 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -0500973 .timeout.wait_q = NULL, \
974 .timeout.thread = NULL, \
975 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400976 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -0500977 .expiry_fn = expiry, \
978 .stop_fn = stop, \
979 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500980 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -0500981 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400982 }
983
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984/**
Allan Stephensc98da842016-11-11 15:45:03 -0500985 * INTERNAL_HIDDEN @endcond
986 */
987
988/**
989 * @defgroup timer_apis Timer APIs
990 * @ingroup kernel_apis
991 * @{
992 */
993
994/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500995 * @typedef k_timer_expiry_t
996 * @brief Timer expiry function type.
997 *
998 * A timer's expiry function is executed by the system clock interrupt handler
999 * each time the timer expires. The expiry function is optional, and is only
1000 * invoked if the timer has been initialized with one.
1001 *
1002 * @param timer Address of timer.
1003 *
1004 * @return N/A
1005 */
1006typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1007
1008/**
1009 * @typedef k_timer_stop_t
1010 * @brief Timer stop function type.
1011 *
1012 * A timer's stop function is executed if the timer is stopped prematurely.
1013 * The function runs in the context of the thread that stops the timer.
1014 * The stop function is optional, and is only invoked if the timer has been
1015 * initialized with one.
1016 *
1017 * @param timer Address of timer.
1018 *
1019 * @return N/A
1020 */
1021typedef void (*k_timer_stop_t)(struct k_timer *timer);
1022
1023/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001028 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 *
1030 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001031 * @param expiry_fn Function to invoke each time the timer expires.
1032 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001034#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001035 struct k_timer name \
1036 __in_section(_k_timer, static, name) = \
Allan Stephens1342adb2016-11-03 13:54:53 -05001037 K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001038
Allan Stephens45bfa372016-10-12 12:39:42 -05001039/**
1040 * @brief Initialize a timer.
1041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001043 *
1044 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * @param expiry_fn Function to invoke each time the timer expires.
1046 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001047 *
1048 * @return N/A
1049 */
1050extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001051 k_timer_expiry_t expiry_fn,
1052 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001053
Allan Stephens45bfa372016-10-12 12:39:42 -05001054/**
1055 * @brief Start a timer.
1056 *
1057 * This routine starts a timer, and resets its status to zero. The timer
1058 * begins counting down using the specified duration and period values.
1059 *
1060 * Attempting to start a timer that is already running is permitted.
1061 * The timer's status is reset to zero and the timer begins counting down
1062 * using the new duration and period values.
1063 *
1064 * @param timer Address of timer.
1065 * @param duration Initial timer duration (in milliseconds).
1066 * @param period Timer period (in milliseconds).
1067 *
1068 * @return N/A
1069 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001071 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001072
1073/**
1074 * @brief Stop a timer.
1075 *
1076 * This routine stops a running timer prematurely. The timer's stop function,
1077 * if one exists, is invoked by the caller.
1078 *
1079 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001080 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001081 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001082 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1083 * if @a k_timer_stop is to be called from ISRs.
1084 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001085 * @param timer Address of timer.
1086 *
1087 * @return N/A
1088 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001089extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001090
1091/**
1092 * @brief Read timer status.
1093 *
1094 * This routine reads the timer's status, which indicates the number of times
1095 * it has expired since its status was last read.
1096 *
1097 * Calling this routine resets the timer's status to zero.
1098 *
1099 * @param timer Address of timer.
1100 *
1101 * @return Timer status.
1102 */
Kumar Galacc334c72017-04-21 10:55:34 -05001103extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001104
1105/**
1106 * @brief Synchronize thread to timer expiration.
1107 *
1108 * This routine blocks the calling thread until the timer's status is non-zero
1109 * (indicating that it has expired at least once since it was last examined)
1110 * or the timer is stopped. If the timer status is already non-zero,
1111 * or the timer is already stopped, the caller continues without waiting.
1112 *
1113 * Calling this routine resets the timer's status to zero.
1114 *
1115 * This routine must not be used by interrupt handlers, since they are not
1116 * allowed to block.
1117 *
1118 * @param timer Address of timer.
1119 *
1120 * @return Timer status.
1121 */
Kumar Galacc334c72017-04-21 10:55:34 -05001122extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001123
1124/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001126 *
1127 * This routine computes the (approximate) time remaining before a running
1128 * timer next expires. If the timer is not running, it returns zero.
1129 *
1130 * @param timer Address of timer.
1131 *
1132 * @return Remaining time (in milliseconds).
1133 */
Kumar Galacc334c72017-04-21 10:55:34 -05001134static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001135{
1136 return _timeout_remaining_get(&timer->timeout);
1137}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001138
Allan Stephensc98da842016-11-11 15:45:03 -05001139/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001140 * @brief Associate user-specific data with a timer.
1141 *
1142 * This routine records the @a user_data with the @a timer, to be retrieved
1143 * later.
1144 *
1145 * It can be used e.g. in a timer handler shared across multiple subsystems to
1146 * retrieve data specific to the subsystem this timer is associated with.
1147 *
1148 * @param timer Address of timer.
1149 * @param user_data User data to associate with the timer.
1150 *
1151 * @return N/A
1152 */
1153static inline void k_timer_user_data_set(struct k_timer *timer,
1154 void *user_data)
1155{
1156 timer->user_data = user_data;
1157}
1158
1159/**
1160 * @brief Retrieve the user-specific data from a timer.
1161 *
1162 * @param timer Address of timer.
1163 *
1164 * @return The user data.
1165 */
1166static inline void *k_timer_user_data_get(struct k_timer *timer)
1167{
1168 return timer->user_data;
1169}
1170
1171/**
Allan Stephensc98da842016-11-11 15:45:03 -05001172 * @} end defgroup timer_apis
1173 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001174
Allan Stephensc98da842016-11-11 15:45:03 -05001175/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001176 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001177 * @{
1178 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001179
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * This routine returns the elapsed time since the system booted,
1184 * in milliseconds.
1185 *
1186 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001187 */
Kumar Galacc334c72017-04-21 10:55:34 -05001188extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001189
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001190#ifdef CONFIG_TICKLESS_KERNEL
1191/**
1192 * @brief Enable clock always on in tickless kernel
1193 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001194 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001195 * there are no timer events programmed in tickless kernel
1196 * scheduling. This is necessary if the clock is used to track
1197 * passage of time.
1198 *
1199 * @retval prev_status Previous status of always on flag
1200 */
1201static inline int k_enable_sys_clock_always_on(void)
1202{
1203 int prev_status = _sys_clock_always_on;
1204
1205 _sys_clock_always_on = 1;
1206 _enable_sys_clock();
1207
1208 return prev_status;
1209}
1210
1211/**
1212 * @brief Disable clock always on in tickless kernel
1213 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001214 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001215 * there are no timer events programmed in tickless kernel
1216 * scheduling. To save power, this routine should be called
1217 * immediately when clock is not used to track time.
1218 */
1219static inline void k_disable_sys_clock_always_on(void)
1220{
1221 _sys_clock_always_on = 0;
1222}
1223#else
1224#define k_enable_sys_clock_always_on() do { } while ((0))
1225#define k_disable_sys_clock_always_on() do { } while ((0))
1226#endif
1227
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * This routine returns the lower 32-bits of the elapsed time since the system
1232 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * This routine can be more efficient than k_uptime_get(), as it reduces the
1235 * need for interrupt locking and 64-bit math. However, the 32-bit result
1236 * cannot hold a system uptime time larger than approximately 50 days, so the
1237 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001240 */
Kumar Galacc334c72017-04-21 10:55:34 -05001241extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001242
1243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001246 * This routine computes the elapsed time between the current system uptime
1247 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001249 * @param reftime Pointer to a reference time, which is updated to the current
1250 * uptime upon return.
1251 *
1252 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001253 */
Kumar Galacc334c72017-04-21 10:55:34 -05001254extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001255
1256/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001257 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001259 * This routine computes the elapsed time between the current system uptime
1260 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1263 * need for interrupt locking and 64-bit math. However, the 32-bit result
1264 * cannot hold an elapsed time larger than approximately 50 days, so the
1265 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * @param reftime Pointer to a reference time, which is updated to the current
1268 * uptime upon return.
1269 *
1270 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001271 */
Kumar Galacc334c72017-04-21 10:55:34 -05001272extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001273
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * This routine returns the current time, as measured by the system's hardware
1278 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001280 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001281 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001282#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001283
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001285 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286 */
1287
Allan Stephensc98da842016-11-11 15:45:03 -05001288/**
1289 * @cond INTERNAL_HIDDEN
1290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001292struct k_queue {
1293 _wait_q_t wait_q;
1294 sys_slist_t data_q;
1295 _POLL_EVENT;
1296
1297 _OBJECT_TRACING_NEXT_PTR(k_queue);
1298};
1299
1300#define K_QUEUE_INITIALIZER(obj) \
1301 { \
1302 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1303 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
1304 _POLL_EVENT_OBJ_INIT \
1305 _OBJECT_TRACING_INIT \
1306 }
1307
1308/**
1309 * INTERNAL_HIDDEN @endcond
1310 */
1311
1312/**
1313 * @defgroup queue_apis Queue APIs
1314 * @ingroup kernel_apis
1315 * @{
1316 */
1317
1318/**
1319 * @brief Initialize a queue.
1320 *
1321 * This routine initializes a queue object, prior to its first use.
1322 *
1323 * @param queue Address of the queue.
1324 *
1325 * @return N/A
1326 */
1327extern void k_queue_init(struct k_queue *queue);
1328
1329/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001330 * @brief Cancel waiting on a queue.
1331 *
1332 * This routine causes first thread pending on @a queue, if any, to
1333 * return from k_queue_get() call with NULL value (as if timeout expired).
1334 *
1335 * @note Can be called by ISRs.
1336 *
1337 * @param queue Address of the queue.
1338 *
1339 * @return N/A
1340 */
1341extern void k_queue_cancel_wait(struct k_queue *queue);
1342
1343/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001344 * @brief Append an element to the end of a queue.
1345 *
1346 * This routine appends a data item to @a queue. A queue data item must be
1347 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1348 * reserved for the kernel's use.
1349 *
1350 * @note Can be called by ISRs.
1351 *
1352 * @param queue Address of the queue.
1353 * @param data Address of the data item.
1354 *
1355 * @return N/A
1356 */
1357extern void k_queue_append(struct k_queue *queue, void *data);
1358
1359/**
1360 * @brief Prepend an element to a queue.
1361 *
1362 * This routine prepends a data item to @a queue. A queue data item must be
1363 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1364 * reserved for the kernel's use.
1365 *
1366 * @note Can be called by ISRs.
1367 *
1368 * @param queue Address of the queue.
1369 * @param data Address of the data item.
1370 *
1371 * @return N/A
1372 */
1373extern void k_queue_prepend(struct k_queue *queue, void *data);
1374
1375/**
1376 * @brief Inserts an element to a queue.
1377 *
1378 * This routine inserts a data item to @a queue after previous item. A queue
1379 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1380 * item are reserved for the kernel's use.
1381 *
1382 * @note Can be called by ISRs.
1383 *
1384 * @param queue Address of the queue.
1385 * @param prev Address of the previous data item.
1386 * @param data Address of the data item.
1387 *
1388 * @return N/A
1389 */
1390extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1391
1392/**
1393 * @brief Atomically append a list of elements to a queue.
1394 *
1395 * This routine adds a list of data items to @a queue in one operation.
1396 * The data items must be in a singly-linked list, with the first 32 bits
1397 * in each data item pointing to the next data item; the list must be
1398 * NULL-terminated.
1399 *
1400 * @note Can be called by ISRs.
1401 *
1402 * @param queue Address of the queue.
1403 * @param head Pointer to first node in singly-linked list.
1404 * @param tail Pointer to last node in singly-linked list.
1405 *
1406 * @return N/A
1407 */
1408extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1409
1410/**
1411 * @brief Atomically add a list of elements to a queue.
1412 *
1413 * This routine adds a list of data items to @a queue in one operation.
1414 * The data items must be in a singly-linked list implemented using a
1415 * sys_slist_t object. Upon completion, the original list is empty.
1416 *
1417 * @note Can be called by ISRs.
1418 *
1419 * @param queue Address of the queue.
1420 * @param list Pointer to sys_slist_t object.
1421 *
1422 * @return N/A
1423 */
1424extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1425
1426/**
1427 * @brief Get an element from a queue.
1428 *
1429 * This routine removes first data item from @a queue. The first 32 bits of the
1430 * data item are reserved for the kernel's use.
1431 *
1432 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1433 *
1434 * @param queue Address of the queue.
1435 * @param timeout Waiting period to obtain a data item (in milliseconds),
1436 * or one of the special values K_NO_WAIT and K_FOREVER.
1437 *
1438 * @return Address of the data item if successful; NULL if returned
1439 * without waiting, or waiting period timed out.
1440 */
Kumar Galacc334c72017-04-21 10:55:34 -05001441extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001442
1443/**
1444 * @brief Query a queue to see if it has data available.
1445 *
1446 * Note that the data might be already gone by the time this function returns
1447 * if other threads are also trying to read from the queue.
1448 *
1449 * @note Can be called by ISRs.
1450 *
1451 * @param queue Address of the queue.
1452 *
1453 * @return Non-zero if the queue is empty.
1454 * @return 0 if data is available.
1455 */
1456static inline int k_queue_is_empty(struct k_queue *queue)
1457{
1458 return (int)sys_slist_is_empty(&queue->data_q);
1459}
1460
1461/**
1462 * @brief Statically define and initialize a queue.
1463 *
1464 * The queue can be accessed outside the module where it is defined using:
1465 *
1466 * @code extern struct k_queue <name>; @endcode
1467 *
1468 * @param name Name of the queue.
1469 */
1470#define K_QUEUE_DEFINE(name) \
1471 struct k_queue name \
1472 __in_section(_k_queue, static, name) = \
1473 K_QUEUE_INITIALIZER(name)
1474
1475/**
1476 * @} end defgroup queue_apis
1477 */
1478
1479/**
1480 * @cond INTERNAL_HIDDEN
1481 */
1482
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001483struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001484 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001485};
1486
Allan Stephensc98da842016-11-11 15:45:03 -05001487#define K_FIFO_INITIALIZER(obj) \
1488 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001489 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001490 }
1491
1492/**
1493 * INTERNAL_HIDDEN @endcond
1494 */
1495
1496/**
1497 * @defgroup fifo_apis Fifo APIs
1498 * @ingroup kernel_apis
1499 * @{
1500 */
1501
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001503 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001507 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 *
1509 * @return N/A
1510 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001511#define k_fifo_init(fifo) \
1512 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513
1514/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001515 * @brief Cancel waiting on a fifo.
1516 *
1517 * This routine causes first thread pending on @a fifo, if any, to
1518 * return from k_fifo_get() call with NULL value (as if timeout
1519 * expired).
1520 *
1521 * @note Can be called by ISRs.
1522 *
1523 * @param fifo Address of the fifo.
1524 *
1525 * @return N/A
1526 */
1527#define k_fifo_cancel_wait(fifo) \
1528 k_queue_cancel_wait((struct k_queue *) fifo)
1529
1530/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001531 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001533 * This routine adds a data item to @a fifo. A fifo data item must be
1534 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1535 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001537 * @note Can be called by ISRs.
1538 *
1539 * @param fifo Address of the fifo.
1540 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001541 *
1542 * @return N/A
1543 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001544#define k_fifo_put(fifo, data) \
1545 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001546
1547/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001548 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001550 * This routine adds a list of data items to @a fifo in one operation.
1551 * The data items must be in a singly-linked list, with the first 32 bits
1552 * each data item pointing to the next data item; the list must be
1553 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001555 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * @param fifo Address of the fifo.
1558 * @param head Pointer to first node in singly-linked list.
1559 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001560 *
1561 * @return N/A
1562 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001563#define k_fifo_put_list(fifo, head, tail) \
1564 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001565
1566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001569 * This routine adds a list of data items to @a fifo in one operation.
1570 * The data items must be in a singly-linked list implemented using a
1571 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001572 * and must be re-initialized via sys_slist_init().
1573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001574 * @note Can be called by ISRs.
1575 *
1576 * @param fifo Address of the fifo.
1577 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001578 *
1579 * @return N/A
1580 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001581#define k_fifo_put_slist(fifo, list) \
1582 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001583
1584/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001585 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001587 * This routine removes a data item from @a fifo in a "first in, first out"
1588 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1591 *
1592 * @param fifo Address of the fifo.
1593 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001594 * or one of the special values K_NO_WAIT and K_FOREVER.
1595 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001596 * @return Address of the data item if successful; NULL if returned
1597 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001598 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001599#define k_fifo_get(fifo, timeout) \
1600 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001601
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001602/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001603 * @brief Query a fifo to see if it has data available.
1604 *
1605 * Note that the data might be already gone by the time this function returns
1606 * if other threads is also trying to read from the fifo.
1607 *
1608 * @note Can be called by ISRs.
1609 *
1610 * @param fifo Address of the fifo.
1611 *
1612 * @return Non-zero if the fifo is empty.
1613 * @return 0 if data is available.
1614 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001615#define k_fifo_is_empty(fifo) \
1616 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001617
1618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001621 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001622 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001623 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001626 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001627#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001628 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001629 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001630 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001631
Allan Stephensc98da842016-11-11 15:45:03 -05001632/**
1633 * @} end defgroup fifo_apis
1634 */
1635
1636/**
1637 * @cond INTERNAL_HIDDEN
1638 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001639
1640struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001641 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001642};
1643
Allan Stephensc98da842016-11-11 15:45:03 -05001644#define K_LIFO_INITIALIZER(obj) \
1645 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001646 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001647 }
1648
1649/**
1650 * INTERNAL_HIDDEN @endcond
1651 */
1652
1653/**
1654 * @defgroup lifo_apis Lifo APIs
1655 * @ingroup kernel_apis
1656 * @{
1657 */
1658
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001659/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001660 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001662 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001664 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001665 *
1666 * @return N/A
1667 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001668#define k_lifo_init(lifo) \
1669 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001670
1671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001674 * This routine adds a data item to @a lifo. A lifo data item must be
1675 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1676 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * @note Can be called by ISRs.
1679 *
1680 * @param lifo Address of the lifo.
1681 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001682 *
1683 * @return N/A
1684 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001685#define k_lifo_put(lifo, data) \
1686 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001687
1688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * This routine removes a data item from @a lifo in a "last in, first out"
1692 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1695 *
1696 * @param lifo Address of the lifo.
1697 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001698 * or one of the special values K_NO_WAIT and K_FOREVER.
1699 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001700 * @return Address of the data item if successful; NULL if returned
1701 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001702 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001703#define k_lifo_get(lifo, timeout) \
1704 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001705
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001707 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001710 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001711 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001714 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001715#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001716 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001717 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001718 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001719
Allan Stephensc98da842016-11-11 15:45:03 -05001720/**
1721 * @} end defgroup lifo_apis
1722 */
1723
1724/**
1725 * @cond INTERNAL_HIDDEN
1726 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001727
1728struct k_stack {
1729 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001730 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001731
Anas Nashif2f203c22016-12-18 06:57:45 -05001732 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001733};
1734
Allan Stephensc98da842016-11-11 15:45:03 -05001735#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1736 { \
1737 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1738 .base = stack_buffer, \
1739 .next = stack_buffer, \
1740 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001741 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001742 }
1743
1744/**
1745 * INTERNAL_HIDDEN @endcond
1746 */
1747
1748/**
1749 * @defgroup stack_apis Stack APIs
1750 * @ingroup kernel_apis
1751 * @{
1752 */
1753
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754/**
1755 * @brief Initialize a stack.
1756 *
1757 * This routine initializes a stack object, prior to its first use.
1758 *
1759 * @param stack Address of the stack.
1760 * @param buffer Address of array used to hold stacked values.
1761 * @param num_entries Maximum number of values that can be stacked.
1762 *
1763 * @return N/A
1764 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001765extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001766 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767
1768/**
1769 * @brief Push an element onto a stack.
1770 *
1771 * This routine adds a 32-bit value @a data to @a stack.
1772 *
1773 * @note Can be called by ISRs.
1774 *
1775 * @param stack Address of the stack.
1776 * @param data Value to push onto the stack.
1777 *
1778 * @return N/A
1779 */
Kumar Galacc334c72017-04-21 10:55:34 -05001780extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001781
1782/**
1783 * @brief Pop an element from a stack.
1784 *
1785 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1786 * manner and stores the value in @a data.
1787 *
1788 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1789 *
1790 * @param stack Address of the stack.
1791 * @param data Address of area to hold the value popped from the stack.
1792 * @param timeout Waiting period to obtain a value (in milliseconds),
1793 * or one of the special values K_NO_WAIT and K_FOREVER.
1794 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001795 * @retval 0 Element popped from stack.
1796 * @retval -EBUSY Returned without waiting.
1797 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 */
Kumar Galacc334c72017-04-21 10:55:34 -05001799extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001800
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001806 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001808 * @param name Name of the stack.
1809 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001810 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001811#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001812 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001813 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001814 struct k_stack name \
1815 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001816 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1817 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819/**
Allan Stephensc98da842016-11-11 15:45:03 -05001820 * @} end defgroup stack_apis
1821 */
1822
Allan Stephens6bba9b02016-11-16 14:56:54 -05001823struct k_work;
1824
Allan Stephensc98da842016-11-11 15:45:03 -05001825/**
1826 * @defgroup workqueue_apis Workqueue Thread APIs
1827 * @ingroup kernel_apis
1828 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001829 */
1830
Allan Stephens6bba9b02016-11-16 14:56:54 -05001831/**
1832 * @typedef k_work_handler_t
1833 * @brief Work item handler function type.
1834 *
1835 * A work item's handler function is executed by a workqueue's thread
1836 * when the work item is processed by the workqueue.
1837 *
1838 * @param work Address of the work item.
1839 *
1840 * @return N/A
1841 */
1842typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001843
1844/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001845 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001846 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001847
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001848struct k_work_q {
1849 struct k_fifo fifo;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001850 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001851};
1852
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001853enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001854 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855};
1856
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001857struct k_work {
1858 void *_reserved; /* Used by k_fifo implementation. */
1859 k_work_handler_t handler;
1860 atomic_t flags[1];
1861};
1862
Allan Stephens6bba9b02016-11-16 14:56:54 -05001863struct k_delayed_work {
1864 struct k_work work;
1865 struct _timeout timeout;
1866 struct k_work_q *work_q;
1867};
1868
1869extern struct k_work_q k_sys_work_q;
1870
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001871/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001872 * INTERNAL_HIDDEN @endcond
1873 */
1874
1875/**
1876 * @brief Initialize a statically-defined work item.
1877 *
1878 * This macro can be used to initialize a statically-defined workqueue work
1879 * item, prior to its first use. For example,
1880 *
1881 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1882 *
1883 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884 */
1885#define K_WORK_INITIALIZER(work_handler) \
1886 { \
1887 ._reserved = NULL, \
1888 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001889 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001890 }
1891
1892/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001893 * @brief Initialize a work item.
1894 *
1895 * This routine initializes a workqueue work item, prior to its first use.
1896 *
1897 * @param work Address of work item.
1898 * @param handler Function to invoke each time work item is processed.
1899 *
1900 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901 */
1902static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1903{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001904 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905 work->handler = handler;
1906}
1907
1908/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001909 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001910 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001911 * This routine submits work item @a work to be processed by workqueue
1912 * @a work_q. If the work item is already pending in the workqueue's queue
1913 * as a result of an earlier submission, this routine has no effect on the
1914 * work item. If the work item has already been processed, or is currently
1915 * being processed, its work is considered complete and the work item can be
1916 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001917 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001918 * @warning
1919 * A submitted work item must not be modified until it has been processed
1920 * by the workqueue.
1921 *
1922 * @note Can be called by ISRs.
1923 *
1924 * @param work_q Address of workqueue.
1925 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001926 *
1927 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001928 */
1929static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1930 struct k_work *work)
1931{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001932 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001933 k_fifo_put(&work_q->fifo, work);
1934 }
1935}
1936
1937/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001938 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001939 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001940 * This routine indicates if work item @a work is pending in a workqueue's
1941 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001942 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001943 * @note Can be called by ISRs.
1944 *
1945 * @param work Address of work item.
1946 *
1947 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001948 */
1949static inline int k_work_pending(struct k_work *work)
1950{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001951 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001952}
1953
1954/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001955 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001957 * This routine starts workqueue @a work_q. The workqueue spawns its work
1958 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001959 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001960 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07001961 * @param stack Pointer to work queue thread's stack space, as defined by
1962 * K_THREAD_STACK_DEFINE()
1963 * @param stack_size Size of the work queue thread's stack (in bytes), which
1964 * should either be the same constant passed to
1965 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05001966 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 *
1968 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001969 */
Allan Stephens904cf972016-10-07 13:59:23 -05001970extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05001971 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001972
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001974 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001976 * This routine initializes a workqueue delayed work item, prior to
1977 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001979 * @param work Address of delayed work item.
1980 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981 *
1982 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001983 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04001984extern void k_delayed_work_init(struct k_delayed_work *work,
1985 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001986
1987/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001988 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001989 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001990 * This routine schedules work item @a work to be processed by workqueue
1991 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07001992 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05001993 * Only when the countdown completes is the work item actually submitted to
1994 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001995 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001996 * Submitting a previously submitted delayed work item that is still
1997 * counting down cancels the existing submission and restarts the countdown
1998 * using the new delay. If the work item is currently pending on the
1999 * workqueue's queue because the countdown has completed it is too late to
2000 * resubmit the item, and resubmission fails without impacting the work item.
2001 * If the work item has already been processed, or is currently being processed,
2002 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002004 * @warning
2005 * A delayed work item must not be modified until it has been processed
2006 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002008 * @note Can be called by ISRs.
2009 *
2010 * @param work_q Address of workqueue.
2011 * @param work Address of delayed work item.
2012 * @param delay Delay before submitting the work item (in milliseconds).
2013 *
2014 * @retval 0 Work item countdown started.
2015 * @retval -EINPROGRESS Work item is already pending.
2016 * @retval -EINVAL Work item is being processed or has completed its work.
2017 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002019extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2020 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002021 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002022
2023/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002024 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002025 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002026 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002027 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002028 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002029 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002030 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002032 * @param work Address of delayed work item.
2033 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002034 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002035 * @retval -EINPROGRESS Work item is already pending.
2036 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002037 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002038extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002039
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041 * @brief Submit a work item to the system workqueue.
2042 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002043 * This routine submits work item @a work to be processed by the system
2044 * workqueue. If the work item is already pending in the workqueue's queue
2045 * as a result of an earlier submission, this routine has no effect on the
2046 * work item. If the work item has already been processed, or is currently
2047 * being processed, its work is considered complete and the work item can be
2048 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002049 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002050 * @warning
2051 * Work items submitted to the system workqueue should avoid using handlers
2052 * that block or yield since this may prevent the system workqueue from
2053 * processing other work items in a timely manner.
2054 *
2055 * @note Can be called by ISRs.
2056 *
2057 * @param work Address of work item.
2058 *
2059 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002060 */
2061static inline void k_work_submit(struct k_work *work)
2062{
2063 k_work_submit_to_queue(&k_sys_work_q, work);
2064}
2065
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002066/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002067 * @brief Submit a delayed work item to the system workqueue.
2068 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002069 * This routine schedules work item @a work to be processed by the system
2070 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002071 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002072 * Only when the countdown completes is the work item actually submitted to
2073 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002074 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002075 * Submitting a previously submitted delayed work item that is still
2076 * counting down cancels the existing submission and restarts the countdown
2077 * using the new delay. If the work item is currently pending on the
2078 * workqueue's queue because the countdown has completed it is too late to
2079 * resubmit the item, and resubmission fails without impacting the work item.
2080 * If the work item has already been processed, or is currently being processed,
2081 * its work is considered complete and the work item can be resubmitted.
2082 *
2083 * @warning
2084 * Work items submitted to the system workqueue should avoid using handlers
2085 * that block or yield since this may prevent the system workqueue from
2086 * processing other work items in a timely manner.
2087 *
2088 * @note Can be called by ISRs.
2089 *
2090 * @param work Address of delayed work item.
2091 * @param delay Delay before submitting the work item (in milliseconds).
2092 *
2093 * @retval 0 Work item countdown started.
2094 * @retval -EINPROGRESS Work item is already pending.
2095 * @retval -EINVAL Work item is being processed or has completed its work.
2096 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002097 */
2098static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002099 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002100{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002101 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002102}
2103
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002104/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002105 * @brief Get time remaining before a delayed work gets scheduled.
2106 *
2107 * This routine computes the (approximate) time remaining before a
2108 * delayed work gets executed. If the delayed work is not waiting to be
2109 * schedules, it returns zero.
2110 *
2111 * @param work Delayed work item.
2112 *
2113 * @return Remaining time (in milliseconds).
2114 */
Kumar Galacc334c72017-04-21 10:55:34 -05002115static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002116{
2117 return _timeout_remaining_get(&work->timeout);
2118}
2119
2120/**
Allan Stephensc98da842016-11-11 15:45:03 -05002121 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122 */
2123
Allan Stephensc98da842016-11-11 15:45:03 -05002124/**
2125 * @cond INTERNAL_HIDDEN
2126 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127
2128struct k_mutex {
2129 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002130 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002131 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002133
Anas Nashif2f203c22016-12-18 06:57:45 -05002134 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002135};
2136
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002137#define K_MUTEX_INITIALIZER(obj) \
2138 { \
2139 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2140 .owner = NULL, \
2141 .lock_count = 0, \
2142 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002143 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144 }
2145
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146/**
Allan Stephensc98da842016-11-11 15:45:03 -05002147 * INTERNAL_HIDDEN @endcond
2148 */
2149
2150/**
2151 * @defgroup mutex_apis Mutex APIs
2152 * @ingroup kernel_apis
2153 * @{
2154 */
2155
2156/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002157 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002159 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002161 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002165#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002166 struct k_mutex name \
2167 __in_section(_k_mutex, static, name) = \
2168 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002169
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002171 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002173 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002175 * Upon completion, the mutex is available and does not have an owner.
2176 *
2177 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 *
2179 * @return N/A
2180 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182
2183/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * This routine locks @a mutex. If the mutex is locked by another thread,
2187 * the calling thread waits until the mutex becomes available or until
2188 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002190 * A thread is permitted to lock a mutex it has already locked. The operation
2191 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002193 * @param mutex Address of the mutex.
2194 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195 * or one of the special values K_NO_WAIT and K_FOREVER.
2196 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002197 * @retval 0 Mutex locked.
2198 * @retval -EBUSY Returned without waiting.
2199 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002200 */
Kumar Galacc334c72017-04-21 10:55:34 -05002201extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002202
2203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002204 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002206 * This routine unlocks @a mutex. The mutex must already be locked by the
2207 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002208 *
2209 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002210 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211 * thread.
2212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002213 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214 *
2215 * @return N/A
2216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217extern void k_mutex_unlock(struct k_mutex *mutex);
2218
Allan Stephensc98da842016-11-11 15:45:03 -05002219/**
2220 * @} end defgroup mutex_apis
2221 */
2222
2223/**
2224 * @cond INTERNAL_HIDDEN
2225 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226
2227struct k_sem {
2228 _wait_q_t wait_q;
2229 unsigned int count;
2230 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002231 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232
Anas Nashif2f203c22016-12-18 06:57:45 -05002233 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234};
2235
Allan Stephensc98da842016-11-11 15:45:03 -05002236#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2237 { \
2238 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2239 .count = initial_count, \
2240 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002241 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002242 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002243 }
2244
2245/**
2246 * INTERNAL_HIDDEN @endcond
2247 */
2248
2249/**
2250 * @defgroup semaphore_apis Semaphore APIs
2251 * @ingroup kernel_apis
2252 * @{
2253 */
2254
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002255/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002256 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002258 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002260 * @param sem Address of the semaphore.
2261 * @param initial_count Initial semaphore count.
2262 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002263 *
2264 * @return N/A
2265 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002266extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2267 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002268
2269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002272 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002274 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2275 *
2276 * @param sem Address of the semaphore.
2277 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002278 * or one of the special values K_NO_WAIT and K_FOREVER.
2279 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002280 * @note When porting code from the nanokernel legacy API to the new API, be
2281 * careful with the return value of this function. The return value is the
2282 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2283 * non-zero means failure, while the nano_sem_take family returns 1 for success
2284 * and 0 for failure.
2285 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002286 * @retval 0 Semaphore taken.
2287 * @retval -EBUSY Returned without waiting.
2288 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002289 */
Kumar Galacc334c72017-04-21 10:55:34 -05002290extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002291
2292/**
2293 * @brief Give a semaphore.
2294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002295 * This routine gives @a sem, unless the semaphore is already at its maximum
2296 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002298 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002300 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002301 *
2302 * @return N/A
2303 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304extern void k_sem_give(struct k_sem *sem);
2305
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002306/**
2307 * @brief Reset a semaphore's count to zero.
2308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002309 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002311 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002312 *
2313 * @return N/A
2314 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002315static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316{
2317 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318}
2319
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002320/**
2321 * @brief Get a semaphore's count.
2322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002328 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002329static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330{
2331 return sem->count;
2332}
2333
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002334/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002338 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002339 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002341 * @param name Name of the semaphore.
2342 * @param initial_count Initial semaphore count.
2343 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002344 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002346 struct k_sem name \
2347 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348 K_SEM_INITIALIZER(name, initial_count, count_limit)
2349
Allan Stephensc98da842016-11-11 15:45:03 -05002350/**
2351 * @} end defgroup semaphore_apis
2352 */
2353
2354/**
2355 * @defgroup alert_apis Alert APIs
2356 * @ingroup kernel_apis
2357 * @{
2358 */
2359
Allan Stephens5eceb852016-11-16 10:16:30 -05002360/**
2361 * @typedef k_alert_handler_t
2362 * @brief Alert handler function type.
2363 *
2364 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002365 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002366 * and is only invoked if the alert has been initialized with one.
2367 *
2368 * @param alert Address of the alert.
2369 *
2370 * @return 0 if alert has been consumed; non-zero if alert should pend.
2371 */
2372typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002373
2374/**
2375 * @} end defgroup alert_apis
2376 */
2377
2378/**
2379 * @cond INTERNAL_HIDDEN
2380 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002382#define K_ALERT_DEFAULT NULL
2383#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002385struct k_alert {
2386 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 atomic_t send_count;
2388 struct k_work work_item;
2389 struct k_sem sem;
2390
Anas Nashif2f203c22016-12-18 06:57:45 -05002391 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392};
2393
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002394extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002395
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002396#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002398 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002400 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002401 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002402 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002403 }
2404
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002405/**
Allan Stephensc98da842016-11-11 15:45:03 -05002406 * INTERNAL_HIDDEN @endcond
2407 */
2408
2409/**
2410 * @addtogroup alert_apis
2411 * @{
2412 */
2413
2414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002417 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002419 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * @param name Name of the alert.
2422 * @param alert_handler Action to take when alert is sent. Specify either
2423 * the address of a function to be invoked by the system workqueue
2424 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2425 * K_ALERT_DEFAULT (which causes the alert to pend).
2426 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002428#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002429 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002430 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002431 K_ALERT_INITIALIZER(name, alert_handler, \
2432 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002434/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002435 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002437 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * @param alert Address of the alert.
2440 * @param handler Action to take when alert is sent. Specify either the address
2441 * of a function to be invoked by the system workqueue thread,
2442 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2443 * K_ALERT_DEFAULT (which causes the alert to pend).
2444 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002445 *
2446 * @return N/A
2447 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002448extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2449 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450
2451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2457 *
2458 * @param alert Address of the alert.
2459 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 * or one of the special values K_NO_WAIT and K_FOREVER.
2461 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002462 * @retval 0 Alert received.
2463 * @retval -EBUSY Returned without waiting.
2464 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 */
Kumar Galacc334c72017-04-21 10:55:34 -05002466extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467
2468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * This routine signals @a alert. The action specified for @a alert will
2472 * be taken, which may trigger the execution of an alert handler function
2473 * and/or cause the alert to pend (assuming the alert has not reached its
2474 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @note Can be called by ISRs.
2477 *
2478 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
2480 * @return N/A
2481 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002482extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483
2484/**
Allan Stephensc98da842016-11-11 15:45:03 -05002485 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486 */
2487
Allan Stephensc98da842016-11-11 15:45:03 -05002488/**
2489 * @cond INTERNAL_HIDDEN
2490 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491
2492struct k_msgq {
2493 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002494 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002495 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496 char *buffer_start;
2497 char *buffer_end;
2498 char *read_ptr;
2499 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002500 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501
Anas Nashif2f203c22016-12-18 06:57:45 -05002502 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503};
2504
Peter Mitsis1da807e2016-10-06 11:36:59 -04002505#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 { \
2507 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002508 .max_msgs = q_max_msgs, \
2509 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002511 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 .read_ptr = q_buffer, \
2513 .write_ptr = q_buffer, \
2514 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002515 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516 }
2517
Peter Mitsis1da807e2016-10-06 11:36:59 -04002518/**
Allan Stephensc98da842016-11-11 15:45:03 -05002519 * INTERNAL_HIDDEN @endcond
2520 */
2521
2522/**
2523 * @defgroup msgq_apis Message Queue APIs
2524 * @ingroup kernel_apis
2525 * @{
2526 */
2527
2528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2532 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002533 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2534 * message is similarly aligned to this boundary, @a q_msg_size must also be
2535 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * The message queue can be accessed outside the module where it is defined
2538 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002540 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * @param q_name Name of the message queue.
2543 * @param q_msg_size Message size (in bytes).
2544 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002545 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002546 */
2547#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2548 static char __noinit __aligned(q_align) \
2549 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002550 struct k_msgq q_name \
2551 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002552 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2553 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554
Peter Mitsisd7a37502016-10-13 11:37:40 -04002555/**
2556 * @brief Initialize a message queue.
2557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * This routine initializes a message queue object, prior to its first use.
2559 *
Allan Stephensda827222016-11-09 14:23:58 -06002560 * The message queue's ring buffer must contain space for @a max_msgs messages,
2561 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2562 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2563 * that each message is similarly aligned to this boundary, @a q_msg_size
2564 * must also be a multiple of N.
2565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002566 * @param q Address of the message queue.
2567 * @param buffer Pointer to ring buffer that holds queued messages.
2568 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002569 * @param max_msgs Maximum number of messages that can be queued.
2570 *
2571 * @return N/A
2572 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002573extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002574 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002575
2576/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002580 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002581 * @note Can be called by ISRs.
2582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002583 * @param q Address of the message queue.
2584 * @param data Pointer to the message.
2585 * @param timeout Waiting period to add the message (in milliseconds),
2586 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002587 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002588 * @retval 0 Message sent.
2589 * @retval -ENOMSG Returned without waiting or queue purged.
2590 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002591 */
Kumar Galacc334c72017-04-21 10:55:34 -05002592extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002593
2594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002595 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 * This routine receives a message from message queue @a q in a "first in,
2598 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002599 *
Allan Stephensc98da842016-11-11 15:45:03 -05002600 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @param q Address of the message queue.
2603 * @param data Address of area to hold the received message.
2604 * @param timeout Waiting period to receive the message (in milliseconds),
2605 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002606 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002607 * @retval 0 Message received.
2608 * @retval -ENOMSG Returned without waiting.
2609 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002610 */
Kumar Galacc334c72017-04-21 10:55:34 -05002611extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002612
2613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002614 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * This routine discards all unreceived messages in a message queue's ring
2617 * buffer. Any threads that are blocked waiting to send a message to the
2618 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002620 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002621 *
2622 * @return N/A
2623 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624extern void k_msgq_purge(struct k_msgq *q);
2625
Peter Mitsis67be2492016-10-07 11:44:34 -04002626/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 * This routine returns the number of unused entries in a message queue's
2630 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 * @param q Address of the message queue.
2633 *
2634 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002635 */
Kumar Galacc334c72017-04-21 10:55:34 -05002636static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002637{
2638 return q->max_msgs - q->used_msgs;
2639}
2640
Peter Mitsisd7a37502016-10-13 11:37:40 -04002641/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @param q Address of the message queue.
2647 *
2648 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002649 */
Kumar Galacc334c72017-04-21 10:55:34 -05002650static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651{
2652 return q->used_msgs;
2653}
2654
Allan Stephensc98da842016-11-11 15:45:03 -05002655/**
2656 * @} end defgroup msgq_apis
2657 */
2658
2659/**
2660 * @defgroup mem_pool_apis Memory Pool APIs
2661 * @ingroup kernel_apis
2662 * @{
2663 */
2664
Andy Ross73cb9582017-05-09 10:42:39 -07002665/* Note on sizing: the use of a 20 bit field for block means that,
2666 * assuming a reasonable minimum block size of 16 bytes, we're limited
2667 * to 16M of memory managed by a single pool. Long term it would be
2668 * good to move to a variable bit size based on configuration.
2669 */
2670struct k_mem_block_id {
2671 u32_t pool : 8;
2672 u32_t level : 4;
2673 u32_t block : 20;
2674};
2675
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002678 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679};
2680
Allan Stephensc98da842016-11-11 15:45:03 -05002681/**
2682 * @} end defgroup mem_pool_apis
2683 */
2684
2685/**
2686 * @defgroup mailbox_apis Mailbox APIs
2687 * @ingroup kernel_apis
2688 * @{
2689 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690
2691struct k_mbox_msg {
2692 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002693 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002695 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002697 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698 /** sender's message data buffer */
2699 void *tx_data;
2700 /** internal use only - needed for legacy API support */
2701 void *_rx_data;
2702 /** message data block descriptor */
2703 struct k_mem_block tx_block;
2704 /** source thread id */
2705 k_tid_t rx_source_thread;
2706 /** target thread id */
2707 k_tid_t tx_target_thread;
2708 /** internal use only - thread waiting on send (may be a dummy) */
2709 k_tid_t _syncing_thread;
2710#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2711 /** internal use only - semaphore used during asynchronous send */
2712 struct k_sem *_async_sem;
2713#endif
2714};
2715
Allan Stephensc98da842016-11-11 15:45:03 -05002716/**
2717 * @cond INTERNAL_HIDDEN
2718 */
2719
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720struct k_mbox {
2721 _wait_q_t tx_msg_queue;
2722 _wait_q_t rx_msg_queue;
2723
Anas Nashif2f203c22016-12-18 06:57:45 -05002724 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725};
2726
2727#define K_MBOX_INITIALIZER(obj) \
2728 { \
2729 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2730 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002731 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732 }
2733
Peter Mitsis12092702016-10-14 12:57:23 -04002734/**
Allan Stephensc98da842016-11-11 15:45:03 -05002735 * INTERNAL_HIDDEN @endcond
2736 */
2737
2738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002739 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002743 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002746 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002748 struct k_mbox name \
2749 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 K_MBOX_INITIALIZER(name) \
2751
Peter Mitsis12092702016-10-14 12:57:23 -04002752/**
2753 * @brief Initialize a mailbox.
2754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * This routine initializes a mailbox object, prior to its first use.
2756 *
2757 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002758 *
2759 * @return N/A
2760 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761extern void k_mbox_init(struct k_mbox *mbox);
2762
Peter Mitsis12092702016-10-14 12:57:23 -04002763/**
2764 * @brief Send a mailbox message in a synchronous manner.
2765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002766 * This routine sends a message to @a mbox and waits for a receiver to both
2767 * receive and process it. The message data may be in a buffer, in a memory
2768 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @param mbox Address of the mailbox.
2771 * @param tx_msg Address of the transmit message descriptor.
2772 * @param timeout Waiting period for the message to be received (in
2773 * milliseconds), or one of the special values K_NO_WAIT
2774 * and K_FOREVER. Once the message has been received,
2775 * this routine waits as long as necessary for the message
2776 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002777 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002778 * @retval 0 Message sent.
2779 * @retval -ENOMSG Returned without waiting.
2780 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002781 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002782extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002783 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002784
Peter Mitsis12092702016-10-14 12:57:23 -04002785/**
2786 * @brief Send a mailbox message in an asynchronous manner.
2787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * This routine sends a message to @a mbox without waiting for a receiver
2789 * to process it. The message data may be in a buffer, in a memory pool block,
2790 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2791 * will be given when the message has been both received and completely
2792 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * @param mbox Address of the mailbox.
2795 * @param tx_msg Address of the transmit message descriptor.
2796 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002797 *
2798 * @return N/A
2799 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002800extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801 struct k_sem *sem);
2802
Peter Mitsis12092702016-10-14 12:57:23 -04002803/**
2804 * @brief Receive a mailbox message.
2805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002806 * This routine receives a message from @a mbox, then optionally retrieves
2807 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * @param mbox Address of the mailbox.
2810 * @param rx_msg Address of the receive message descriptor.
2811 * @param buffer Address of the buffer to receive data, or NULL to defer data
2812 * retrieval and message disposal until later.
2813 * @param timeout Waiting period for a message to be received (in
2814 * milliseconds), or one of the special values K_NO_WAIT
2815 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002816 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002817 * @retval 0 Message received.
2818 * @retval -ENOMSG Returned without waiting.
2819 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002820 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002821extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002822 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002823
2824/**
2825 * @brief Retrieve mailbox message data into a buffer.
2826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * This routine completes the processing of a received message by retrieving
2828 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002829 *
2830 * Alternatively, this routine can be used to dispose of a received message
2831 * without retrieving its data.
2832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * @param rx_msg Address of the receive message descriptor.
2834 * @param buffer Address of the buffer to receive data, or NULL to discard
2835 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002836 *
2837 * @return N/A
2838 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002839extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002840
2841/**
2842 * @brief Retrieve mailbox message data into a memory pool block.
2843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * This routine completes the processing of a received message by retrieving
2845 * its data into a memory pool block, then disposing of the message.
2846 * The memory pool block that results from successful retrieval must be
2847 * returned to the pool once the data has been processed, even in cases
2848 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002849 *
2850 * Alternatively, this routine can be used to dispose of a received message
2851 * without retrieving its data. In this case there is no need to return a
2852 * memory pool block to the pool.
2853 *
2854 * This routine allocates a new memory pool block for the data only if the
2855 * data is not already in one. If a new block cannot be allocated, the routine
2856 * returns a failure code and the received message is left unchanged. This
2857 * permits the caller to reattempt data retrieval at a later time or to dispose
2858 * of the received message without retrieving its data.
2859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @param rx_msg Address of a receive message descriptor.
2861 * @param pool Address of memory pool, or NULL to discard data.
2862 * @param block Address of the area to hold memory pool block info.
2863 * @param timeout Waiting period to wait for a memory pool block (in
2864 * milliseconds), or one of the special values K_NO_WAIT
2865 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002866 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002867 * @retval 0 Data retrieved.
2868 * @retval -ENOMEM Returned without waiting.
2869 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002870 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002871extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002872 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002873 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874
Allan Stephensc98da842016-11-11 15:45:03 -05002875/**
2876 * @} end defgroup mailbox_apis
2877 */
2878
2879/**
2880 * @cond INTERNAL_HIDDEN
2881 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002882
2883struct k_pipe {
2884 unsigned char *buffer; /* Pipe buffer: may be NULL */
2885 size_t size; /* Buffer size */
2886 size_t bytes_used; /* # bytes used in buffer */
2887 size_t read_index; /* Where in buffer to read from */
2888 size_t write_index; /* Where in buffer to write */
2889
2890 struct {
2891 _wait_q_t readers; /* Reader wait queue */
2892 _wait_q_t writers; /* Writer wait queue */
2893 } wait_q;
2894
Anas Nashif2f203c22016-12-18 06:57:45 -05002895 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896};
2897
Peter Mitsise5d9c582016-10-14 14:44:57 -04002898#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002899 { \
2900 .buffer = pipe_buffer, \
2901 .size = pipe_buffer_size, \
2902 .bytes_used = 0, \
2903 .read_index = 0, \
2904 .write_index = 0, \
2905 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2906 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002907 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908 }
2909
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910/**
Allan Stephensc98da842016-11-11 15:45:03 -05002911 * INTERNAL_HIDDEN @endcond
2912 */
2913
2914/**
2915 * @defgroup pipe_apis Pipe APIs
2916 * @ingroup kernel_apis
2917 * @{
2918 */
2919
2920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002924 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002925 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * @param name Name of the pipe.
2928 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2929 * or zero if no ring buffer is used.
2930 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002931 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002932#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2933 static unsigned char __noinit __aligned(pipe_align) \
2934 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002935 struct k_pipe name \
2936 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002937 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * This routine initializes a pipe object, prior to its first use.
2943 *
2944 * @param pipe Address of the pipe.
2945 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
2946 * is used.
2947 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
2948 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 *
2950 * @return N/A
2951 */
2952extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
2953 size_t size);
2954
2955/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * @param pipe Address of the pipe.
2961 * @param data Address of data to write.
2962 * @param bytes_to_write Size of data (in bytes).
2963 * @param bytes_written Address of area to hold the number of bytes written.
2964 * @param min_xfer Minimum number of bytes to write.
2965 * @param timeout Waiting period to wait for the data to be written (in
2966 * milliseconds), or one of the special values K_NO_WAIT
2967 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002968 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002969 * @retval 0 At least @a min_xfer bytes of data were written.
2970 * @retval -EIO Returned without waiting; zero data bytes were written.
2971 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002973 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002974extern int k_pipe_put(struct k_pipe *pipe, void *data,
2975 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05002976 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002977
2978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @param pipe Address of the pipe.
2984 * @param data Address to place the data read from pipe.
2985 * @param bytes_to_read Maximum number of data bytes to read.
2986 * @param bytes_read Address of area to hold the number of bytes read.
2987 * @param min_xfer Minimum number of data bytes to read.
2988 * @param timeout Waiting period to wait for the data to be read (in
2989 * milliseconds), or one of the special values K_NO_WAIT
2990 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002992 * @retval 0 At least @a min_xfer bytes of data were read.
2993 * @retval -EIO Returned without waiting; zero data bytes were read.
2994 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002997extern int k_pipe_get(struct k_pipe *pipe, void *data,
2998 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05002999 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003000
3001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * This routine writes the data contained in a memory block to @a pipe.
3005 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009 * @param block Memory block containing data to send
3010 * @param size Number of data bytes in memory block to send
3011 * @param sem Semaphore to signal upon completion (else NULL)
3012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014 */
3015extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3016 size_t size, struct k_sem *sem);
3017
3018/**
Allan Stephensc98da842016-11-11 15:45:03 -05003019 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020 */
3021
Allan Stephensc98da842016-11-11 15:45:03 -05003022/**
3023 * @cond INTERNAL_HIDDEN
3024 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003026struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003028 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003029 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003030 char *buffer;
3031 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003032 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033
Anas Nashif2f203c22016-12-18 06:57:45 -05003034 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003035};
3036
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003037#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
3038 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039 { \
3040 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003041 .num_blocks = slab_num_blocks, \
3042 .block_size = slab_block_size, \
3043 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044 .free_list = NULL, \
3045 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003046 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047 }
3048
Peter Mitsis578f9112016-10-07 13:50:31 -04003049/**
Allan Stephensc98da842016-11-11 15:45:03 -05003050 * INTERNAL_HIDDEN @endcond
3051 */
3052
3053/**
3054 * @defgroup mem_slab_apis Memory Slab APIs
3055 * @ingroup kernel_apis
3056 * @{
3057 */
3058
3059/**
Allan Stephensda827222016-11-09 14:23:58 -06003060 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003061 *
Allan Stephensda827222016-11-09 14:23:58 -06003062 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003064 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3065 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003066 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003067 *
Allan Stephensda827222016-11-09 14:23:58 -06003068 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003070 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003071 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003073 * @param name Name of the memory slab.
3074 * @param slab_block_size Size of each memory block (in bytes).
3075 * @param slab_num_blocks Number memory blocks.
3076 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003077 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003078#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3079 char __noinit __aligned(slab_align) \
3080 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3081 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003082 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003083 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
3084 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003086/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003087 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003090 *
Allan Stephensda827222016-11-09 14:23:58 -06003091 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3092 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3093 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3094 * To ensure that each memory block is similarly aligned to this boundary,
3095 * @a slab_block_size must also be a multiple of N.
3096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * @param slab Address of the memory slab.
3098 * @param buffer Pointer to buffer used for the memory blocks.
3099 * @param block_size Size of each memory block (in bytes).
3100 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003101 *
3102 * @return N/A
3103 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003104extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003105 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003106
3107/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003108 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * @param slab Address of the memory slab.
3113 * @param mem Pointer to block address area.
3114 * @param timeout Maximum time to wait for operation to complete
3115 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3116 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003117 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003118 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003120 * @retval -ENOMEM Returned without waiting.
3121 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003122 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003123extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003124 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003125
3126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * This routine releases a previously allocated memory block back to its
3130 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003132 * @param slab Address of the memory slab.
3133 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003134 *
3135 * @return N/A
3136 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003137extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003139/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * This routine gets the number of memory blocks that are currently
3143 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003145 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003148 */
Kumar Galacc334c72017-04-21 10:55:34 -05003149static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003151 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152}
3153
Peter Mitsisc001aa82016-10-13 13:53:37 -04003154/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * This routine gets the number of memory blocks that are currently
3158 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003163 */
Kumar Galacc334c72017-04-21 10:55:34 -05003164static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003165{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003166 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003167}
3168
Allan Stephensc98da842016-11-11 15:45:03 -05003169/**
3170 * @} end defgroup mem_slab_apis
3171 */
3172
3173/**
3174 * @cond INTERNAL_HIDDEN
3175 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176
Andy Ross73cb9582017-05-09 10:42:39 -07003177struct k_mem_pool_lvl {
3178 union {
3179 u32_t *bits_p;
3180 u32_t bits;
3181 };
3182 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003183};
3184
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003185struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003186 void *buf;
3187 size_t max_sz;
3188 u16_t n_max;
3189 u8_t n_levels;
3190 u8_t max_inline_level;
3191 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003192 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193};
3194
Andy Ross73cb9582017-05-09 10:42:39 -07003195#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196
Andy Ross73cb9582017-05-09 10:42:39 -07003197#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3198
3199#define _MPOOL_LVLS(maxsz, minsz) \
3200 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3201 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3202 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3203 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3204 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3205 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3206 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3207 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3208 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3209 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3210 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3211 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3212 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3213 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3214 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3215 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3216
3217/* Rounds the needed bits up to integer multiples of u32_t */
3218#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3219 ((((n_max) << (2*(l))) + 31) / 32)
3220
3221/* One word gets stored free unioned with the pointer, otherwise the
3222 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003223 */
Andy Ross73cb9582017-05-09 10:42:39 -07003224#define _MPOOL_LBIT_WORDS(n_max, l) \
3225 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3226 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003227
Andy Ross73cb9582017-05-09 10:42:39 -07003228/* How many bytes for the bitfields of a single level? */
3229#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3230 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3231 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003232
Andy Ross73cb9582017-05-09 10:42:39 -07003233/* Size of the bitmap array that follows the buffer in allocated memory */
3234#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3235 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3236 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3237 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3238 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3239 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3240 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3241 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3242 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3243 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3244 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3245 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3246 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3247 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3248 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3249 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3250 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003251
3252/**
Allan Stephensc98da842016-11-11 15:45:03 -05003253 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003254 */
3255
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003256/**
Allan Stephensc98da842016-11-11 15:45:03 -05003257 * @addtogroup mem_pool_apis
3258 * @{
3259 */
3260
3261/**
3262 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3265 * long. The memory pool allows blocks to be repeatedly partitioned into
3266 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003267 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003268 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003269 * If the pool is to be accessed outside the module where it is defined, it
3270 * can be declared via
3271 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003272 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003275 * @param minsz Size of the smallest blocks in the pool (in bytes).
3276 * @param maxsz Size of the largest blocks in the pool (in bytes).
3277 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003279 */
Andy Ross73cb9582017-05-09 10:42:39 -07003280#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3281 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3282 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3283 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3284 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3285 .buf = _mpool_buf_##name, \
3286 .max_sz = maxsz, \
3287 .n_max = nmax, \
3288 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3289 .levels = _mpool_lvls_##name, \
3290 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003291
Peter Mitsis937042c2016-10-13 13:18:26 -04003292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @param pool Address of the memory pool.
3298 * @param block Pointer to block descriptor for the allocated memory.
3299 * @param size Amount of memory to allocate (in bytes).
3300 * @param timeout Maximum time to wait for operation to complete
3301 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3302 * or K_FOREVER to wait as long as necessary.
3303 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003304 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003306 * @retval -ENOMEM Returned without waiting.
3307 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003308 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003309extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003310 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003311
3312/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * This routine releases a previously allocated memory block back to its
3316 * memory pool.
3317 *
3318 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003319 *
3320 * @return N/A
3321 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003323
3324/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003325 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003326 *
Andy Ross73cb9582017-05-09 10:42:39 -07003327 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003328 *
Andy Ross73cb9582017-05-09 10:42:39 -07003329 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003330 *
3331 * @return N/A
3332 */
Andy Ross73cb9582017-05-09 10:42:39 -07003333static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003334
3335/**
Allan Stephensc98da842016-11-11 15:45:03 -05003336 * @} end addtogroup mem_pool_apis
3337 */
3338
3339/**
3340 * @defgroup heap_apis Heap Memory Pool APIs
3341 * @ingroup kernel_apis
3342 * @{
3343 */
3344
3345/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003349 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003354 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003355extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003356
3357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003359 *
3360 * This routine provides traditional free() semantics. The memory being
3361 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003362 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003363 * If @a ptr is NULL, no operation is performed.
3364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003366 *
3367 * @return N/A
3368 */
3369extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370
Allan Stephensc98da842016-11-11 15:45:03 -05003371/**
3372 * @} end defgroup heap_apis
3373 */
3374
Benjamin Walshacc68c12017-01-29 18:57:45 -05003375/* polling API - PRIVATE */
3376
Benjamin Walshb0179862017-02-02 16:39:57 -05003377#ifdef CONFIG_POLL
3378#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3379#else
3380#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3381#endif
3382
Benjamin Walshacc68c12017-01-29 18:57:45 -05003383/* private - implementation data created as needed, per-type */
3384struct _poller {
3385 struct k_thread *thread;
3386};
3387
3388/* private - types bit positions */
3389enum _poll_types_bits {
3390 /* can be used to ignore an event */
3391 _POLL_TYPE_IGNORE,
3392
3393 /* to be signaled by k_poll_signal() */
3394 _POLL_TYPE_SIGNAL,
3395
3396 /* semaphore availability */
3397 _POLL_TYPE_SEM_AVAILABLE,
3398
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003399 /* queue/fifo/lifo data availability */
3400 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003401
3402 _POLL_NUM_TYPES
3403};
3404
3405#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3406
3407/* private - states bit positions */
3408enum _poll_states_bits {
3409 /* default state when creating event */
3410 _POLL_STATE_NOT_READY,
3411
3412 /* there was another poller already on the object */
3413 _POLL_STATE_EADDRINUSE,
3414
3415 /* signaled by k_poll_signal() */
3416 _POLL_STATE_SIGNALED,
3417
3418 /* semaphore is available */
3419 _POLL_STATE_SEM_AVAILABLE,
3420
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003421 /* data is available to read on queue/fifo/lifo */
3422 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003423
3424 _POLL_NUM_STATES
3425};
3426
3427#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3428
3429#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003430 (32 - (0 \
3431 + 8 /* tag */ \
3432 + _POLL_NUM_TYPES \
3433 + _POLL_NUM_STATES \
3434 + 1 /* modes */ \
3435 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003436
3437#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3438#error overflow of 32-bit word in struct k_poll_event
3439#endif
3440
3441/* end of polling API - PRIVATE */
3442
3443
3444/**
3445 * @defgroup poll_apis Async polling APIs
3446 * @ingroup kernel_apis
3447 * @{
3448 */
3449
3450/* Public polling API */
3451
3452/* public - values for k_poll_event.type bitfield */
3453#define K_POLL_TYPE_IGNORE 0
3454#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3455#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003456#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3457#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003458
3459/* public - polling modes */
3460enum k_poll_modes {
3461 /* polling thread does not take ownership of objects when available */
3462 K_POLL_MODE_NOTIFY_ONLY = 0,
3463
3464 K_POLL_NUM_MODES
3465};
3466
3467/* public - values for k_poll_event.state bitfield */
3468#define K_POLL_STATE_NOT_READY 0
3469#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3470#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3471#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003472#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3473#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003474
3475/* public - poll signal object */
3476struct k_poll_signal {
3477 /* PRIVATE - DO NOT TOUCH */
3478 struct k_poll_event *poll_event;
3479
3480 /*
3481 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3482 * user resets it to 0.
3483 */
3484 unsigned int signaled;
3485
3486 /* custom result value passed to k_poll_signal() if needed */
3487 int result;
3488};
3489
3490#define K_POLL_SIGNAL_INITIALIZER() \
3491 { \
3492 .poll_event = NULL, \
3493 .signaled = 0, \
3494 .result = 0, \
3495 }
3496
3497struct k_poll_event {
3498 /* PRIVATE - DO NOT TOUCH */
3499 struct _poller *poller;
3500
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003501 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003502 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003503
Benjamin Walshacc68c12017-01-29 18:57:45 -05003504 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003505 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003506
3507 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003508 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003509
3510 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003511 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003512
3513 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003514 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003515
3516 /* per-type data */
3517 union {
3518 void *obj;
3519 struct k_poll_signal *signal;
3520 struct k_sem *sem;
3521 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003522 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003523 };
3524};
3525
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003526#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003527 { \
3528 .poller = NULL, \
3529 .type = event_type, \
3530 .state = K_POLL_STATE_NOT_READY, \
3531 .mode = event_mode, \
3532 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003533 { .obj = event_obj }, \
3534 }
3535
3536#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3537 event_tag) \
3538 { \
3539 .type = event_type, \
3540 .tag = event_tag, \
3541 .state = K_POLL_STATE_NOT_READY, \
3542 .mode = event_mode, \
3543 .unused = 0, \
3544 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003545 }
3546
3547/**
3548 * @brief Initialize one struct k_poll_event instance
3549 *
3550 * After this routine is called on a poll event, the event it ready to be
3551 * placed in an event array to be passed to k_poll().
3552 *
3553 * @param event The event to initialize.
3554 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3555 * values. Only values that apply to the same object being polled
3556 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3557 * event.
3558 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3559 * @param obj Kernel object or poll signal.
3560 *
3561 * @return N/A
3562 */
3563
Kumar Galacc334c72017-04-21 10:55:34 -05003564extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003565 int mode, void *obj);
3566
3567/**
3568 * @brief Wait for one or many of multiple poll events to occur
3569 *
3570 * This routine allows a thread to wait concurrently for one or many of
3571 * multiple poll events to have occurred. Such events can be a kernel object
3572 * being available, like a semaphore, or a poll signal event.
3573 *
3574 * When an event notifies that a kernel object is available, the kernel object
3575 * is not "given" to the thread calling k_poll(): it merely signals the fact
3576 * that the object was available when the k_poll() call was in effect. Also,
3577 * all threads trying to acquire an object the regular way, i.e. by pending on
3578 * the object, have precedence over the thread polling on the object. This
3579 * means that the polling thread will never get the poll event on an object
3580 * until the object becomes available and its pend queue is empty. For this
3581 * reason, the k_poll() call is more effective when the objects being polled
3582 * only have one thread, the polling thread, trying to acquire them.
3583 *
3584 * Only one thread can be polling for a particular object at a given time. If
3585 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3586 * and returns as soon as it has finished handling the other events. This means
3587 * that k_poll() can return -EADDRINUSE and have the state value of some events
3588 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3589 * parameter is ignored.
3590 *
3591 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3592 * events that were passed to k_poll() and check the state field for the values
3593 * that were expected and take the associated actions.
3594 *
3595 * Before being reused for another call to k_poll(), the user has to reset the
3596 * state field to K_POLL_STATE_NOT_READY.
3597 *
3598 * @param events An array of pointers to events to be polled for.
3599 * @param num_events The number of events in the array.
3600 * @param timeout Waiting period for an event to be ready (in milliseconds),
3601 * or one of the special values K_NO_WAIT and K_FOREVER.
3602 *
3603 * @retval 0 One or more events are ready.
3604 * @retval -EADDRINUSE One or more objects already had a poller.
3605 * @retval -EAGAIN Waiting period timed out.
3606 */
3607
3608extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003609 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003610
3611/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003612 * @brief Initialize a poll signal object.
3613 *
3614 * Ready a poll signal object to be signaled via k_poll_signal().
3615 *
3616 * @param signal A poll signal.
3617 *
3618 * @return N/A
3619 */
3620
3621extern void k_poll_signal_init(struct k_poll_signal *signal);
3622
3623/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003624 * @brief Signal a poll signal object.
3625 *
3626 * This routine makes ready a poll signal, which is basically a poll event of
3627 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3628 * made ready to run. A @a result value can be specified.
3629 *
3630 * The poll signal contains a 'signaled' field that, when set by
3631 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3632 * be reset by the user before being passed again to k_poll() or k_poll() will
3633 * consider it being signaled, and will return immediately.
3634 *
3635 * @param signal A poll signal.
3636 * @param result The value to store in the result field of the signal.
3637 *
3638 * @retval 0 The signal was delivered successfully.
3639 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3640 */
3641
3642extern int k_poll_signal(struct k_poll_signal *signal, int result);
3643
3644/* private internal function */
3645extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003646 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003647
3648/**
3649 * @} end defgroup poll_apis
3650 */
3651
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003652/**
3653 * @brief Make the CPU idle.
3654 *
3655 * This function makes the CPU idle until an event wakes it up.
3656 *
3657 * In a regular system, the idle thread should be the only thread responsible
3658 * for making the CPU idle and triggering any type of power management.
3659 * However, in some more constrained systems, such as a single-threaded system,
3660 * the only thread would be responsible for this if needed.
3661 *
3662 * @return N/A
3663 */
3664extern void k_cpu_idle(void);
3665
3666/**
3667 * @brief Make the CPU idle in an atomic fashion.
3668 *
3669 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3670 * must be done atomically before making the CPU idle.
3671 *
3672 * @param key Interrupt locking key obtained from irq_lock().
3673 *
3674 * @return N/A
3675 */
3676extern void k_cpu_atomic_idle(unsigned int key);
3677
Kumar Galacc334c72017-04-21 10:55:34 -05003678extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003679
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680#include <arch/cpu.h>
3681
Andrew Boiecdb94d62017-04-18 15:22:05 -07003682#ifdef _ARCH_EXCEPT
3683/* This archtecture has direct support for triggering a CPU exception */
3684#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3685#else
3686
3687#include <misc/printk.h>
3688
3689/* NOTE: This is the implementation for arches that do not implement
3690 * _ARCH_EXCEPT() to generate a real CPU exception.
3691 *
3692 * We won't have a real exception frame to determine the PC value when
3693 * the oops occurred, so print file and line number before we jump into
3694 * the fatal error handler.
3695 */
3696#define _k_except_reason(reason) do { \
3697 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3698 _NanoFatalErrorHandler(reason, &_default_esf); \
3699 CODE_UNREACHABLE; \
3700 } while (0)
3701
3702#endif /* _ARCH__EXCEPT */
3703
3704/**
3705 * @brief Fatally terminate a thread
3706 *
3707 * This should be called when a thread has encountered an unrecoverable
3708 * runtime condition and needs to terminate. What this ultimately
3709 * means is determined by the _fatal_error_handler() implementation, which
3710 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3711 *
3712 * If this is called from ISR context, the default system fatal error handler
3713 * will treat it as an unrecoverable system error, just like k_panic().
3714 */
3715#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3716
3717/**
3718 * @brief Fatally terminate the system
3719 *
3720 * This should be called when the Zephyr kernel has encountered an
3721 * unrecoverable runtime condition and needs to terminate. What this ultimately
3722 * means is determined by the _fatal_error_handler() implementation, which
3723 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3724 */
3725#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3726
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003727/*
3728 * private APIs that are utilized by one or more public APIs
3729 */
3730
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003731#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003732extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003733#else
3734#define _init_static_threads() do { } while ((0))
3735#endif
3736
3737extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003738extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739
Andrew Boiedc5d9352017-06-02 12:56:47 -07003740/* arch/cpu.h may declare an architecture or platform-specific macro
3741 * for properly declaring stacks, compatible with MMU/MPU constraints if
3742 * enabled
3743 */
3744#ifdef _ARCH_THREAD_STACK_DEFINE
3745#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3746#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3747 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3748#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3749#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
3750#define K_THREAD_STACK_BUFFER(sym) _ARCH_THREAD_STACK_BUFFER(sym)
3751#else
3752/**
3753 * @brief Declare a toplevel thread stack memory region
3754 *
3755 * This declares a region of memory suitable for use as a thread's stack.
3756 *
3757 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3758 * 'noinit' section so that it isn't zeroed at boot
3759 *
3760 * The declared symbol will always be a character array which can be passed to
3761 * k_thread_create, but should otherwise not be manipulated.
3762 *
3763 * It is legal to precede this definition with the 'static' keyword.
3764 *
3765 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3766 * parameter of k_thread_create(), it may not be the same as the
3767 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3768 *
3769 * @param sym Thread stack symbol name
3770 * @param size Size of the stack memory region
3771 */
3772#define K_THREAD_STACK_DEFINE(sym, size) \
3773 char __noinit __aligned(STACK_ALIGN) sym[size]
3774
3775/**
3776 * @brief Declare a toplevel array of thread stack memory regions
3777 *
3778 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3779 * definition for additional details and constraints.
3780 *
3781 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3782 * 'noinit' section so that it isn't zeroed at boot
3783 *
3784 * @param sym Thread stack symbol name
3785 * @param nmemb Number of stacks to declare
3786 * @param size Size of the stack memory region
3787 */
3788
3789#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3790 char __noinit __aligned(STACK_ALIGN) sym[nmemb][size]
3791
3792/**
3793 * @brief Declare an embedded stack memory region
3794 *
3795 * Used for stacks embedded within other data structures. Use is highly
3796 * discouraged but in some cases necessary. For memory protection scenarios,
3797 * it is very important that any RAM preceding this member not be writable
3798 * by threads else a stack overflow will lead to silent corruption. In other
3799 * words, the containing data structure should live in RAM owned by the kernel.
3800 *
3801 * @param sym Thread stack symbol name
3802 * @param size Size of the stack memory region
3803 */
3804#define K_THREAD_STACK_MEMBER(sym, size) \
3805 char __aligned(STACK_ALIGN) sym[size]
3806
3807/**
3808 * @brief Return the size in bytes of a stack memory region
3809 *
3810 * Convenience macro for passing the desired stack size to k_thread_create()
3811 * since the underlying implementation may actually create something larger
3812 * (for instance a guard area).
3813 *
3814 * The value returned here is guaranteed to match the 'size' parameter
3815 * passed to K_THREAD_STACK_DEFINE and related macros.
3816 *
3817 * @param sym Stack memory symbol
3818 * @return Size of the stack
3819 */
3820#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
3821
3822/**
3823 * @brief Get a pointer to the physical stack buffer
3824 *
3825 * Convenience macro to get at the real underlying stack buffer used by
3826 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
3827 * This is really only intended for diagnostic tools which want to examine
3828 * stack memory contents.
3829 *
3830 * @param sym Declared stack symbol name
3831 * @return The buffer itself, a char *
3832 */
3833#define K_THREAD_STACK_BUFFER(sym) sym
3834
3835#endif /* _ARCH_DECLARE_STACK */
3836
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003837#ifdef __cplusplus
3838}
3839#endif
3840
Andrew Boiee004dec2016-11-07 09:01:19 -08003841#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3842/*
3843 * Define new and delete operators.
3844 * At this moment, the operators do nothing since objects are supposed
3845 * to be statically allocated.
3846 */
3847inline void operator delete(void *ptr)
3848{
3849 (void)ptr;
3850}
3851
3852inline void operator delete[](void *ptr)
3853{
3854 (void)ptr;
3855}
3856
3857inline void *operator new(size_t size)
3858{
3859 (void)size;
3860 return NULL;
3861}
3862
3863inline void *operator new[](size_t size)
3864{
3865 (void)size;
3866 return NULL;
3867}
3868
3869/* Placement versions of operator new and delete */
3870inline void operator delete(void *ptr1, void *ptr2)
3871{
3872 (void)ptr1;
3873 (void)ptr2;
3874}
3875
3876inline void operator delete[](void *ptr1, void *ptr2)
3877{
3878 (void)ptr1;
3879 (void)ptr2;
3880}
3881
3882inline void *operator new(size_t size, void *ptr)
3883{
3884 (void)size;
3885 return ptr;
3886}
3887
3888inline void *operator new[](size_t size, void *ptr)
3889{
3890 (void)size;
3891 return ptr;
3892}
3893
3894#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3895
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003896#endif /* !_ASMLANGUAGE */
3897
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898#endif /* _kernel__h_ */