blob: 104c32b222eef1046530e8e41345a77e3cb7b045 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
101#define _POLL_EVENT_OBJ_INIT \
102 .poll_event = NULL,
103#define _POLL_EVENT struct k_poll_event *poll_event
104#else
105#define _POLL_EVENT_OBJ_INIT
106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Andrew Boie73abd322017-04-04 13:19:13 -0700126/* timeouts */
127
128struct _timeout;
129typedef void (*_timeout_func_t)(struct _timeout *t);
130
131struct _timeout {
132 sys_dnode_t node;
133 struct k_thread *thread;
134 sys_dlist_t *wait_q;
135 s32_t delta_ticks_from_prev;
136 _timeout_func_t func;
137};
138
139extern s32_t _timeout_remaining_get(struct _timeout *timeout);
140
141/* Threads */
142typedef void (*_thread_entry_t)(void *, void *, void *);
143
144#ifdef CONFIG_THREAD_MONITOR
145struct __thread_entry {
146 _thread_entry_t pEntry;
147 void *parameter1;
148 void *parameter2;
149 void *parameter3;
150};
151#endif
152
153/* can be used for creating 'dummy' threads, e.g. for pending on objects */
154struct _thread_base {
155
156 /* this thread's entry in a ready/wait queue */
157 sys_dnode_t k_q_node;
158
159 /* user facing 'thread options'; values defined in include/kernel.h */
160 u8_t user_options;
161
162 /* thread state */
163 u8_t thread_state;
164
165 /*
166 * scheduler lock count and thread priority
167 *
168 * These two fields control the preemptibility of a thread.
169 *
170 * When the scheduler is locked, sched_locked is decremented, which
171 * means that the scheduler is locked for values from 0xff to 0x01. A
172 * thread is coop if its prio is negative, thus 0x80 to 0xff when
173 * looked at the value as unsigned.
174 *
175 * By putting them end-to-end, this means that a thread is
176 * non-preemptible if the bundled value is greater than or equal to
177 * 0x0080.
178 */
179 union {
180 struct {
181#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
182 u8_t sched_locked;
183 s8_t prio;
184#else /* LITTLE and PDP */
185 s8_t prio;
186 u8_t sched_locked;
187#endif
188 };
189 u16_t preempt;
190 };
191
192 /* data returned by APIs */
193 void *swap_data;
194
195#ifdef CONFIG_SYS_CLOCK_EXISTS
196 /* this thread's entry in a timeout queue */
197 struct _timeout timeout;
198#endif
199
200};
201
202typedef struct _thread_base _thread_base_t;
203
204#if defined(CONFIG_THREAD_STACK_INFO)
205/* Contains the stack information of a thread */
206struct _thread_stack_info {
207 /* Stack Start */
208 u32_t start;
209 /* Stack Size */
210 u32_t size;
211};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700212
213typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700214#endif /* CONFIG_THREAD_STACK_INFO */
215
216struct k_thread {
217
218 struct _thread_base base;
219
220 /* defined by the architecture, but all archs need these */
221 struct _caller_saved caller_saved;
222 struct _callee_saved callee_saved;
223
224 /* static thread init data */
225 void *init_data;
226
227 /* abort function */
228 void (*fn_abort)(void);
229
230#if defined(CONFIG_THREAD_MONITOR)
231 /* thread entry and parameters description */
232 struct __thread_entry *entry;
233
234 /* next item in list of all threads */
235 struct k_thread *next_thread;
236#endif
237
238#ifdef CONFIG_THREAD_CUSTOM_DATA
239 /* crude thread-local storage */
240 void *custom_data;
241#endif
242
243#ifdef CONFIG_ERRNO
244 /* per-thread errno variable */
245 int errno_var;
246#endif
247
248#if defined(CONFIG_THREAD_STACK_INFO)
249 /* Stack Info */
250 struct _thread_stack_info stack_info;
251#endif /* CONFIG_THREAD_STACK_INFO */
252
253 /* arch-specifics: must always be at the end */
254 struct _thread_arch arch;
255};
256
257typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400258typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700259#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400260
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400261enum execution_context_types {
262 K_ISR = 0,
263 K_COOP_THREAD,
264 K_PREEMPT_THREAD,
265};
266
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400267/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100268 * @defgroup profiling_apis Profiling APIs
269 * @ingroup kernel_apis
270 * @{
271 */
272
273/**
274 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
275 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700276 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100277 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
278 *
279 * CONFIG_MAIN_STACK_SIZE
280 * CONFIG_IDLE_STACK_SIZE
281 * CONFIG_ISR_STACK_SIZE
282 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
283 *
284 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
285 * produce output.
286 *
287 * @return N/A
288 */
289extern void k_call_stacks_analyze(void);
290
291/**
292 * @} end defgroup profiling_apis
293 */
294
295/**
Allan Stephensc98da842016-11-11 15:45:03 -0500296 * @defgroup thread_apis Thread APIs
297 * @ingroup kernel_apis
298 * @{
299 */
300
301/**
Allan Stephens5eceb852016-11-16 10:16:30 -0500302 * @typedef k_thread_entry_t
303 * @brief Thread entry point function type.
304 *
305 * A thread's entry point function is invoked when the thread starts executing.
306 * Up to 3 argument values can be passed to the function.
307 *
308 * The thread terminates execution permanently if the entry point function
309 * returns. The thread is responsible for releasing any shared resources
310 * it may own (such as mutexes and dynamically allocated memory), prior to
311 * returning.
312 *
313 * @param p1 First argument.
314 * @param p2 Second argument.
315 * @param p3 Third argument.
316 *
317 * @return N/A
318 */
319typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
320
Benjamin Walshed240f22017-01-22 13:05:08 -0500321#endif /* !_ASMLANGUAGE */
322
323
324/*
325 * Thread user options. May be needed by assembly code. Common part uses low
326 * bits, arch-specific use high bits.
327 */
328
329/* system thread that must not abort */
330#define K_ESSENTIAL (1 << 0)
331
332#if defined(CONFIG_FP_SHARING)
333/* thread uses floating point registers */
334#define K_FP_REGS (1 << 1)
335#endif
336
337#ifdef CONFIG_X86
338/* x86 Bitmask definitions for threads user options */
339
340#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
341/* thread uses SSEx (and also FP) registers */
342#define K_SSE_REGS (1 << 7)
343#endif
344#endif
345
346/* end - thread options */
347
348#if !defined(_ASMLANGUAGE)
349
Allan Stephens5eceb852016-11-16 10:16:30 -0500350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500351 * @brief Spawn a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500353 * This routine initializes a thread, then schedules it for execution.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500355 * The new thread may be scheduled for immediate execution or a delayed start.
356 * If the newly spawned thread does not have a delayed start the kernel
357 * scheduler may preempt the current thread to allow the new thread to
358 * execute.
359 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700360 * Kernel data structures for bookkeeping and context storage for this thread
361 * will be placed at the beginning of the thread's stack memory region and may
362 * become corrupted if too much of the stack is used. This function has been
363 * deprecated in favor of k_thread_create() to give the user more control on
364 * where these data structures reside.
365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500366 * Thread options are architecture-specific, and can include K_ESSENTIAL,
367 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
368 * them using "|" (the logical OR operator).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400369 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700370 * The stack itself should be declared with K_THREAD_STACK_DEFINE or variant
371 * macros. The stack size parameter should either be a defined constant
372 * also passed to K_THREAD_STACK_DEFINE, or the value of K_THREAD_STACK_SIZEOF.
373 * Do not use regular C sizeof().
374 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400375 * @param stack Pointer to the stack space.
376 * @param stack_size Stack size in bytes.
377 * @param entry Thread entry function.
378 * @param p1 1st entry point parameter.
379 * @param p2 2nd entry point parameter.
380 * @param p3 3rd entry point parameter.
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500381 * @param prio Thread priority.
382 * @param options Thread options.
383 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500385 * @return ID of new thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386 */
Andrew Boied26cf2d2017-03-30 13:07:02 -0700387extern __deprecated k_tid_t k_thread_spawn(char *stack, size_t stack_size,
Allan Stephens5eceb852016-11-16 10:16:30 -0500388 k_thread_entry_t entry,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400389 void *p1, void *p2, void *p3,
Kumar Galacc334c72017-04-21 10:55:34 -0500390 int prio, u32_t options, s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400391
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400392/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700393 * @brief Create a thread.
394 *
395 * This routine initializes a thread, then schedules it for execution.
396 *
397 * The new thread may be scheduled for immediate execution or a delayed start.
398 * If the newly spawned thread does not have a delayed start the kernel
399 * scheduler may preempt the current thread to allow the new thread to
400 * execute.
401 *
402 * Thread options are architecture-specific, and can include K_ESSENTIAL,
403 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
404 * them using "|" (the logical OR operator).
405 *
406 * Historically, users often would use the beginning of the stack memory region
407 * to store the struct k_thread data, although corruption will occur if the
408 * stack overflows this region and stack protection features may not detect this
409 * situation.
410 *
411 * @param new_thread Pointer to uninitialized struct k_thread
412 * @param stack Pointer to the stack space.
413 * @param stack_size Stack size in bytes.
414 * @param entry Thread entry function.
415 * @param p1 1st entry point parameter.
416 * @param p2 2nd entry point parameter.
417 * @param p3 3rd entry point parameter.
418 * @param prio Thread priority.
419 * @param options Thread options.
420 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
421 *
422 * @return ID of new thread.
423 */
424extern k_tid_t k_thread_create(struct k_thread *new_thread, char *stack,
425 size_t stack_size,
426 void (*entry)(void *, void *, void*),
427 void *p1, void *p2, void *p3,
428 int prio, u32_t options, s32_t delay);
429
430/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500431 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400432 *
Allan Stephensc98da842016-11-11 15:45:03 -0500433 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500434 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500436 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400437 *
438 * @return N/A
439 */
Kumar Galacc334c72017-04-21 10:55:34 -0500440extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400441
442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500443 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400444 *
445 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500446 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400447 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400448 * @return N/A
449 */
Kumar Galacc334c72017-04-21 10:55:34 -0500450extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400451
452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500453 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500455 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500457 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
459 * @return N/A
460 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400461extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400462
463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500464 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500466 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500468 * If @a thread is not currently sleeping, the routine has no effect.
469 *
470 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400471 *
472 * @return N/A
473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475
476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500477 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500479 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400480 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400481extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400482
483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500484 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500486 * This routine prevents @a thread from executing if it has not yet started
487 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500489 * @param thread ID of thread to cancel.
490 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700491 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500492 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400493 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400494extern int k_thread_cancel(k_tid_t thread);
495
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400496/**
Allan Stephensc98da842016-11-11 15:45:03 -0500497 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500499 * This routine permanently stops execution of @a thread. The thread is taken
500 * off all kernel queues it is part of (i.e. the ready queue, the timeout
501 * queue, or a kernel object wait queue). However, any kernel resources the
502 * thread might currently own (such as mutexes or memory blocks) are not
503 * released. It is the responsibility of the caller of this routine to ensure
504 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500506 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400507 *
508 * @return N/A
509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400510extern void k_thread_abort(k_tid_t thread);
511
Allan Stephensc98da842016-11-11 15:45:03 -0500512/**
513 * @cond INTERNAL_HIDDEN
514 */
515
Benjamin Walshd211a522016-12-06 11:44:01 -0500516/* timeout has timed out and is not on _timeout_q anymore */
517#define _EXPIRED (-2)
518
519/* timeout is not in use */
520#define _INACTIVE (-1)
521
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400522struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700523 struct k_thread *init_thread;
524 char *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400525 unsigned int init_stack_size;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500526 void (*init_entry)(void *, void *, void *);
527 void *init_p1;
528 void *init_p2;
529 void *init_p3;
530 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500531 u32_t init_options;
532 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500533 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500534 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400535};
536
Andrew Boied26cf2d2017-03-30 13:07:02 -0700537#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400538 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500539 prio, options, delay, abort, groups) \
540 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700541 .init_thread = (thread), \
542 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500543 .init_stack_size = (stack_size), \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400544 .init_entry = (void (*)(void *, void *, void *))entry, \
545 .init_p1 = (void *)p1, \
546 .init_p2 = (void *)p2, \
547 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500548 .init_prio = (prio), \
549 .init_options = (options), \
550 .init_delay = (delay), \
551 .init_abort = (abort), \
552 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400553 }
554
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400555/**
Allan Stephensc98da842016-11-11 15:45:03 -0500556 * INTERNAL_HIDDEN @endcond
557 */
558
559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * @brief Statically define and initialize a thread.
561 *
562 * The thread may be scheduled for immediate execution or a delayed start.
563 *
564 * Thread options are architecture-specific, and can include K_ESSENTIAL,
565 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
566 * them using "|" (the logical OR operator).
567 *
568 * The ID of the thread can be accessed using:
569 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500570 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500571 *
572 * @param name Name of the thread.
573 * @param stack_size Stack size in bytes.
574 * @param entry Thread entry function.
575 * @param p1 1st entry point parameter.
576 * @param p2 2nd entry point parameter.
577 * @param p3 3rd entry point parameter.
578 * @param prio Thread priority.
579 * @param options Thread options.
580 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400581 *
582 * @internal It has been observed that the x86 compiler by default aligns
583 * these _static_thread_data structures to 32-byte boundaries, thereby
584 * wasting space. To work around this, force a 4-byte alignment.
585 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500586#define K_THREAD_DEFINE(name, stack_size, \
587 entry, p1, p2, p3, \
588 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700589 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700590 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500591 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500592 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700593 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
594 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500595 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700596 NULL, 0); \
597 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500600 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500602 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @param thread ID of thread whose priority is needed.
605 *
606 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400607 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500608extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400609
610/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500611 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500613 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400614 *
615 * Rescheduling can occur immediately depending on the priority @a thread is
616 * set to:
617 *
618 * - If its priority is raised above the priority of the caller of this
619 * function, and the caller is preemptible, @a thread will be scheduled in.
620 *
621 * - If the caller operates on itself, it lowers its priority below that of
622 * other threads in the system, and the caller is preemptible, the thread of
623 * highest priority will be scheduled in.
624 *
625 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
626 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
627 * highest priority.
628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500629 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630 * @param prio New priority.
631 *
632 * @warning Changing the priority of a thread currently involved in mutex
633 * priority inheritance may result in undefined behavior.
634 *
635 * @return N/A
636 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400637extern void k_thread_priority_set(k_tid_t thread, int prio);
638
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500640 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * This routine prevents the kernel scheduler from making @a thread the
643 * current thread. All other internal operations on @a thread are still
644 * performed; for example, any timeout it is waiting on keeps ticking,
645 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500647 * If @a thread is already suspended, the routine has no effect.
648 *
649 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 *
651 * @return N/A
652 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400653extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400654
655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500658 * This routine allows the kernel scheduler to make @a thread the current
659 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * If @a thread is not currently suspended, the routine has no effect.
662 *
663 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400664 *
665 * @return N/A
666 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400667extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400668
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * This routine specifies how the scheduler will perform time slicing of
673 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500675 * To enable time slicing, @a slice must be non-zero. The scheduler
676 * ensures that no thread runs for more than the specified time limit
677 * before other threads of that priority are given a chance to execute.
678 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700679 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400682 * execute. Once the scheduler selects a thread for execution, there is no
683 * minimum guaranteed time the thread will execute before threads of greater or
684 * equal priority are scheduled.
685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500686 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400687 * for execution, this routine has no effect; the thread is immediately
688 * rescheduled after the slice period expires.
689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500690 * To disable timeslicing, set both @a slice and @a prio to zero.
691 *
692 * @param slice Maximum time slice length (in milliseconds).
693 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694 *
695 * @return N/A
696 */
Kumar Galacc334c72017-04-21 10:55:34 -0500697extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400698
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699/**
Allan Stephensc98da842016-11-11 15:45:03 -0500700 * @} end defgroup thread_apis
701 */
702
703/**
704 * @addtogroup isr_apis
705 * @{
706 */
707
708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
Allan Stephensc98da842016-11-11 15:45:03 -0500711 * This routine allows the caller to customize its actions, depending on
712 * whether it is a thread or an ISR.
713 *
714 * @note Can be called by ISRs.
715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500716 * @return 0 if invoked by a thread.
717 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500719extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400720
Benjamin Walsh445830d2016-11-10 15:54:27 -0500721/**
722 * @brief Determine if code is running in a preemptible thread.
723 *
Allan Stephensc98da842016-11-11 15:45:03 -0500724 * This routine allows the caller to customize its actions, depending on
725 * whether it can be preempted by another thread. The routine returns a 'true'
726 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500727 *
Allan Stephensc98da842016-11-11 15:45:03 -0500728 * - The code is running in a thread, not at ISR.
729 * - The thread's priority is in the preemptible range.
730 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500731 *
Allan Stephensc98da842016-11-11 15:45:03 -0500732 * @note Can be called by ISRs.
733 *
734 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500735 * @return Non-zero if invoked by a preemptible thread.
736 */
737extern int k_is_preempt_thread(void);
738
Allan Stephensc98da842016-11-11 15:45:03 -0500739/**
740 * @} end addtogroup isr_apis
741 */
742
743/**
744 * @addtogroup thread_apis
745 * @{
746 */
747
748/**
749 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500750 *
Allan Stephensc98da842016-11-11 15:45:03 -0500751 * This routine prevents the current thread from being preempted by another
752 * thread by instructing the scheduler to treat it as a cooperative thread.
753 * If the thread subsequently performs an operation that makes it unready,
754 * it will be context switched out in the normal manner. When the thread
755 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500756 *
Allan Stephensc98da842016-11-11 15:45:03 -0500757 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500758 *
Allan Stephensc98da842016-11-11 15:45:03 -0500759 * @note k_sched_lock() and k_sched_unlock() should normally be used
760 * when the operation being performed can be safely interrupted by ISRs.
761 * However, if the amount of processing involved is very small, better
762 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500763 *
764 * @return N/A
765 */
766extern void k_sched_lock(void);
767
Allan Stephensc98da842016-11-11 15:45:03 -0500768/**
769 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500770 *
Allan Stephensc98da842016-11-11 15:45:03 -0500771 * This routine reverses the effect of a previous call to k_sched_lock().
772 * A thread must call the routine once for each time it called k_sched_lock()
773 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500774 *
775 * @return N/A
776 */
777extern void k_sched_unlock(void);
778
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * Custom data is not used by the kernel itself, and is freely available
785 * for a thread to use as it sees fit. It can be used as a framework
786 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
790 * @return N/A
791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400792extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400801extern void *k_thread_custom_data_get(void);
802
803/**
Allan Stephensc98da842016-11-11 15:45:03 -0500804 * @} end addtogroup thread_apis
805 */
806
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400807#include <sys_clock.h>
808
Allan Stephensc2f15a42016-11-17 12:24:22 -0500809/**
810 * @addtogroup clock_apis
811 * @{
812 */
813
814/**
815 * @brief Generate null timeout delay.
816 *
817 * This macro generates a timeout delay that that instructs a kernel API
818 * not to wait if the requested operation cannot be performed immediately.
819 *
820 * @return Timeout delay value.
821 */
822#define K_NO_WAIT 0
823
824/**
825 * @brief Generate timeout delay from milliseconds.
826 *
827 * This macro generates a timeout delay that that instructs a kernel API
828 * to wait up to @a ms milliseconds to perform the requested operation.
829 *
830 * @param ms Duration in milliseconds.
831 *
832 * @return Timeout delay value.
833 */
Johan Hedberg14471692016-11-13 10:52:15 +0200834#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500835
836/**
837 * @brief Generate timeout delay from seconds.
838 *
839 * This macro generates a timeout delay that that instructs a kernel API
840 * to wait up to @a s seconds to perform the requested operation.
841 *
842 * @param s Duration in seconds.
843 *
844 * @return Timeout delay value.
845 */
Johan Hedberg14471692016-11-13 10:52:15 +0200846#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500847
848/**
849 * @brief Generate timeout delay from minutes.
850 *
851 * This macro generates a timeout delay that that instructs a kernel API
852 * to wait up to @a m minutes to perform the requested operation.
853 *
854 * @param m Duration in minutes.
855 *
856 * @return Timeout delay value.
857 */
Johan Hedberg14471692016-11-13 10:52:15 +0200858#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500859
860/**
861 * @brief Generate timeout delay from hours.
862 *
863 * This macro generates a timeout delay that that instructs a kernel API
864 * to wait up to @a h hours to perform the requested operation.
865 *
866 * @param h Duration in hours.
867 *
868 * @return Timeout delay value.
869 */
Johan Hedberg14471692016-11-13 10:52:15 +0200870#define K_HOURS(h) K_MINUTES((h) * 60)
871
Allan Stephensc98da842016-11-11 15:45:03 -0500872/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500873 * @brief Generate infinite timeout delay.
874 *
875 * This macro generates a timeout delay that that instructs a kernel API
876 * to wait as long as necessary to perform the requested operation.
877 *
878 * @return Timeout delay value.
879 */
880#define K_FOREVER (-1)
881
882/**
883 * @} end addtogroup clock_apis
884 */
885
886/**
Allan Stephensc98da842016-11-11 15:45:03 -0500887 * @cond INTERNAL_HIDDEN
888 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400889
Benjamin Walsh62092182016-12-20 14:39:08 -0500890/* kernel clocks */
891
892#if (sys_clock_ticks_per_sec == 1000) || \
893 (sys_clock_ticks_per_sec == 500) || \
894 (sys_clock_ticks_per_sec == 250) || \
895 (sys_clock_ticks_per_sec == 125) || \
896 (sys_clock_ticks_per_sec == 100) || \
897 (sys_clock_ticks_per_sec == 50) || \
898 (sys_clock_ticks_per_sec == 25) || \
899 (sys_clock_ticks_per_sec == 20) || \
900 (sys_clock_ticks_per_sec == 10) || \
901 (sys_clock_ticks_per_sec == 1)
902
903 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
904#else
905 /* yields horrible 64-bit math on many architectures: try to avoid */
906 #define _NON_OPTIMIZED_TICKS_PER_SEC
907#endif
908
909#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500910extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -0500911#else
Kumar Galacc334c72017-04-21 10:55:34 -0500912static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -0500913{
Kumar Galacc334c72017-04-21 10:55:34 -0500914 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -0500915}
916#endif
917
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500918/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800919#ifdef CONFIG_TICKLESS_KERNEL
920#define _TICK_ALIGN 0
921#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500922#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -0800923#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -0500924
Kumar Galacc334c72017-04-21 10:55:34 -0500925static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400926{
Benjamin Walsh62092182016-12-20 14:39:08 -0500927#ifdef CONFIG_SYS_CLOCK_EXISTS
928
929#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -0500930 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400931#else
Kumar Galacc334c72017-04-21 10:55:34 -0500932 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -0500933#endif
934
935#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -0400936 __ASSERT(ticks == 0, "");
937 return 0;
938#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400939}
940
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941struct k_timer {
942 /*
943 * _timeout structure must be first here if we want to use
944 * dynamic timer allocation. timeout.node is used in the double-linked
945 * list of free timers
946 */
947 struct _timeout timeout;
948
Allan Stephens45bfa372016-10-12 12:39:42 -0500949 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950 _wait_q_t wait_q;
951
952 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -0500953 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400954
955 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -0500956 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400957
958 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -0500959 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400960
Allan Stephens45bfa372016-10-12 12:39:42 -0500961 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -0500962 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400963
Benjamin Walshe4e98f92017-01-12 19:38:53 -0500964 /* user-specific data, also used to support legacy features */
965 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966
Anas Nashif2f203c22016-12-18 06:57:45 -0500967 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400968};
969
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/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001462 * @brief Peek element at the head of queue.
1463 *
1464 * Return element from the head of queue without removing it.
1465 *
1466 * @param queue Address of the queue.
1467 *
1468 * @return Head element, or NULL if queue is empty.
1469 */
1470static inline void *k_queue_peek_head(struct k_queue *queue)
1471{
1472 return sys_slist_peek_head(&queue->data_q);
1473}
1474
1475/**
1476 * @brief Peek element at the tail of queue.
1477 *
1478 * Return element from the tail of queue without removing it.
1479 *
1480 * @param queue Address of the queue.
1481 *
1482 * @return Tail element, or NULL if queue is empty.
1483 */
1484static inline void *k_queue_peek_tail(struct k_queue *queue)
1485{
1486 return sys_slist_peek_tail(&queue->data_q);
1487}
1488
1489/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001490 * @brief Statically define and initialize a queue.
1491 *
1492 * The queue can be accessed outside the module where it is defined using:
1493 *
1494 * @code extern struct k_queue <name>; @endcode
1495 *
1496 * @param name Name of the queue.
1497 */
1498#define K_QUEUE_DEFINE(name) \
1499 struct k_queue name \
1500 __in_section(_k_queue, static, name) = \
1501 K_QUEUE_INITIALIZER(name)
1502
1503/**
1504 * @} end defgroup queue_apis
1505 */
1506
1507/**
1508 * @cond INTERNAL_HIDDEN
1509 */
1510
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001511struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001512 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001513};
1514
Allan Stephensc98da842016-11-11 15:45:03 -05001515#define K_FIFO_INITIALIZER(obj) \
1516 { \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001517 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001518 }
1519
1520/**
1521 * INTERNAL_HIDDEN @endcond
1522 */
1523
1524/**
1525 * @defgroup fifo_apis Fifo APIs
1526 * @ingroup kernel_apis
1527 * @{
1528 */
1529
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001530/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001531 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001533 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001535 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001536 *
1537 * @return N/A
1538 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001539#define k_fifo_init(fifo) \
1540 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001541
1542/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001543 * @brief Cancel waiting on a fifo.
1544 *
1545 * This routine causes first thread pending on @a fifo, if any, to
1546 * return from k_fifo_get() call with NULL value (as if timeout
1547 * expired).
1548 *
1549 * @note Can be called by ISRs.
1550 *
1551 * @param fifo Address of the fifo.
1552 *
1553 * @return N/A
1554 */
1555#define k_fifo_cancel_wait(fifo) \
1556 k_queue_cancel_wait((struct k_queue *) fifo)
1557
1558/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001561 * This routine adds a data item to @a fifo. A fifo data item must be
1562 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1563 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001565 * @note Can be called by ISRs.
1566 *
1567 * @param fifo Address of the fifo.
1568 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001569 *
1570 * @return N/A
1571 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001572#define k_fifo_put(fifo, data) \
1573 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001574
1575/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001576 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * This routine adds a list of data items to @a fifo in one operation.
1579 * The data items must be in a singly-linked list, with the first 32 bits
1580 * each data item pointing to the next data item; the list must be
1581 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001583 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001585 * @param fifo Address of the fifo.
1586 * @param head Pointer to first node in singly-linked list.
1587 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588 *
1589 * @return N/A
1590 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001591#define k_fifo_put_list(fifo, head, tail) \
1592 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001593
1594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001595 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001597 * This routine adds a list of data items to @a fifo in one operation.
1598 * The data items must be in a singly-linked list implemented using a
1599 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001600 * and must be re-initialized via sys_slist_init().
1601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001602 * @note Can be called by ISRs.
1603 *
1604 * @param fifo Address of the fifo.
1605 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001606 *
1607 * @return N/A
1608 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001609#define k_fifo_put_slist(fifo, list) \
1610 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001611
1612/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001613 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001615 * This routine removes a data item from @a fifo in a "first in, first out"
1616 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001618 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1619 *
1620 * @param fifo Address of the fifo.
1621 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001622 * or one of the special values K_NO_WAIT and K_FOREVER.
1623 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001624 * @return Address of the data item if successful; NULL if returned
1625 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001626 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001627#define k_fifo_get(fifo, timeout) \
1628 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001630/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001631 * @brief Query a fifo to see if it has data available.
1632 *
1633 * Note that the data might be already gone by the time this function returns
1634 * if other threads is also trying to read from the fifo.
1635 *
1636 * @note Can be called by ISRs.
1637 *
1638 * @param fifo Address of the fifo.
1639 *
1640 * @return Non-zero if the fifo is empty.
1641 * @return 0 if data is available.
1642 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001643#define k_fifo_is_empty(fifo) \
1644 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001645
1646/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001647 * @brief Peek element at the head of fifo.
1648 *
1649 * Return element from the head of fifo without removing it. A usecase
1650 * for this is if elements of the fifo are themselves containers. Then
1651 * on each iteration of processing, a head container will be peeked,
1652 * and some data processed out of it, and only if the container is empty,
1653 * it will be completely remove from the fifo.
1654 *
1655 * @param fifo Address of the fifo.
1656 *
1657 * @return Head element, or NULL if the fifo is empty.
1658 */
1659#define k_fifo_peek_head(fifo) \
1660 k_queue_peek_head((struct k_queue *) fifo)
1661
1662/**
1663 * @brief Peek element at the tail of fifo.
1664 *
1665 * Return element from the tail of fifo (without removing it). A usecase
1666 * for this is if elements of the fifo are themselves containers. Then
1667 * it may be useful to add more data to the last container in fifo.
1668 *
1669 * @param fifo Address of the fifo.
1670 *
1671 * @return Tail element, or NULL if fifo is empty.
1672 */
1673#define k_fifo_peek_tail(fifo) \
1674 k_queue_peek_tail((struct k_queue *) fifo)
1675
1676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001679 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001680 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001681 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001684 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001685#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001686 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001687 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001688 K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001689
Allan Stephensc98da842016-11-11 15:45:03 -05001690/**
1691 * @} end defgroup fifo_apis
1692 */
1693
1694/**
1695 * @cond INTERNAL_HIDDEN
1696 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001697
1698struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001699 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001700};
1701
Allan Stephensc98da842016-11-11 15:45:03 -05001702#define K_LIFO_INITIALIZER(obj) \
1703 { \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001704 ._queue = K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001705 }
1706
1707/**
1708 * INTERNAL_HIDDEN @endcond
1709 */
1710
1711/**
1712 * @defgroup lifo_apis Lifo APIs
1713 * @ingroup kernel_apis
1714 * @{
1715 */
1716
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723 *
1724 * @return N/A
1725 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001726#define k_lifo_init(lifo) \
1727 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728
1729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * This routine adds a data item to @a lifo. A lifo data item must be
1733 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1734 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @note Can be called by ISRs.
1737 *
1738 * @param lifo Address of the lifo.
1739 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740 *
1741 * @return N/A
1742 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001743#define k_lifo_put(lifo, data) \
1744 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001745
1746/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001747 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * This routine removes a data item from @a lifo in a "last in, first out"
1750 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1753 *
1754 * @param lifo Address of the lifo.
1755 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756 * or one of the special values K_NO_WAIT and K_FOREVER.
1757 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001758 * @return Address of the data item if successful; NULL if returned
1759 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001761#define k_lifo_get(lifo, timeout) \
1762 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001763
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001765 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001769 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001773#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001774 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001775 __in_section(_k_queue, static, name) = \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001776 K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001777
Allan Stephensc98da842016-11-11 15:45:03 -05001778/**
1779 * @} end defgroup lifo_apis
1780 */
1781
1782/**
1783 * @cond INTERNAL_HIDDEN
1784 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001785
1786struct k_stack {
1787 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001788 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001789
Anas Nashif2f203c22016-12-18 06:57:45 -05001790 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001791};
1792
Allan Stephensc98da842016-11-11 15:45:03 -05001793#define K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
1794 { \
1795 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1796 .base = stack_buffer, \
1797 .next = stack_buffer, \
1798 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001799 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001800 }
1801
1802/**
1803 * INTERNAL_HIDDEN @endcond
1804 */
1805
1806/**
1807 * @defgroup stack_apis Stack APIs
1808 * @ingroup kernel_apis
1809 * @{
1810 */
1811
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812/**
1813 * @brief Initialize a stack.
1814 *
1815 * This routine initializes a stack object, prior to its first use.
1816 *
1817 * @param stack Address of the stack.
1818 * @param buffer Address of array used to hold stacked values.
1819 * @param num_entries Maximum number of values that can be stacked.
1820 *
1821 * @return N/A
1822 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001823extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001824 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825
1826/**
1827 * @brief Push an element onto a stack.
1828 *
1829 * This routine adds a 32-bit value @a data to @a stack.
1830 *
1831 * @note Can be called by ISRs.
1832 *
1833 * @param stack Address of the stack.
1834 * @param data Value to push onto the stack.
1835 *
1836 * @return N/A
1837 */
Kumar Galacc334c72017-04-21 10:55:34 -05001838extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001839
1840/**
1841 * @brief Pop an element from a stack.
1842 *
1843 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1844 * manner and stores the value in @a data.
1845 *
1846 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1847 *
1848 * @param stack Address of the stack.
1849 * @param data Address of area to hold the value popped from the stack.
1850 * @param timeout Waiting period to obtain a value (in milliseconds),
1851 * or one of the special values K_NO_WAIT and K_FOREVER.
1852 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001853 * @retval 0 Element popped from stack.
1854 * @retval -EBUSY Returned without waiting.
1855 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 */
Kumar Galacc334c72017-04-21 10:55:34 -05001857extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001858
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001864 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001866 * @param name Name of the stack.
1867 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001868 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001869#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001870 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001871 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001872 struct k_stack name \
1873 __in_section(_k_stack, static, name) = \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001874 K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
1875 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001876
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877/**
Allan Stephensc98da842016-11-11 15:45:03 -05001878 * @} end defgroup stack_apis
1879 */
1880
Allan Stephens6bba9b02016-11-16 14:56:54 -05001881struct k_work;
1882
Allan Stephensc98da842016-11-11 15:45:03 -05001883/**
1884 * @defgroup workqueue_apis Workqueue Thread APIs
1885 * @ingroup kernel_apis
1886 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887 */
1888
Allan Stephens6bba9b02016-11-16 14:56:54 -05001889/**
1890 * @typedef k_work_handler_t
1891 * @brief Work item handler function type.
1892 *
1893 * A work item's handler function is executed by a workqueue's thread
1894 * when the work item is processed by the workqueue.
1895 *
1896 * @param work Address of the work item.
1897 *
1898 * @return N/A
1899 */
1900typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001901
1902/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001903 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001904 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05001905
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001906struct k_work_q {
1907 struct k_fifo fifo;
Andrew Boied26cf2d2017-03-30 13:07:02 -07001908 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001909};
1910
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001911enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03001912 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913};
1914
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001915struct k_work {
1916 void *_reserved; /* Used by k_fifo implementation. */
1917 k_work_handler_t handler;
1918 atomic_t flags[1];
1919};
1920
Allan Stephens6bba9b02016-11-16 14:56:54 -05001921struct k_delayed_work {
1922 struct k_work work;
1923 struct _timeout timeout;
1924 struct k_work_q *work_q;
1925};
1926
1927extern struct k_work_q k_sys_work_q;
1928
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001930 * INTERNAL_HIDDEN @endcond
1931 */
1932
1933/**
1934 * @brief Initialize a statically-defined work item.
1935 *
1936 * This macro can be used to initialize a statically-defined workqueue work
1937 * item, prior to its first use. For example,
1938 *
1939 * @code struct k_work <work> = K_WORK_INITIALIZER(<work_handler>); @endcode
1940 *
1941 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001942 */
1943#define K_WORK_INITIALIZER(work_handler) \
1944 { \
1945 ._reserved = NULL, \
1946 .handler = work_handler, \
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001947 .flags = { 0 } \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001948 }
1949
1950/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001951 * @brief Initialize a work item.
1952 *
1953 * This routine initializes a workqueue work item, prior to its first use.
1954 *
1955 * @param work Address of work item.
1956 * @param handler Function to invoke each time work item is processed.
1957 *
1958 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959 */
1960static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
1961{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03001962 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001963 work->handler = handler;
1964}
1965
1966/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001967 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001968 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001969 * This routine submits work item @a work to be processed by workqueue
1970 * @a work_q. If the work item is already pending in the workqueue's queue
1971 * as a result of an earlier submission, this routine has no effect on the
1972 * work item. If the work item has already been processed, or is currently
1973 * being processed, its work is considered complete and the work item can be
1974 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001975 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001976 * @warning
1977 * A submitted work item must not be modified until it has been processed
1978 * by the workqueue.
1979 *
1980 * @note Can be called by ISRs.
1981 *
1982 * @param work_q Address of workqueue.
1983 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001984 *
1985 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001986 */
1987static inline void k_work_submit_to_queue(struct k_work_q *work_q,
1988 struct k_work *work)
1989{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03001990 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001991 k_fifo_put(&work_q->fifo, work);
1992 }
1993}
1994
1995/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05001996 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001997 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05001998 * This routine indicates if work item @a work is pending in a workqueue's
1999 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002001 * @note Can be called by ISRs.
2002 *
2003 * @param work Address of work item.
2004 *
2005 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002006 */
2007static inline int k_work_pending(struct k_work *work)
2008{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002009 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002010}
2011
2012/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002013 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002015 * This routine starts workqueue @a work_q. The workqueue spawns its work
2016 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002018 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002019 * @param stack Pointer to work queue thread's stack space, as defined by
2020 * K_THREAD_STACK_DEFINE()
2021 * @param stack_size Size of the work queue thread's stack (in bytes), which
2022 * should either be the same constant passed to
2023 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002024 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 *
2026 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002027 */
Allan Stephens904cf972016-10-07 13:59:23 -05002028extern void k_work_q_start(struct k_work_q *work_q, char *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002029 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002032 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002034 * This routine initializes a workqueue delayed work item, prior to
2035 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002037 * @param work Address of delayed work item.
2038 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
2040 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002041 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002042extern void k_delayed_work_init(struct k_delayed_work *work,
2043 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044
2045/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002046 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002048 * This routine schedules work item @a work to be processed by workqueue
2049 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002050 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002051 * Only when the countdown completes is the work item actually submitted to
2052 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002054 * Submitting a previously submitted delayed work item that is still
2055 * counting down cancels the existing submission and restarts the countdown
2056 * using the new delay. If the work item is currently pending on the
2057 * workqueue's queue because the countdown has completed it is too late to
2058 * resubmit the item, and resubmission fails without impacting the work item.
2059 * If the work item has already been processed, or is currently being processed,
2060 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002062 * @warning
2063 * A delayed work item must not be modified until it has been processed
2064 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002065 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002066 * @note Can be called by ISRs.
2067 *
2068 * @param work_q Address of workqueue.
2069 * @param work Address of delayed work item.
2070 * @param delay Delay before submitting the work item (in milliseconds).
2071 *
2072 * @retval 0 Work item countdown started.
2073 * @retval -EINPROGRESS Work item is already pending.
2074 * @retval -EINVAL Work item is being processed or has completed its work.
2075 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002076 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002077extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2078 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002079 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002080
2081/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002082 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002083 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002084 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002085 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002086 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002088 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002089 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002090 * @param work Address of delayed work item.
2091 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002092 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002093 * @retval -EINPROGRESS Work item is already pending.
2094 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002095 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002096extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002097
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002099 * @brief Submit a work item to the system workqueue.
2100 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002101 * This routine submits work item @a work to be processed by the system
2102 * workqueue. If the work item is already pending in the workqueue's queue
2103 * as a result of an earlier submission, this routine has no effect on the
2104 * work item. If the work item has already been processed, or is currently
2105 * being processed, its work is considered complete and the work item can be
2106 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002107 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002108 * @warning
2109 * Work items submitted to the system workqueue should avoid using handlers
2110 * that block or yield since this may prevent the system workqueue from
2111 * processing other work items in a timely manner.
2112 *
2113 * @note Can be called by ISRs.
2114 *
2115 * @param work Address of work item.
2116 *
2117 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118 */
2119static inline void k_work_submit(struct k_work *work)
2120{
2121 k_work_submit_to_queue(&k_sys_work_q, work);
2122}
2123
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125 * @brief Submit a delayed work item to the system workqueue.
2126 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127 * This routine schedules work item @a work to be processed by the system
2128 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002129 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002130 * Only when the countdown completes is the work item actually submitted to
2131 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002133 * Submitting a previously submitted delayed work item that is still
2134 * counting down cancels the existing submission and restarts the countdown
2135 * using the new delay. If the work item is currently pending on the
2136 * workqueue's queue because the countdown has completed it is too late to
2137 * resubmit the item, and resubmission fails without impacting the work item.
2138 * If the work item has already been processed, or is currently being processed,
2139 * its work is considered complete and the work item can be resubmitted.
2140 *
2141 * @warning
2142 * Work items submitted to the system workqueue should avoid using handlers
2143 * that block or yield since this may prevent the system workqueue from
2144 * processing other work items in a timely manner.
2145 *
2146 * @note Can be called by ISRs.
2147 *
2148 * @param work Address of delayed work item.
2149 * @param delay Delay before submitting the work item (in milliseconds).
2150 *
2151 * @retval 0 Work item countdown started.
2152 * @retval -EINPROGRESS Work item is already pending.
2153 * @retval -EINVAL Work item is being processed or has completed its work.
2154 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155 */
2156static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002157 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002159 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160}
2161
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002163 * @brief Get time remaining before a delayed work gets scheduled.
2164 *
2165 * This routine computes the (approximate) time remaining before a
2166 * delayed work gets executed. If the delayed work is not waiting to be
2167 * schedules, it returns zero.
2168 *
2169 * @param work Delayed work item.
2170 *
2171 * @return Remaining time (in milliseconds).
2172 */
Kumar Galacc334c72017-04-21 10:55:34 -05002173static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002174{
2175 return _timeout_remaining_get(&work->timeout);
2176}
2177
2178/**
Allan Stephensc98da842016-11-11 15:45:03 -05002179 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002180 */
2181
Allan Stephensc98da842016-11-11 15:45:03 -05002182/**
2183 * @cond INTERNAL_HIDDEN
2184 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185
2186struct k_mutex {
2187 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002188 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002189 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191
Anas Nashif2f203c22016-12-18 06:57:45 -05002192 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193};
2194
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195#define K_MUTEX_INITIALIZER(obj) \
2196 { \
2197 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2198 .owner = NULL, \
2199 .lock_count = 0, \
2200 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002201 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202 }
2203
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002204/**
Allan Stephensc98da842016-11-11 15:45:03 -05002205 * INTERNAL_HIDDEN @endcond
2206 */
2207
2208/**
2209 * @defgroup mutex_apis Mutex APIs
2210 * @ingroup kernel_apis
2211 * @{
2212 */
2213
2214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002215 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002217 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002219 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002221 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002224 struct k_mutex name \
2225 __in_section(_k_mutex, static, name) = \
2226 K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002229 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * Upon completion, the mutex is available and does not have an owner.
2234 *
2235 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
2237 * @return N/A
2238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240
2241/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002242 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002244 * This routine locks @a mutex. If the mutex is locked by another thread,
2245 * the calling thread waits until the mutex becomes available or until
2246 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002248 * A thread is permitted to lock a mutex it has already locked. The operation
2249 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002251 * @param mutex Address of the mutex.
2252 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253 * or one of the special values K_NO_WAIT and K_FOREVER.
2254 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002255 * @retval 0 Mutex locked.
2256 * @retval -EBUSY Returned without waiting.
2257 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 */
Kumar Galacc334c72017-04-21 10:55:34 -05002259extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260
2261/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * This routine unlocks @a mutex. The mutex must already be locked by the
2265 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266 *
2267 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 * thread.
2270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002271 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
2273 * @return N/A
2274 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275extern void k_mutex_unlock(struct k_mutex *mutex);
2276
Allan Stephensc98da842016-11-11 15:45:03 -05002277/**
2278 * @} end defgroup mutex_apis
2279 */
2280
2281/**
2282 * @cond INTERNAL_HIDDEN
2283 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284
2285struct k_sem {
2286 _wait_q_t wait_q;
2287 unsigned int count;
2288 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002289 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290
Anas Nashif2f203c22016-12-18 06:57:45 -05002291 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292};
2293
Allan Stephensc98da842016-11-11 15:45:03 -05002294#define K_SEM_INITIALIZER(obj, initial_count, count_limit) \
2295 { \
2296 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2297 .count = initial_count, \
2298 .limit = count_limit, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05002299 _POLL_EVENT_OBJ_INIT \
Anas Nashif2f203c22016-12-18 06:57:45 -05002300 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002301 }
2302
2303/**
2304 * INTERNAL_HIDDEN @endcond
2305 */
2306
2307/**
2308 * @defgroup semaphore_apis Semaphore APIs
2309 * @ingroup kernel_apis
2310 * @{
2311 */
2312
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002313/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002314 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002316 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * @param sem Address of the semaphore.
2319 * @param initial_count Initial semaphore count.
2320 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002321 *
2322 * @return N/A
2323 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2325 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002326
2327/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002328 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2333 *
2334 * @param sem Address of the semaphore.
2335 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002336 * or one of the special values K_NO_WAIT and K_FOREVER.
2337 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002338 * @note When porting code from the nanokernel legacy API to the new API, be
2339 * careful with the return value of this function. The return value is the
2340 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2341 * non-zero means failure, while the nano_sem_take family returns 1 for success
2342 * and 0 for failure.
2343 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002344 * @retval 0 Semaphore taken.
2345 * @retval -EBUSY Returned without waiting.
2346 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002347 */
Kumar Galacc334c72017-04-21 10:55:34 -05002348extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002349
2350/**
2351 * @brief Give a semaphore.
2352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * This routine gives @a sem, unless the semaphore is already at its maximum
2354 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002359 *
2360 * @return N/A
2361 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002362extern void k_sem_give(struct k_sem *sem);
2363
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002364/**
2365 * @brief Reset a semaphore's count to zero.
2366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002367 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002370 *
2371 * @return N/A
2372 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002373static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374{
2375 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376}
2377
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002378/**
2379 * @brief Get a semaphore's count.
2380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002386 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002387static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388{
2389 return sem->count;
2390}
2391
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002396 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002397 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002399 * @param name Name of the semaphore.
2400 * @param initial_count Initial semaphore count.
2401 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002402 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002403#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002404 struct k_sem name \
2405 __in_section(_k_sem, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406 K_SEM_INITIALIZER(name, initial_count, count_limit)
2407
Allan Stephensc98da842016-11-11 15:45:03 -05002408/**
2409 * @} end defgroup semaphore_apis
2410 */
2411
2412/**
2413 * @defgroup alert_apis Alert APIs
2414 * @ingroup kernel_apis
2415 * @{
2416 */
2417
Allan Stephens5eceb852016-11-16 10:16:30 -05002418/**
2419 * @typedef k_alert_handler_t
2420 * @brief Alert handler function type.
2421 *
2422 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002423 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002424 * and is only invoked if the alert has been initialized with one.
2425 *
2426 * @param alert Address of the alert.
2427 *
2428 * @return 0 if alert has been consumed; non-zero if alert should pend.
2429 */
2430typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002431
2432/**
2433 * @} end defgroup alert_apis
2434 */
2435
2436/**
2437 * @cond INTERNAL_HIDDEN
2438 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002440#define K_ALERT_DEFAULT NULL
2441#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002443struct k_alert {
2444 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445 atomic_t send_count;
2446 struct k_work work_item;
2447 struct k_sem sem;
2448
Anas Nashif2f203c22016-12-18 06:57:45 -05002449 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450};
2451
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002452extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002454#define K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002456 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457 .send_count = ATOMIC_INIT(0), \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002458 .work_item = K_WORK_INITIALIZER(_alert_deliver), \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002459 .sem = K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002460 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461 }
2462
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463/**
Allan Stephensc98da842016-11-11 15:45:03 -05002464 * INTERNAL_HIDDEN @endcond
2465 */
2466
2467/**
2468 * @addtogroup alert_apis
2469 * @{
2470 */
2471
2472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002475 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002476 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002477 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * @param name Name of the alert.
2480 * @param alert_handler Action to take when alert is sent. Specify either
2481 * the address of a function to be invoked by the system workqueue
2482 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2483 * K_ALERT_DEFAULT (which causes the alert to pend).
2484 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002486#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002487 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002488 __in_section(_k_alert, static, name) = \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002489 K_ALERT_INITIALIZER(name, alert_handler, \
2490 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @param alert Address of the alert.
2498 * @param handler Action to take when alert is sent. Specify either the address
2499 * of a function to be invoked by the system workqueue thread,
2500 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2501 * K_ALERT_DEFAULT (which causes the alert to pend).
2502 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 *
2504 * @return N/A
2505 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002506extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2507 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508
2509/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2515 *
2516 * @param alert Address of the alert.
2517 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002518 * or one of the special values K_NO_WAIT and K_FOREVER.
2519 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002520 * @retval 0 Alert received.
2521 * @retval -EBUSY Returned without waiting.
2522 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002523 */
Kumar Galacc334c72017-04-21 10:55:34 -05002524extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525
2526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * This routine signals @a alert. The action specified for @a alert will
2530 * be taken, which may trigger the execution of an alert handler function
2531 * and/or cause the alert to pend (assuming the alert has not reached its
2532 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @note Can be called by ISRs.
2535 *
2536 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537 *
2538 * @return N/A
2539 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002540extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541
2542/**
Allan Stephensc98da842016-11-11 15:45:03 -05002543 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544 */
2545
Allan Stephensc98da842016-11-11 15:45:03 -05002546/**
2547 * @cond INTERNAL_HIDDEN
2548 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549
2550struct k_msgq {
2551 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002552 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002553 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554 char *buffer_start;
2555 char *buffer_end;
2556 char *read_ptr;
2557 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002558 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559
Anas Nashif2f203c22016-12-18 06:57:45 -05002560 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561};
2562
Peter Mitsis1da807e2016-10-06 11:36:59 -04002563#define K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564 { \
2565 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002566 .max_msgs = q_max_msgs, \
2567 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002569 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570 .read_ptr = q_buffer, \
2571 .write_ptr = q_buffer, \
2572 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002573 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002574 }
2575
Peter Mitsis1da807e2016-10-06 11:36:59 -04002576/**
Allan Stephensc98da842016-11-11 15:45:03 -05002577 * INTERNAL_HIDDEN @endcond
2578 */
2579
2580/**
2581 * @defgroup msgq_apis Message Queue APIs
2582 * @ingroup kernel_apis
2583 * @{
2584 */
2585
2586/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2590 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002591 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2592 * message is similarly aligned to this boundary, @a q_msg_size must also be
2593 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002595 * The message queue can be accessed outside the module where it is defined
2596 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002597 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002598 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002600 * @param q_name Name of the message queue.
2601 * @param q_msg_size Message size (in bytes).
2602 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002603 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002604 */
2605#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2606 static char __noinit __aligned(q_align) \
2607 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002608 struct k_msgq q_name \
2609 __in_section(_k_msgq, static, q_name) = \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002610 K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
2611 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612
Peter Mitsisd7a37502016-10-13 11:37:40 -04002613/**
2614 * @brief Initialize a message queue.
2615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * This routine initializes a message queue object, prior to its first use.
2617 *
Allan Stephensda827222016-11-09 14:23:58 -06002618 * The message queue's ring buffer must contain space for @a max_msgs messages,
2619 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2620 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2621 * that each message is similarly aligned to this boundary, @a q_msg_size
2622 * must also be a multiple of N.
2623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * @param q Address of the message queue.
2625 * @param buffer Pointer to ring buffer that holds queued messages.
2626 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002627 * @param max_msgs Maximum number of messages that can be queued.
2628 *
2629 * @return N/A
2630 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002631extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002632 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002633
2634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002638 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002639 * @note Can be called by ISRs.
2640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 * @param q Address of the message queue.
2642 * @param data Pointer to the message.
2643 * @param timeout Waiting period to add the message (in milliseconds),
2644 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002645 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002646 * @retval 0 Message sent.
2647 * @retval -ENOMSG Returned without waiting or queue purged.
2648 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002649 */
Kumar Galacc334c72017-04-21 10:55:34 -05002650extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002651
2652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002653 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002655 * This routine receives a message from message queue @a q in a "first in,
2656 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002657 *
Allan Stephensc98da842016-11-11 15:45:03 -05002658 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @param q Address of the message queue.
2661 * @param data Address of area to hold the received message.
2662 * @param timeout Waiting period to receive the message (in milliseconds),
2663 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002664 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002665 * @retval 0 Message received.
2666 * @retval -ENOMSG Returned without waiting.
2667 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002668 */
Kumar Galacc334c72017-04-21 10:55:34 -05002669extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002670
2671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * This routine discards all unreceived messages in a message queue's ring
2675 * buffer. Any threads that are blocked waiting to send a message to the
2676 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002679 *
2680 * @return N/A
2681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682extern void k_msgq_purge(struct k_msgq *q);
2683
Peter Mitsis67be2492016-10-07 11:44:34 -04002684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * This routine returns the number of unused entries in a message queue's
2688 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * @param q Address of the message queue.
2691 *
2692 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002693 */
Kumar Galacc334c72017-04-21 10:55:34 -05002694static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002695{
2696 return q->max_msgs - q->used_msgs;
2697}
2698
Peter Mitsisd7a37502016-10-13 11:37:40 -04002699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @param q Address of the message queue.
2705 *
2706 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002707 */
Kumar Galacc334c72017-04-21 10:55:34 -05002708static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709{
2710 return q->used_msgs;
2711}
2712
Allan Stephensc98da842016-11-11 15:45:03 -05002713/**
2714 * @} end defgroup msgq_apis
2715 */
2716
2717/**
2718 * @defgroup mem_pool_apis Memory Pool APIs
2719 * @ingroup kernel_apis
2720 * @{
2721 */
2722
Andy Ross73cb9582017-05-09 10:42:39 -07002723/* Note on sizing: the use of a 20 bit field for block means that,
2724 * assuming a reasonable minimum block size of 16 bytes, we're limited
2725 * to 16M of memory managed by a single pool. Long term it would be
2726 * good to move to a variable bit size based on configuration.
2727 */
2728struct k_mem_block_id {
2729 u32_t pool : 8;
2730 u32_t level : 4;
2731 u32_t block : 20;
2732};
2733
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002736 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737};
2738
Allan Stephensc98da842016-11-11 15:45:03 -05002739/**
2740 * @} end defgroup mem_pool_apis
2741 */
2742
2743/**
2744 * @defgroup mailbox_apis Mailbox APIs
2745 * @ingroup kernel_apis
2746 * @{
2747 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748
2749struct k_mbox_msg {
2750 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002751 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002753 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002755 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756 /** sender's message data buffer */
2757 void *tx_data;
2758 /** internal use only - needed for legacy API support */
2759 void *_rx_data;
2760 /** message data block descriptor */
2761 struct k_mem_block tx_block;
2762 /** source thread id */
2763 k_tid_t rx_source_thread;
2764 /** target thread id */
2765 k_tid_t tx_target_thread;
2766 /** internal use only - thread waiting on send (may be a dummy) */
2767 k_tid_t _syncing_thread;
2768#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2769 /** internal use only - semaphore used during asynchronous send */
2770 struct k_sem *_async_sem;
2771#endif
2772};
2773
Allan Stephensc98da842016-11-11 15:45:03 -05002774/**
2775 * @cond INTERNAL_HIDDEN
2776 */
2777
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778struct k_mbox {
2779 _wait_q_t tx_msg_queue;
2780 _wait_q_t rx_msg_queue;
2781
Anas Nashif2f203c22016-12-18 06:57:45 -05002782 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783};
2784
2785#define K_MBOX_INITIALIZER(obj) \
2786 { \
2787 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2788 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002789 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 }
2791
Peter Mitsis12092702016-10-14 12:57:23 -04002792/**
Allan Stephensc98da842016-11-11 15:45:03 -05002793 * INTERNAL_HIDDEN @endcond
2794 */
2795
2796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002797 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002799 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002800 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002801 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002804 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002806 struct k_mbox name \
2807 __in_section(_k_mbox, static, name) = \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002808 K_MBOX_INITIALIZER(name) \
2809
Peter Mitsis12092702016-10-14 12:57:23 -04002810/**
2811 * @brief Initialize a mailbox.
2812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * This routine initializes a mailbox object, prior to its first use.
2814 *
2815 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002816 *
2817 * @return N/A
2818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819extern void k_mbox_init(struct k_mbox *mbox);
2820
Peter Mitsis12092702016-10-14 12:57:23 -04002821/**
2822 * @brief Send a mailbox message in a synchronous manner.
2823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * This routine sends a message to @a mbox and waits for a receiver to both
2825 * receive and process it. The message data may be in a buffer, in a memory
2826 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * @param mbox Address of the mailbox.
2829 * @param tx_msg Address of the transmit message descriptor.
2830 * @param timeout Waiting period for the message to be received (in
2831 * milliseconds), or one of the special values K_NO_WAIT
2832 * and K_FOREVER. Once the message has been received,
2833 * this routine waits as long as necessary for the message
2834 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002835 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002836 * @retval 0 Message sent.
2837 * @retval -ENOMSG Returned without waiting.
2838 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002839 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002840extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002841 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002842
Peter Mitsis12092702016-10-14 12:57:23 -04002843/**
2844 * @brief Send a mailbox message in an asynchronous manner.
2845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002846 * This routine sends a message to @a mbox without waiting for a receiver
2847 * to process it. The message data may be in a buffer, in a memory pool block,
2848 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2849 * will be given when the message has been both received and completely
2850 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @param mbox Address of the mailbox.
2853 * @param tx_msg Address of the transmit message descriptor.
2854 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04002855 *
2856 * @return N/A
2857 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002858extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859 struct k_sem *sem);
2860
Peter Mitsis12092702016-10-14 12:57:23 -04002861/**
2862 * @brief Receive a mailbox message.
2863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * This routine receives a message from @a mbox, then optionally retrieves
2865 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @param mbox Address of the mailbox.
2868 * @param rx_msg Address of the receive message descriptor.
2869 * @param buffer Address of the buffer to receive data, or NULL to defer data
2870 * retrieval and message disposal until later.
2871 * @param timeout Waiting period for a message to be received (in
2872 * milliseconds), or one of the special values K_NO_WAIT
2873 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002874 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002875 * @retval 0 Message received.
2876 * @retval -ENOMSG Returned without waiting.
2877 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002878 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002879extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002880 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002881
2882/**
2883 * @brief Retrieve mailbox message data into a buffer.
2884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * This routine completes the processing of a received message by retrieving
2886 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04002887 *
2888 * Alternatively, this routine can be used to dispose of a received message
2889 * without retrieving its data.
2890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @param rx_msg Address of the receive message descriptor.
2892 * @param buffer Address of the buffer to receive data, or NULL to discard
2893 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04002894 *
2895 * @return N/A
2896 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002897extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04002898
2899/**
2900 * @brief Retrieve mailbox message data into a memory pool block.
2901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * This routine completes the processing of a received message by retrieving
2903 * its data into a memory pool block, then disposing of the message.
2904 * The memory pool block that results from successful retrieval must be
2905 * returned to the pool once the data has been processed, even in cases
2906 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04002907 *
2908 * Alternatively, this routine can be used to dispose of a received message
2909 * without retrieving its data. In this case there is no need to return a
2910 * memory pool block to the pool.
2911 *
2912 * This routine allocates a new memory pool block for the data only if the
2913 * data is not already in one. If a new block cannot be allocated, the routine
2914 * returns a failure code and the received message is left unchanged. This
2915 * permits the caller to reattempt data retrieval at a later time or to dispose
2916 * of the received message without retrieving its data.
2917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @param rx_msg Address of a receive message descriptor.
2919 * @param pool Address of memory pool, or NULL to discard data.
2920 * @param block Address of the area to hold memory pool block info.
2921 * @param timeout Waiting period to wait for a memory pool block (in
2922 * milliseconds), or one of the special values K_NO_WAIT
2923 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04002924 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002925 * @retval 0 Data retrieved.
2926 * @retval -ENOMEM Returned without waiting.
2927 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002928 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002929extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04002930 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05002931 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932
Allan Stephensc98da842016-11-11 15:45:03 -05002933/**
2934 * @} end defgroup mailbox_apis
2935 */
2936
2937/**
2938 * @cond INTERNAL_HIDDEN
2939 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002940
2941struct k_pipe {
2942 unsigned char *buffer; /* Pipe buffer: may be NULL */
2943 size_t size; /* Buffer size */
2944 size_t bytes_used; /* # bytes used in buffer */
2945 size_t read_index; /* Where in buffer to read from */
2946 size_t write_index; /* Where in buffer to write */
2947
2948 struct {
2949 _wait_q_t readers; /* Reader wait queue */
2950 _wait_q_t writers; /* Writer wait queue */
2951 } wait_q;
2952
Anas Nashif2f203c22016-12-18 06:57:45 -05002953 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002954};
2955
Peter Mitsise5d9c582016-10-14 14:44:57 -04002956#define K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 { \
2958 .buffer = pipe_buffer, \
2959 .size = pipe_buffer_size, \
2960 .bytes_used = 0, \
2961 .read_index = 0, \
2962 .write_index = 0, \
2963 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
2964 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002965 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966 }
2967
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002968/**
Allan Stephensc98da842016-11-11 15:45:03 -05002969 * INTERNAL_HIDDEN @endcond
2970 */
2971
2972/**
2973 * @defgroup pipe_apis Pipe APIs
2974 * @ingroup kernel_apis
2975 * @{
2976 */
2977
2978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002983 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * @param name Name of the pipe.
2986 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
2987 * or zero if no ring buffer is used.
2988 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002989 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04002990#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
2991 static unsigned char __noinit __aligned(pipe_align) \
2992 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002993 struct k_pipe name \
2994 __in_section(_k_pipe, static, name) = \
Peter Mitsise5d9c582016-10-14 14:44:57 -04002995 K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003000 * This routine initializes a pipe object, prior to its first use.
3001 *
3002 * @param pipe Address of the pipe.
3003 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3004 * is used.
3005 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3006 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007 *
3008 * @return N/A
3009 */
3010extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3011 size_t size);
3012
3013/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @param pipe Address of the pipe.
3019 * @param data Address of data to write.
3020 * @param bytes_to_write Size of data (in bytes).
3021 * @param bytes_written Address of area to hold the number of bytes written.
3022 * @param min_xfer Minimum number of bytes to write.
3023 * @param timeout Waiting period to wait for the data to be written (in
3024 * milliseconds), or one of the special values K_NO_WAIT
3025 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003027 * @retval 0 At least @a min_xfer bytes of data were written.
3028 * @retval -EIO Returned without waiting; zero data bytes were written.
3029 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003031 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003032extern int k_pipe_put(struct k_pipe *pipe, void *data,
3033 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003034 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003035
3036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003041 * @param pipe Address of the pipe.
3042 * @param data Address to place the data read from pipe.
3043 * @param bytes_to_read Maximum number of data bytes to read.
3044 * @param bytes_read Address of area to hold the number of bytes read.
3045 * @param min_xfer Minimum number of data bytes to read.
3046 * @param timeout Waiting period to wait for the data to be read (in
3047 * milliseconds), or one of the special values K_NO_WAIT
3048 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003050 * @retval 0 At least @a min_xfer bytes of data were read.
3051 * @retval -EIO Returned without waiting; zero data bytes were read.
3052 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003055extern int k_pipe_get(struct k_pipe *pipe, void *data,
3056 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003057 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058
3059/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * This routine writes the data contained in a memory block to @a pipe.
3063 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003066 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067 * @param block Memory block containing data to send
3068 * @param size Number of data bytes in memory block to send
3069 * @param sem Semaphore to signal upon completion (else NULL)
3070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072 */
3073extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3074 size_t size, struct k_sem *sem);
3075
3076/**
Allan Stephensc98da842016-11-11 15:45:03 -05003077 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078 */
3079
Allan Stephensc98da842016-11-11 15:45:03 -05003080/**
3081 * @cond INTERNAL_HIDDEN
3082 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003084struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003086 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003087 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088 char *buffer;
3089 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003090 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091
Anas Nashif2f203c22016-12-18 06:57:45 -05003092 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093};
3094
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003095#define K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
3096 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 { \
3098 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003099 .num_blocks = slab_num_blocks, \
3100 .block_size = slab_block_size, \
3101 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 .free_list = NULL, \
3103 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003104 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105 }
3106
Peter Mitsis578f9112016-10-07 13:50:31 -04003107/**
Allan Stephensc98da842016-11-11 15:45:03 -05003108 * INTERNAL_HIDDEN @endcond
3109 */
3110
3111/**
3112 * @defgroup mem_slab_apis Memory Slab APIs
3113 * @ingroup kernel_apis
3114 * @{
3115 */
3116
3117/**
Allan Stephensda827222016-11-09 14:23:58 -06003118 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003119 *
Allan Stephensda827222016-11-09 14:23:58 -06003120 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003122 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3123 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003124 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003125 *
Allan Stephensda827222016-11-09 14:23:58 -06003126 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003128 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003129 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * @param name Name of the memory slab.
3132 * @param slab_block_size Size of each memory block (in bytes).
3133 * @param slab_num_blocks Number memory blocks.
3134 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003135 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003136#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3137 char __noinit __aligned(slab_align) \
3138 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3139 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003140 __in_section(_k_mem_slab, static, name) = \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003141 K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
3142 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003143
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003144/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003145 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003148 *
Allan Stephensda827222016-11-09 14:23:58 -06003149 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3150 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3151 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3152 * To ensure that each memory block is similarly aligned to this boundary,
3153 * @a slab_block_size must also be a multiple of N.
3154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @param slab Address of the memory slab.
3156 * @param buffer Pointer to buffer used for the memory blocks.
3157 * @param block_size Size of each memory block (in bytes).
3158 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003159 *
3160 * @return N/A
3161 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003162extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003163 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003164
3165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003166 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003168 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003170 * @param slab Address of the memory slab.
3171 * @param mem Pointer to block address area.
3172 * @param timeout Maximum time to wait for operation to complete
3173 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3174 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003175 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003176 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003178 * @retval -ENOMEM Returned without waiting.
3179 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003180 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003181extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003182 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003183
3184/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003185 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003187 * This routine releases a previously allocated memory block back to its
3188 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * @param slab Address of the memory slab.
3191 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003192 *
3193 * @return N/A
3194 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003195extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * This routine gets the number of memory blocks that are currently
3201 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003206 */
Kumar Galacc334c72017-04-21 10:55:34 -05003207static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003209 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210}
3211
Peter Mitsisc001aa82016-10-13 13:53:37 -04003212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * This routine gets the number of memory blocks that are currently
3216 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003221 */
Kumar Galacc334c72017-04-21 10:55:34 -05003222static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003223{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003224 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003225}
3226
Allan Stephensc98da842016-11-11 15:45:03 -05003227/**
3228 * @} end defgroup mem_slab_apis
3229 */
3230
3231/**
3232 * @cond INTERNAL_HIDDEN
3233 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234
Andy Ross73cb9582017-05-09 10:42:39 -07003235struct k_mem_pool_lvl {
3236 union {
3237 u32_t *bits_p;
3238 u32_t bits;
3239 };
3240 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003241};
3242
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003243struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003244 void *buf;
3245 size_t max_sz;
3246 u16_t n_max;
3247 u8_t n_levels;
3248 u8_t max_inline_level;
3249 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003250 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251};
3252
Andy Ross73cb9582017-05-09 10:42:39 -07003253#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254
Andy Ross73cb9582017-05-09 10:42:39 -07003255#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3256
3257#define _MPOOL_LVLS(maxsz, minsz) \
3258 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3259 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3260 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3261 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3262 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3263 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3264 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3265 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3266 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3267 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3268 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3269 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3270 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3271 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3272 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3273 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3274
3275/* Rounds the needed bits up to integer multiples of u32_t */
3276#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3277 ((((n_max) << (2*(l))) + 31) / 32)
3278
3279/* One word gets stored free unioned with the pointer, otherwise the
3280 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003281 */
Andy Ross73cb9582017-05-09 10:42:39 -07003282#define _MPOOL_LBIT_WORDS(n_max, l) \
3283 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3284 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003285
Andy Ross73cb9582017-05-09 10:42:39 -07003286/* How many bytes for the bitfields of a single level? */
3287#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3288 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3289 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003290
Andy Ross73cb9582017-05-09 10:42:39 -07003291/* Size of the bitmap array that follows the buffer in allocated memory */
3292#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3293 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3294 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3295 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3296 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3297 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3298 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3299 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3300 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3301 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3302 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3303 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3304 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3305 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3306 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3307 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3308 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003309
3310/**
Allan Stephensc98da842016-11-11 15:45:03 -05003311 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003312 */
3313
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003314/**
Allan Stephensc98da842016-11-11 15:45:03 -05003315 * @addtogroup mem_pool_apis
3316 * @{
3317 */
3318
3319/**
3320 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003322 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3323 * long. The memory pool allows blocks to be repeatedly partitioned into
3324 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003325 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003326 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003327 * If the pool is to be accessed outside the module where it is defined, it
3328 * can be declared via
3329 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003330 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003333 * @param minsz Size of the smallest blocks in the pool (in bytes).
3334 * @param maxsz Size of the largest blocks in the pool (in bytes).
3335 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003337 */
Andy Ross73cb9582017-05-09 10:42:39 -07003338#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3339 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3340 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3341 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3342 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3343 .buf = _mpool_buf_##name, \
3344 .max_sz = maxsz, \
3345 .n_max = nmax, \
3346 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3347 .levels = _mpool_lvls_##name, \
3348 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003349
Peter Mitsis937042c2016-10-13 13:18:26 -04003350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @param pool Address of the memory pool.
3356 * @param block Pointer to block descriptor for the allocated memory.
3357 * @param size Amount of memory to allocate (in bytes).
3358 * @param timeout Maximum time to wait for operation to complete
3359 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3360 * or K_FOREVER to wait as long as necessary.
3361 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003362 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003364 * @retval -ENOMEM Returned without waiting.
3365 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003366 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003367extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003368 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003369
3370/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003373 * This routine releases a previously allocated memory block back to its
3374 * memory pool.
3375 *
3376 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003377 *
3378 * @return N/A
3379 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003380extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003381
3382/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003384 *
Andy Ross73cb9582017-05-09 10:42:39 -07003385 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 *
Andy Ross73cb9582017-05-09 10:42:39 -07003387 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003388 *
3389 * @return N/A
3390 */
Andy Ross73cb9582017-05-09 10:42:39 -07003391static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003392
3393/**
Allan Stephensc98da842016-11-11 15:45:03 -05003394 * @} end addtogroup mem_pool_apis
3395 */
3396
3397/**
3398 * @defgroup heap_apis Heap Memory Pool APIs
3399 * @ingroup kernel_apis
3400 * @{
3401 */
3402
3403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003407 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003411 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003412 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003413extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003414
3415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003417 *
3418 * This routine provides traditional free() semantics. The memory being
3419 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003420 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003421 * If @a ptr is NULL, no operation is performed.
3422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003424 *
3425 * @return N/A
3426 */
3427extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428
Allan Stephensc98da842016-11-11 15:45:03 -05003429/**
3430 * @} end defgroup heap_apis
3431 */
3432
Benjamin Walshacc68c12017-01-29 18:57:45 -05003433/* polling API - PRIVATE */
3434
Benjamin Walshb0179862017-02-02 16:39:57 -05003435#ifdef CONFIG_POLL
3436#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3437#else
3438#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3439#endif
3440
Benjamin Walshacc68c12017-01-29 18:57:45 -05003441/* private - implementation data created as needed, per-type */
3442struct _poller {
3443 struct k_thread *thread;
3444};
3445
3446/* private - types bit positions */
3447enum _poll_types_bits {
3448 /* can be used to ignore an event */
3449 _POLL_TYPE_IGNORE,
3450
3451 /* to be signaled by k_poll_signal() */
3452 _POLL_TYPE_SIGNAL,
3453
3454 /* semaphore availability */
3455 _POLL_TYPE_SEM_AVAILABLE,
3456
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003457 /* queue/fifo/lifo data availability */
3458 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003459
3460 _POLL_NUM_TYPES
3461};
3462
3463#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3464
3465/* private - states bit positions */
3466enum _poll_states_bits {
3467 /* default state when creating event */
3468 _POLL_STATE_NOT_READY,
3469
3470 /* there was another poller already on the object */
3471 _POLL_STATE_EADDRINUSE,
3472
3473 /* signaled by k_poll_signal() */
3474 _POLL_STATE_SIGNALED,
3475
3476 /* semaphore is available */
3477 _POLL_STATE_SEM_AVAILABLE,
3478
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003479 /* data is available to read on queue/fifo/lifo */
3480 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003481
3482 _POLL_NUM_STATES
3483};
3484
3485#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3486
3487#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003488 (32 - (0 \
3489 + 8 /* tag */ \
3490 + _POLL_NUM_TYPES \
3491 + _POLL_NUM_STATES \
3492 + 1 /* modes */ \
3493 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003494
3495#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3496#error overflow of 32-bit word in struct k_poll_event
3497#endif
3498
3499/* end of polling API - PRIVATE */
3500
3501
3502/**
3503 * @defgroup poll_apis Async polling APIs
3504 * @ingroup kernel_apis
3505 * @{
3506 */
3507
3508/* Public polling API */
3509
3510/* public - values for k_poll_event.type bitfield */
3511#define K_POLL_TYPE_IGNORE 0
3512#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3513#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003514#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3515#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003516
3517/* public - polling modes */
3518enum k_poll_modes {
3519 /* polling thread does not take ownership of objects when available */
3520 K_POLL_MODE_NOTIFY_ONLY = 0,
3521
3522 K_POLL_NUM_MODES
3523};
3524
3525/* public - values for k_poll_event.state bitfield */
3526#define K_POLL_STATE_NOT_READY 0
3527#define K_POLL_STATE_EADDRINUSE _POLL_STATE_BIT(_POLL_STATE_EADDRINUSE)
3528#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3529#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003530#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3531#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003532
3533/* public - poll signal object */
3534struct k_poll_signal {
3535 /* PRIVATE - DO NOT TOUCH */
3536 struct k_poll_event *poll_event;
3537
3538 /*
3539 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3540 * user resets it to 0.
3541 */
3542 unsigned int signaled;
3543
3544 /* custom result value passed to k_poll_signal() if needed */
3545 int result;
3546};
3547
3548#define K_POLL_SIGNAL_INITIALIZER() \
3549 { \
3550 .poll_event = NULL, \
3551 .signaled = 0, \
3552 .result = 0, \
3553 }
3554
3555struct k_poll_event {
3556 /* PRIVATE - DO NOT TOUCH */
3557 struct _poller *poller;
3558
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003559 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003560 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003561
Benjamin Walshacc68c12017-01-29 18:57:45 -05003562 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003563 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003564
3565 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003566 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003567
3568 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003569 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003570
3571 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003572 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003573
3574 /* per-type data */
3575 union {
3576 void *obj;
3577 struct k_poll_signal *signal;
3578 struct k_sem *sem;
3579 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003580 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003581 };
3582};
3583
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003584#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003585 { \
3586 .poller = NULL, \
3587 .type = event_type, \
3588 .state = K_POLL_STATE_NOT_READY, \
3589 .mode = event_mode, \
3590 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003591 { .obj = event_obj }, \
3592 }
3593
3594#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3595 event_tag) \
3596 { \
3597 .type = event_type, \
3598 .tag = event_tag, \
3599 .state = K_POLL_STATE_NOT_READY, \
3600 .mode = event_mode, \
3601 .unused = 0, \
3602 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003603 }
3604
3605/**
3606 * @brief Initialize one struct k_poll_event instance
3607 *
3608 * After this routine is called on a poll event, the event it ready to be
3609 * placed in an event array to be passed to k_poll().
3610 *
3611 * @param event The event to initialize.
3612 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3613 * values. Only values that apply to the same object being polled
3614 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3615 * event.
3616 * @param mode Future. Use K_POLL_MODE_INFORM_ONLY.
3617 * @param obj Kernel object or poll signal.
3618 *
3619 * @return N/A
3620 */
3621
Kumar Galacc334c72017-04-21 10:55:34 -05003622extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003623 int mode, void *obj);
3624
3625/**
3626 * @brief Wait for one or many of multiple poll events to occur
3627 *
3628 * This routine allows a thread to wait concurrently for one or many of
3629 * multiple poll events to have occurred. Such events can be a kernel object
3630 * being available, like a semaphore, or a poll signal event.
3631 *
3632 * When an event notifies that a kernel object is available, the kernel object
3633 * is not "given" to the thread calling k_poll(): it merely signals the fact
3634 * that the object was available when the k_poll() call was in effect. Also,
3635 * all threads trying to acquire an object the regular way, i.e. by pending on
3636 * the object, have precedence over the thread polling on the object. This
3637 * means that the polling thread will never get the poll event on an object
3638 * until the object becomes available and its pend queue is empty. For this
3639 * reason, the k_poll() call is more effective when the objects being polled
3640 * only have one thread, the polling thread, trying to acquire them.
3641 *
3642 * Only one thread can be polling for a particular object at a given time. If
3643 * another thread tries to poll on it, the k_poll() call returns -EADDRINUSE
3644 * and returns as soon as it has finished handling the other events. This means
3645 * that k_poll() can return -EADDRINUSE and have the state value of some events
3646 * be non-K_POLL_STATE_NOT_READY. When this condition occurs, the @a timeout
3647 * parameter is ignored.
3648 *
3649 * When k_poll() returns 0 or -EADDRINUSE, the caller should loop on all the
3650 * events that were passed to k_poll() and check the state field for the values
3651 * that were expected and take the associated actions.
3652 *
3653 * Before being reused for another call to k_poll(), the user has to reset the
3654 * state field to K_POLL_STATE_NOT_READY.
3655 *
3656 * @param events An array of pointers to events to be polled for.
3657 * @param num_events The number of events in the array.
3658 * @param timeout Waiting period for an event to be ready (in milliseconds),
3659 * or one of the special values K_NO_WAIT and K_FOREVER.
3660 *
3661 * @retval 0 One or more events are ready.
3662 * @retval -EADDRINUSE One or more objects already had a poller.
3663 * @retval -EAGAIN Waiting period timed out.
3664 */
3665
3666extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003667 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003668
3669/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003670 * @brief Initialize a poll signal object.
3671 *
3672 * Ready a poll signal object to be signaled via k_poll_signal().
3673 *
3674 * @param signal A poll signal.
3675 *
3676 * @return N/A
3677 */
3678
3679extern void k_poll_signal_init(struct k_poll_signal *signal);
3680
3681/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003682 * @brief Signal a poll signal object.
3683 *
3684 * This routine makes ready a poll signal, which is basically a poll event of
3685 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3686 * made ready to run. A @a result value can be specified.
3687 *
3688 * The poll signal contains a 'signaled' field that, when set by
3689 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3690 * be reset by the user before being passed again to k_poll() or k_poll() will
3691 * consider it being signaled, and will return immediately.
3692 *
3693 * @param signal A poll signal.
3694 * @param result The value to store in the result field of the signal.
3695 *
3696 * @retval 0 The signal was delivered successfully.
3697 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3698 */
3699
3700extern int k_poll_signal(struct k_poll_signal *signal, int result);
3701
3702/* private internal function */
3703extern int _handle_obj_poll_event(struct k_poll_event **obj_poll_event,
Kumar Galacc334c72017-04-21 10:55:34 -05003704 u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003705
3706/**
3707 * @} end defgroup poll_apis
3708 */
3709
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003710/**
3711 * @brief Make the CPU idle.
3712 *
3713 * This function makes the CPU idle until an event wakes it up.
3714 *
3715 * In a regular system, the idle thread should be the only thread responsible
3716 * for making the CPU idle and triggering any type of power management.
3717 * However, in some more constrained systems, such as a single-threaded system,
3718 * the only thread would be responsible for this if needed.
3719 *
3720 * @return N/A
3721 */
3722extern void k_cpu_idle(void);
3723
3724/**
3725 * @brief Make the CPU idle in an atomic fashion.
3726 *
3727 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3728 * must be done atomically before making the CPU idle.
3729 *
3730 * @param key Interrupt locking key obtained from irq_lock().
3731 *
3732 * @return N/A
3733 */
3734extern void k_cpu_atomic_idle(unsigned int key);
3735
Kumar Galacc334c72017-04-21 10:55:34 -05003736extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003737
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738#include <arch/cpu.h>
3739
Andrew Boiecdb94d62017-04-18 15:22:05 -07003740#ifdef _ARCH_EXCEPT
3741/* This archtecture has direct support for triggering a CPU exception */
3742#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3743#else
3744
3745#include <misc/printk.h>
3746
3747/* NOTE: This is the implementation for arches that do not implement
3748 * _ARCH_EXCEPT() to generate a real CPU exception.
3749 *
3750 * We won't have a real exception frame to determine the PC value when
3751 * the oops occurred, so print file and line number before we jump into
3752 * the fatal error handler.
3753 */
3754#define _k_except_reason(reason) do { \
3755 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3756 _NanoFatalErrorHandler(reason, &_default_esf); \
3757 CODE_UNREACHABLE; \
3758 } while (0)
3759
3760#endif /* _ARCH__EXCEPT */
3761
3762/**
3763 * @brief Fatally terminate a thread
3764 *
3765 * This should be called when a thread has encountered an unrecoverable
3766 * runtime condition and needs to terminate. What this ultimately
3767 * means is determined by the _fatal_error_handler() implementation, which
3768 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3769 *
3770 * If this is called from ISR context, the default system fatal error handler
3771 * will treat it as an unrecoverable system error, just like k_panic().
3772 */
3773#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3774
3775/**
3776 * @brief Fatally terminate the system
3777 *
3778 * This should be called when the Zephyr kernel has encountered an
3779 * unrecoverable runtime condition and needs to terminate. What this ultimately
3780 * means is determined by the _fatal_error_handler() implementation, which
3781 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3782 */
3783#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3784
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785/*
3786 * private APIs that are utilized by one or more public APIs
3787 */
3788
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003789#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003791#else
3792#define _init_static_threads() do { } while ((0))
3793#endif
3794
3795extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003796extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797
Andrew Boiedc5d9352017-06-02 12:56:47 -07003798/* arch/cpu.h may declare an architecture or platform-specific macro
3799 * for properly declaring stacks, compatible with MMU/MPU constraints if
3800 * enabled
3801 */
3802#ifdef _ARCH_THREAD_STACK_DEFINE
3803#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3804#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3805 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3806#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3807#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
3808#define K_THREAD_STACK_BUFFER(sym) _ARCH_THREAD_STACK_BUFFER(sym)
3809#else
3810/**
3811 * @brief Declare a toplevel thread stack memory region
3812 *
3813 * This declares a region of memory suitable for use as a thread's stack.
3814 *
3815 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3816 * 'noinit' section so that it isn't zeroed at boot
3817 *
3818 * The declared symbol will always be a character array which can be passed to
3819 * k_thread_create, but should otherwise not be manipulated.
3820 *
3821 * It is legal to precede this definition with the 'static' keyword.
3822 *
3823 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3824 * parameter of k_thread_create(), it may not be the same as the
3825 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3826 *
3827 * @param sym Thread stack symbol name
3828 * @param size Size of the stack memory region
3829 */
3830#define K_THREAD_STACK_DEFINE(sym, size) \
3831 char __noinit __aligned(STACK_ALIGN) sym[size]
3832
3833/**
3834 * @brief Declare a toplevel array of thread stack memory regions
3835 *
3836 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
3837 * definition for additional details and constraints.
3838 *
3839 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3840 * 'noinit' section so that it isn't zeroed at boot
3841 *
3842 * @param sym Thread stack symbol name
3843 * @param nmemb Number of stacks to declare
3844 * @param size Size of the stack memory region
3845 */
3846
3847#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3848 char __noinit __aligned(STACK_ALIGN) sym[nmemb][size]
3849
3850/**
3851 * @brief Declare an embedded stack memory region
3852 *
3853 * Used for stacks embedded within other data structures. Use is highly
3854 * discouraged but in some cases necessary. For memory protection scenarios,
3855 * it is very important that any RAM preceding this member not be writable
3856 * by threads else a stack overflow will lead to silent corruption. In other
3857 * words, the containing data structure should live in RAM owned by the kernel.
3858 *
3859 * @param sym Thread stack symbol name
3860 * @param size Size of the stack memory region
3861 */
3862#define K_THREAD_STACK_MEMBER(sym, size) \
3863 char __aligned(STACK_ALIGN) sym[size]
3864
3865/**
3866 * @brief Return the size in bytes of a stack memory region
3867 *
3868 * Convenience macro for passing the desired stack size to k_thread_create()
3869 * since the underlying implementation may actually create something larger
3870 * (for instance a guard area).
3871 *
3872 * The value returned here is guaranteed to match the 'size' parameter
3873 * passed to K_THREAD_STACK_DEFINE and related macros.
3874 *
3875 * @param sym Stack memory symbol
3876 * @return Size of the stack
3877 */
3878#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
3879
3880/**
3881 * @brief Get a pointer to the physical stack buffer
3882 *
3883 * Convenience macro to get at the real underlying stack buffer used by
3884 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
3885 * This is really only intended for diagnostic tools which want to examine
3886 * stack memory contents.
3887 *
3888 * @param sym Declared stack symbol name
3889 * @return The buffer itself, a char *
3890 */
3891#define K_THREAD_STACK_BUFFER(sym) sym
3892
3893#endif /* _ARCH_DECLARE_STACK */
3894
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003895#ifdef __cplusplus
3896}
3897#endif
3898
Andrew Boiee004dec2016-11-07 09:01:19 -08003899#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
3900/*
3901 * Define new and delete operators.
3902 * At this moment, the operators do nothing since objects are supposed
3903 * to be statically allocated.
3904 */
3905inline void operator delete(void *ptr)
3906{
3907 (void)ptr;
3908}
3909
3910inline void operator delete[](void *ptr)
3911{
3912 (void)ptr;
3913}
3914
3915inline void *operator new(size_t size)
3916{
3917 (void)size;
3918 return NULL;
3919}
3920
3921inline void *operator new[](size_t size)
3922{
3923 (void)size;
3924 return NULL;
3925}
3926
3927/* Placement versions of operator new and delete */
3928inline void operator delete(void *ptr1, void *ptr2)
3929{
3930 (void)ptr1;
3931 (void)ptr2;
3932}
3933
3934inline void operator delete[](void *ptr1, void *ptr2)
3935{
3936 (void)ptr1;
3937 (void)ptr2;
3938}
3939
3940inline void *operator new(size_t size, void *ptr)
3941{
3942 (void)size;
3943 return ptr;
3944}
3945
3946inline void *operator new[](size_t size, void *ptr)
3947{
3948 (void)size;
3949 return ptr;
3950}
3951
3952#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
3953
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05003954#endif /* !_ASMLANGUAGE */
3955
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003956#endif /* _kernel__h_ */