blob: aacc59a2d3f14261253235ea3ebb53bd48956b49 [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>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070031#include <syscall.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032
33#ifdef __cplusplus
34extern "C" {
35#endif
36
Anas Nashifbbb157d2017-01-15 08:46:31 -050037/**
38 * @brief Kernel APIs
39 * @defgroup kernel_apis Kernel APIs
40 * @{
41 * @}
42 */
43
Anas Nashif61f4b242016-11-18 10:53:59 -050044#ifdef CONFIG_KERNEL_DEBUG
45#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040046#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
47#else
48#define K_DEBUG(fmt, ...)
49#endif
50
Benjamin Walsh2f280412017-01-14 19:23:46 -050051#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
52#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
53#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
54#elif defined(CONFIG_COOP_ENABLED)
55#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
56#define _NUM_PREEMPT_PRIO (0)
57#elif defined(CONFIG_PREEMPT_ENABLED)
58#define _NUM_COOP_PRIO (0)
59#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
60#else
61#error "invalid configuration"
62#endif
63
64#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_PRIO_PREEMPT(x) (x)
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_ANY NULL
68#define K_END NULL
69
Benjamin Walshedb35702017-01-14 18:47:22 -050070#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050072#elif defined(CONFIG_COOP_ENABLED)
73#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
74#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050076#else
77#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#endif
79
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050080#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
82#else
83#define K_LOWEST_THREAD_PRIO -1
84#endif
85
Benjamin Walshfab8d922016-11-08 15:36:36 -050086#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
87
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
89#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
90
91typedef sys_dlist_t _wait_q_t;
92
Anas Nashif2f203c22016-12-18 06:57:45 -050093#ifdef CONFIG_OBJECT_TRACING
94#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
95#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096#else
Anas Nashif2f203c22016-12-18 06:57:45 -050097#define _OBJECT_TRACING_INIT
98#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#endif
100
Benjamin Walshacc68c12017-01-29 18:57:45 -0500101#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300102#define _POLL_EVENT_OBJ_INIT(obj) \
103 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
104#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#define _POLL_EVENT
108#endif
109
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500110struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mutex;
112struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400113struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_msgq;
115struct k_mbox;
116struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200117struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_fifo;
119struct k_lifo;
120struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400121struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mem_pool;
123struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124struct k_poll_event;
125struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126
Andrew Boie945af952017-08-22 13:15:23 -0700127enum k_objects {
128 K_OBJ_ALERT,
129 K_OBJ_DELAYED_WORK,
130 K_OBJ_MEM_SLAB,
131 K_OBJ_MSGQ,
132 K_OBJ_MUTEX,
133 K_OBJ_PIPE,
134 K_OBJ_SEM,
135 K_OBJ_STACK,
136 K_OBJ_THREAD,
137 K_OBJ_TIMER,
138 K_OBJ_WORK,
139 K_OBJ_WORK_Q,
140
141 K_OBJ_LAST
142};
143
144#ifdef CONFIG_USERSPACE
145/* Table generated by gperf, these objects are retrieved via
146 * _k_object_find() */
147struct _k_object {
148 char *name;
149 char perms[CONFIG_MAX_THREAD_BYTES];
150 char type;
151 char flags;
152} __packed;
153
154#define K_OBJ_FLAG_INITIALIZED BIT(0)
155/**
156 * Ensure a system object is a valid object of the expected type
157 *
158 * Searches for the object and ensures that it is indeed an object
159 * of the expected type, that the caller has the right permissions on it,
160 * and that the object has been initialized.
161 *
162 * This function is intended to be called on the kernel-side system
163 * call handlers to validate kernel object pointers passed in from
164 * userspace.
165 *
166 * @param obj Address of the kernel object
167 * @param otype Expected type of the kernel object
168 * @param init If true, this is for an init function and we will not error
169 * out if the object is not initialized
170 * @return 0 If the object is valid
171 * -EBADF if not a valid object of the specified type
172 * -EPERM If the caller does not have permissions
David B. Kinder8065dbc2017-09-21 15:25:40 -0700173 * -EINVAL Object is not initialized
Andrew Boie945af952017-08-22 13:15:23 -0700174 */
175int _k_object_validate(void *obj, enum k_objects otype, int init);
176
177
178/**
179 * Lookup a kernel object and init its metadata if it exists
180 *
181 * Calling this on an object will make it usable from userspace.
182 * Intended to be called as the last statement in kernel object init
183 * functions.
184 *
185 * @param object Address of the kernel object
186 */
187void _k_object_init(void *obj);
188
189
190/**
191 * grant a thread access to a kernel object
192 *
193 * The thread will be granted access to the object if the caller is from
194 * supervisor mode, or the caller is from user mode AND has permissions
195 * on the object already.
196 *
197 * @param object Address of kernel object
198 * @param thread Thread to grant access to the object
199 */
200void k_object_grant_access(void *object, struct k_thread *thread);
201
202#else
203static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
204{
205 ARG_UNUSED(obj);
206 ARG_UNUSED(otype);
207 ARG_UNUSED(init);
208
209 return 0;
210}
211
212static inline void _k_object_init(void *obj)
213{
214 ARG_UNUSED(obj);
215}
216
217static inline void k_object_grant_access(void *object, struct k_thread *thread)
218{
219 ARG_UNUSED(object);
220 ARG_UNUSED(thread);
221}
222#endif /* CONFIG_USERSPACE */
223
Andrew Boie73abd322017-04-04 13:19:13 -0700224/* timeouts */
225
226struct _timeout;
227typedef void (*_timeout_func_t)(struct _timeout *t);
228
229struct _timeout {
230 sys_dnode_t node;
231 struct k_thread *thread;
232 sys_dlist_t *wait_q;
233 s32_t delta_ticks_from_prev;
234 _timeout_func_t func;
235};
236
237extern s32_t _timeout_remaining_get(struct _timeout *timeout);
238
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700239/**
240 * @typedef k_thread_entry_t
241 * @brief Thread entry point function type.
242 *
243 * A thread's entry point function is invoked when the thread starts executing.
244 * Up to 3 argument values can be passed to the function.
245 *
246 * The thread terminates execution permanently if the entry point function
247 * returns. The thread is responsible for releasing any shared resources
248 * it may own (such as mutexes and dynamically allocated memory), prior to
249 * returning.
250 *
251 * @param p1 First argument.
252 * @param p2 Second argument.
253 * @param p3 Third argument.
254 *
255 * @return N/A
256 */
257typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700258
259#ifdef CONFIG_THREAD_MONITOR
260struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700261 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700262 void *parameter1;
263 void *parameter2;
264 void *parameter3;
265};
266#endif
267
268/* can be used for creating 'dummy' threads, e.g. for pending on objects */
269struct _thread_base {
270
271 /* this thread's entry in a ready/wait queue */
272 sys_dnode_t k_q_node;
273
274 /* user facing 'thread options'; values defined in include/kernel.h */
275 u8_t user_options;
276
277 /* thread state */
278 u8_t thread_state;
279
280 /*
281 * scheduler lock count and thread priority
282 *
283 * These two fields control the preemptibility of a thread.
284 *
285 * When the scheduler is locked, sched_locked is decremented, which
286 * means that the scheduler is locked for values from 0xff to 0x01. A
287 * thread is coop if its prio is negative, thus 0x80 to 0xff when
288 * looked at the value as unsigned.
289 *
290 * By putting them end-to-end, this means that a thread is
291 * non-preemptible if the bundled value is greater than or equal to
292 * 0x0080.
293 */
294 union {
295 struct {
296#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
297 u8_t sched_locked;
298 s8_t prio;
299#else /* LITTLE and PDP */
300 s8_t prio;
301 u8_t sched_locked;
302#endif
303 };
304 u16_t preempt;
305 };
306
307 /* data returned by APIs */
308 void *swap_data;
309
310#ifdef CONFIG_SYS_CLOCK_EXISTS
311 /* this thread's entry in a timeout queue */
312 struct _timeout timeout;
313#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700314
315#ifdef CONFIG_USERSPACE
316 /* Bit position in kernel object permissions bitfield for this thread */
317 unsigned int perm_index;
318#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700319};
320
321typedef struct _thread_base _thread_base_t;
322
323#if defined(CONFIG_THREAD_STACK_INFO)
324/* Contains the stack information of a thread */
325struct _thread_stack_info {
326 /* Stack Start */
327 u32_t start;
328 /* Stack Size */
329 u32_t size;
330};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700331
332typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700333#endif /* CONFIG_THREAD_STACK_INFO */
334
335struct k_thread {
336
337 struct _thread_base base;
338
339 /* defined by the architecture, but all archs need these */
340 struct _caller_saved caller_saved;
341 struct _callee_saved callee_saved;
342
343 /* static thread init data */
344 void *init_data;
345
346 /* abort function */
347 void (*fn_abort)(void);
348
349#if defined(CONFIG_THREAD_MONITOR)
350 /* thread entry and parameters description */
351 struct __thread_entry *entry;
352
353 /* next item in list of all threads */
354 struct k_thread *next_thread;
355#endif
356
357#ifdef CONFIG_THREAD_CUSTOM_DATA
358 /* crude thread-local storage */
359 void *custom_data;
360#endif
361
362#ifdef CONFIG_ERRNO
363 /* per-thread errno variable */
364 int errno_var;
365#endif
366
367#if defined(CONFIG_THREAD_STACK_INFO)
368 /* Stack Info */
369 struct _thread_stack_info stack_info;
370#endif /* CONFIG_THREAD_STACK_INFO */
371
372 /* arch-specifics: must always be at the end */
373 struct _thread_arch arch;
374};
375
376typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400377typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700378#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400379
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400380enum execution_context_types {
381 K_ISR = 0,
382 K_COOP_THREAD,
383 K_PREEMPT_THREAD,
384};
385
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400386/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100387 * @defgroup profiling_apis Profiling APIs
388 * @ingroup kernel_apis
389 * @{
390 */
391
392/**
393 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
394 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700395 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100396 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
397 *
398 * CONFIG_MAIN_STACK_SIZE
399 * CONFIG_IDLE_STACK_SIZE
400 * CONFIG_ISR_STACK_SIZE
401 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
402 *
403 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
404 * produce output.
405 *
406 * @return N/A
407 */
408extern void k_call_stacks_analyze(void);
409
410/**
411 * @} end defgroup profiling_apis
412 */
413
414/**
Allan Stephensc98da842016-11-11 15:45:03 -0500415 * @defgroup thread_apis Thread APIs
416 * @ingroup kernel_apis
417 * @{
418 */
419
Benjamin Walshed240f22017-01-22 13:05:08 -0500420#endif /* !_ASMLANGUAGE */
421
422
423/*
424 * Thread user options. May be needed by assembly code. Common part uses low
425 * bits, arch-specific use high bits.
426 */
427
428/* system thread that must not abort */
429#define K_ESSENTIAL (1 << 0)
430
431#if defined(CONFIG_FP_SHARING)
432/* thread uses floating point registers */
433#define K_FP_REGS (1 << 1)
434#endif
435
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700436/* This thread has dropped from supervisor mode to user mode and consequently
437 * has additional restrictions
438 */
439#define K_USER (1 << 2)
440
Benjamin Walshed240f22017-01-22 13:05:08 -0500441#ifdef CONFIG_X86
442/* x86 Bitmask definitions for threads user options */
443
444#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
445/* thread uses SSEx (and also FP) registers */
446#define K_SSE_REGS (1 << 7)
447#endif
448#endif
449
450/* end - thread options */
451
452#if !defined(_ASMLANGUAGE)
453
Andrew Boie507852a2017-07-25 18:47:07 -0700454/* Using typedef deliberately here, this is quite intended to be an opaque
455 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
456 *
457 * The purpose of this data type is to clearly distinguish between the
458 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
459 * buffer which composes the stack data actually used by the underlying
460 * thread; they cannot be used interchangably as some arches precede the
461 * stack buffer region with guard areas that trigger a MPU or MMU fault
462 * if written to.
463 *
464 * APIs that want to work with the buffer inside should continue to use
465 * char *.
466 *
467 * Stacks should always be created with K_THREAD_STACK_DEFINE().
468 */
469struct __packed _k_thread_stack_element {
470 char data;
471};
472typedef struct _k_thread_stack_element *k_thread_stack_t;
473
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400474
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400475/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700476 * @brief Create a thread.
477 *
478 * This routine initializes a thread, then schedules it for execution.
479 *
480 * The new thread may be scheduled for immediate execution or a delayed start.
481 * If the newly spawned thread does not have a delayed start the kernel
482 * scheduler may preempt the current thread to allow the new thread to
483 * execute.
484 *
485 * Thread options are architecture-specific, and can include K_ESSENTIAL,
486 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
487 * them using "|" (the logical OR operator).
488 *
489 * Historically, users often would use the beginning of the stack memory region
490 * to store the struct k_thread data, although corruption will occur if the
491 * stack overflows this region and stack protection features may not detect this
492 * situation.
493 *
494 * @param new_thread Pointer to uninitialized struct k_thread
495 * @param stack Pointer to the stack space.
496 * @param stack_size Stack size in bytes.
497 * @param entry Thread entry function.
498 * @param p1 1st entry point parameter.
499 * @param p2 2nd entry point parameter.
500 * @param p3 3rd entry point parameter.
501 * @param prio Thread priority.
502 * @param options Thread options.
503 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
504 *
505 * @return ID of new thread.
506 */
Andrew Boie507852a2017-07-25 18:47:07 -0700507extern k_tid_t k_thread_create(struct k_thread *new_thread,
508 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700509 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700510 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700511 void *p1, void *p2, void *p3,
512 int prio, u32_t options, s32_t delay);
513
Andrew Boie3f091b52017-08-30 14:34:14 -0700514#ifdef CONFIG_USERSPACE
515/**
516 * @brief Drop a thread's privileges permanently to user mode
517 *
518 * @param entry Function to start executing from
519 * @param p1 1st entry point parameter
520 * @param p2 2nd entry point parameter
521 * @param p3 3rd entry point parameter
522 */
523extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
524 void *p1, void *p2,
525 void *p3);
526#endif
527
Andrew Boied26cf2d2017-03-30 13:07:02 -0700528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
Allan Stephensc98da842016-11-11 15:45:03 -0500531 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500532 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500534 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400535 *
536 * @return N/A
537 */
Kumar Galacc334c72017-04-21 10:55:34 -0500538extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400539
540/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500541 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400542 *
543 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500544 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400545 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400546 * @return N/A
547 */
Kumar Galacc334c72017-04-21 10:55:34 -0500548extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400549
550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500551 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500553 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400554 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500555 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400556 *
557 * @return N/A
558 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400559extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400560
561/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500562 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500564 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500566 * If @a thread is not currently sleeping, the routine has no effect.
567 *
568 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400569 *
570 * @return N/A
571 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400572extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400573
574/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500575 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500577 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400578 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400580
581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500584 * This routine prevents @a thread from executing if it has not yet started
585 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500587 * @param thread ID of thread to cancel.
588 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700589 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500590 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400591 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592extern int k_thread_cancel(k_tid_t thread);
593
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594/**
Allan Stephensc98da842016-11-11 15:45:03 -0500595 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500597 * This routine permanently stops execution of @a thread. The thread is taken
598 * off all kernel queues it is part of (i.e. the ready queue, the timeout
599 * queue, or a kernel object wait queue). However, any kernel resources the
600 * thread might currently own (such as mutexes or memory blocks) are not
601 * released. It is the responsibility of the caller of this routine to ensure
602 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500604 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605 *
606 * @return N/A
607 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400608extern void k_thread_abort(k_tid_t thread);
609
Andrew Boie7d627c52017-08-30 11:01:56 -0700610
611/**
612 * @brief Start an inactive thread
613 *
614 * If a thread was created with K_FOREVER in the delay parameter, it will
615 * not be added to the scheduling queue until this function is called
616 * on it.
617 *
618 * @param thread thread to start
619 */
620extern void k_thread_start(k_tid_t thread);
621
Allan Stephensc98da842016-11-11 15:45:03 -0500622/**
623 * @cond INTERNAL_HIDDEN
624 */
625
Benjamin Walshd211a522016-12-06 11:44:01 -0500626/* timeout has timed out and is not on _timeout_q anymore */
627#define _EXPIRED (-2)
628
629/* timeout is not in use */
630#define _INACTIVE (-1)
631
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400632struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700633 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700634 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400635 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700636 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500637 void *init_p1;
638 void *init_p2;
639 void *init_p3;
640 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500641 u32_t init_options;
642 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500643 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500644 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400645};
646
Andrew Boied26cf2d2017-03-30 13:07:02 -0700647#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400648 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500649 prio, options, delay, abort, groups) \
650 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700651 .init_thread = (thread), \
652 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500653 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700654 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400655 .init_p1 = (void *)p1, \
656 .init_p2 = (void *)p2, \
657 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500658 .init_prio = (prio), \
659 .init_options = (options), \
660 .init_delay = (delay), \
661 .init_abort = (abort), \
662 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400663 }
664
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400665/**
Allan Stephensc98da842016-11-11 15:45:03 -0500666 * INTERNAL_HIDDEN @endcond
667 */
668
669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500670 * @brief Statically define and initialize a thread.
671 *
672 * The thread may be scheduled for immediate execution or a delayed start.
673 *
674 * Thread options are architecture-specific, and can include K_ESSENTIAL,
675 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
676 * them using "|" (the logical OR operator).
677 *
678 * The ID of the thread can be accessed using:
679 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500680 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 *
682 * @param name Name of the thread.
683 * @param stack_size Stack size in bytes.
684 * @param entry Thread entry function.
685 * @param p1 1st entry point parameter.
686 * @param p2 2nd entry point parameter.
687 * @param p3 3rd entry point parameter.
688 * @param prio Thread priority.
689 * @param options Thread options.
690 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400691 *
692 * @internal It has been observed that the x86 compiler by default aligns
693 * these _static_thread_data structures to 32-byte boundaries, thereby
694 * wasting space. To work around this, force a 4-byte alignment.
695 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500696#define K_THREAD_DEFINE(name, stack_size, \
697 entry, p1, p2, p3, \
698 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700699 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700700 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500701 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500702 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700703 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
704 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500705 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700706 NULL, 0); \
707 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400708
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400709/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500710 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500712 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500714 * @param thread ID of thread whose priority is needed.
715 *
716 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400717 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500718extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400719
720/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500721 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500723 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400724 *
725 * Rescheduling can occur immediately depending on the priority @a thread is
726 * set to:
727 *
728 * - If its priority is raised above the priority of the caller of this
729 * function, and the caller is preemptible, @a thread will be scheduled in.
730 *
731 * - If the caller operates on itself, it lowers its priority below that of
732 * other threads in the system, and the caller is preemptible, the thread of
733 * highest priority will be scheduled in.
734 *
735 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
736 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
737 * highest priority.
738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500739 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400740 * @param prio New priority.
741 *
742 * @warning Changing the priority of a thread currently involved in mutex
743 * priority inheritance may result in undefined behavior.
744 *
745 * @return N/A
746 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400747extern void k_thread_priority_set(k_tid_t thread, int prio);
748
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500750 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500752 * This routine prevents the kernel scheduler from making @a thread the
753 * current thread. All other internal operations on @a thread are still
754 * performed; for example, any timeout it is waiting on keeps ticking,
755 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * If @a thread is already suspended, the routine has no effect.
758 *
759 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760 *
761 * @return N/A
762 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400763extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764
765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * This routine allows the kernel scheduler to make @a thread the current
769 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 * If @a thread is not currently suspended, the routine has no effect.
772 *
773 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
775 * @return N/A
776 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400777extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400778
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * This routine specifies how the scheduler will perform time slicing of
783 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500785 * To enable time slicing, @a slice must be non-zero. The scheduler
786 * ensures that no thread runs for more than the specified time limit
787 * before other threads of that priority are given a chance to execute.
788 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700789 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500791 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 * execute. Once the scheduler selects a thread for execution, there is no
793 * minimum guaranteed time the thread will execute before threads of greater or
794 * equal priority are scheduled.
795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 * for execution, this routine has no effect; the thread is immediately
798 * rescheduled after the slice period expires.
799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * To disable timeslicing, set both @a slice and @a prio to zero.
801 *
802 * @param slice Maximum time slice length (in milliseconds).
803 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 *
805 * @return N/A
806 */
Kumar Galacc334c72017-04-21 10:55:34 -0500807extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400808
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809/**
Allan Stephensc98da842016-11-11 15:45:03 -0500810 * @} end defgroup thread_apis
811 */
812
813/**
814 * @addtogroup isr_apis
815 * @{
816 */
817
818/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
Allan Stephensc98da842016-11-11 15:45:03 -0500821 * This routine allows the caller to customize its actions, depending on
822 * whether it is a thread or an ISR.
823 *
824 * @note Can be called by ISRs.
825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * @return 0 if invoked by a thread.
827 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500829extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400830
Benjamin Walsh445830d2016-11-10 15:54:27 -0500831/**
832 * @brief Determine if code is running in a preemptible thread.
833 *
Allan Stephensc98da842016-11-11 15:45:03 -0500834 * This routine allows the caller to customize its actions, depending on
835 * whether it can be preempted by another thread. The routine returns a 'true'
836 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500837 *
Allan Stephensc98da842016-11-11 15:45:03 -0500838 * - The code is running in a thread, not at ISR.
839 * - The thread's priority is in the preemptible range.
840 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500841 *
Allan Stephensc98da842016-11-11 15:45:03 -0500842 * @note Can be called by ISRs.
843 *
844 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500845 * @return Non-zero if invoked by a preemptible thread.
846 */
847extern int k_is_preempt_thread(void);
848
Allan Stephensc98da842016-11-11 15:45:03 -0500849/**
850 * @} end addtogroup isr_apis
851 */
852
853/**
854 * @addtogroup thread_apis
855 * @{
856 */
857
858/**
859 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500860 *
Allan Stephensc98da842016-11-11 15:45:03 -0500861 * This routine prevents the current thread from being preempted by another
862 * thread by instructing the scheduler to treat it as a cooperative thread.
863 * If the thread subsequently performs an operation that makes it unready,
864 * it will be context switched out in the normal manner. When the thread
865 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500866 *
Allan Stephensc98da842016-11-11 15:45:03 -0500867 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500868 *
Allan Stephensc98da842016-11-11 15:45:03 -0500869 * @note k_sched_lock() and k_sched_unlock() should normally be used
870 * when the operation being performed can be safely interrupted by ISRs.
871 * However, if the amount of processing involved is very small, better
872 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500873 *
874 * @return N/A
875 */
876extern void k_sched_lock(void);
877
Allan Stephensc98da842016-11-11 15:45:03 -0500878/**
879 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500880 *
Allan Stephensc98da842016-11-11 15:45:03 -0500881 * This routine reverses the effect of a previous call to k_sched_lock().
882 * A thread must call the routine once for each time it called k_sched_lock()
883 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500884 *
885 * @return N/A
886 */
887extern void k_sched_unlock(void);
888
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500894 * Custom data is not used by the kernel itself, and is freely available
895 * for a thread to use as it sees fit. It can be used as a framework
896 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500898 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400899 *
900 * @return N/A
901 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400902extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903
904/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500907 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400911extern void *k_thread_custom_data_get(void);
912
913/**
Allan Stephensc98da842016-11-11 15:45:03 -0500914 * @} end addtogroup thread_apis
915 */
916
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400917#include <sys_clock.h>
918
Allan Stephensc2f15a42016-11-17 12:24:22 -0500919/**
920 * @addtogroup clock_apis
921 * @{
922 */
923
924/**
925 * @brief Generate null timeout delay.
926 *
927 * This macro generates a timeout delay that that instructs a kernel API
928 * not to wait if the requested operation cannot be performed immediately.
929 *
930 * @return Timeout delay value.
931 */
932#define K_NO_WAIT 0
933
934/**
935 * @brief Generate timeout delay from milliseconds.
936 *
937 * This macro generates a timeout delay that that instructs a kernel API
938 * to wait up to @a ms milliseconds to perform the requested operation.
939 *
940 * @param ms Duration in milliseconds.
941 *
942 * @return Timeout delay value.
943 */
Johan Hedberg14471692016-11-13 10:52:15 +0200944#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500945
946/**
947 * @brief Generate timeout delay from seconds.
948 *
949 * This macro generates a timeout delay that that instructs a kernel API
950 * to wait up to @a s seconds to perform the requested operation.
951 *
952 * @param s Duration in seconds.
953 *
954 * @return Timeout delay value.
955 */
Johan Hedberg14471692016-11-13 10:52:15 +0200956#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500957
958/**
959 * @brief Generate timeout delay from minutes.
960 *
961 * This macro generates a timeout delay that that instructs a kernel API
962 * to wait up to @a m minutes to perform the requested operation.
963 *
964 * @param m Duration in minutes.
965 *
966 * @return Timeout delay value.
967 */
Johan Hedberg14471692016-11-13 10:52:15 +0200968#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500969
970/**
971 * @brief Generate timeout delay from hours.
972 *
973 * This macro generates a timeout delay that that instructs a kernel API
974 * to wait up to @a h hours to perform the requested operation.
975 *
976 * @param h Duration in hours.
977 *
978 * @return Timeout delay value.
979 */
Johan Hedberg14471692016-11-13 10:52:15 +0200980#define K_HOURS(h) K_MINUTES((h) * 60)
981
Allan Stephensc98da842016-11-11 15:45:03 -0500982/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500983 * @brief Generate infinite timeout delay.
984 *
985 * This macro generates a timeout delay that that instructs a kernel API
986 * to wait as long as necessary to perform the requested operation.
987 *
988 * @return Timeout delay value.
989 */
990#define K_FOREVER (-1)
991
992/**
993 * @} end addtogroup clock_apis
994 */
995
996/**
Allan Stephensc98da842016-11-11 15:45:03 -0500997 * @cond INTERNAL_HIDDEN
998 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400999
Benjamin Walsh62092182016-12-20 14:39:08 -05001000/* kernel clocks */
1001
1002#if (sys_clock_ticks_per_sec == 1000) || \
1003 (sys_clock_ticks_per_sec == 500) || \
1004 (sys_clock_ticks_per_sec == 250) || \
1005 (sys_clock_ticks_per_sec == 125) || \
1006 (sys_clock_ticks_per_sec == 100) || \
1007 (sys_clock_ticks_per_sec == 50) || \
1008 (sys_clock_ticks_per_sec == 25) || \
1009 (sys_clock_ticks_per_sec == 20) || \
1010 (sys_clock_ticks_per_sec == 10) || \
1011 (sys_clock_ticks_per_sec == 1)
1012
1013 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1014#else
1015 /* yields horrible 64-bit math on many architectures: try to avoid */
1016 #define _NON_OPTIMIZED_TICKS_PER_SEC
1017#endif
1018
1019#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001020extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001021#else
Kumar Galacc334c72017-04-21 10:55:34 -05001022static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001023{
Kumar Galacc334c72017-04-21 10:55:34 -05001024 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001025}
1026#endif
1027
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001028/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001029#ifdef CONFIG_TICKLESS_KERNEL
1030#define _TICK_ALIGN 0
1031#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001032#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001033#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001034
Kumar Galacc334c72017-04-21 10:55:34 -05001035static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001036{
Benjamin Walsh62092182016-12-20 14:39:08 -05001037#ifdef CONFIG_SYS_CLOCK_EXISTS
1038
1039#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001040 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001041#else
Kumar Galacc334c72017-04-21 10:55:34 -05001042 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001043#endif
1044
1045#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001046 __ASSERT(ticks == 0, "");
1047 return 0;
1048#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001049}
1050
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001051struct k_timer {
1052 /*
1053 * _timeout structure must be first here if we want to use
1054 * dynamic timer allocation. timeout.node is used in the double-linked
1055 * list of free timers
1056 */
1057 struct _timeout timeout;
1058
Allan Stephens45bfa372016-10-12 12:39:42 -05001059 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001060 _wait_q_t wait_q;
1061
1062 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001063 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001064
1065 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001066 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067
1068 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001069 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070
Allan Stephens45bfa372016-10-12 12:39:42 -05001071 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001072 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001073
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001074 /* user-specific data, also used to support legacy features */
1075 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001076
Anas Nashif2f203c22016-12-18 06:57:45 -05001077 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001078};
1079
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001080#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001081 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001082 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001083 .timeout.wait_q = NULL, \
1084 .timeout.thread = NULL, \
1085 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001086 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001087 .expiry_fn = expiry, \
1088 .stop_fn = stop, \
1089 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001090 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001091 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001092 }
1093
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001094#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1095
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096/**
Allan Stephensc98da842016-11-11 15:45:03 -05001097 * INTERNAL_HIDDEN @endcond
1098 */
1099
1100/**
1101 * @defgroup timer_apis Timer APIs
1102 * @ingroup kernel_apis
1103 * @{
1104 */
1105
1106/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001107 * @typedef k_timer_expiry_t
1108 * @brief Timer expiry function type.
1109 *
1110 * A timer's expiry function is executed by the system clock interrupt handler
1111 * each time the timer expires. The expiry function is optional, and is only
1112 * invoked if the timer has been initialized with one.
1113 *
1114 * @param timer Address of timer.
1115 *
1116 * @return N/A
1117 */
1118typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1119
1120/**
1121 * @typedef k_timer_stop_t
1122 * @brief Timer stop function type.
1123 *
1124 * A timer's stop function is executed if the timer is stopped prematurely.
1125 * The function runs in the context of the thread that stops the timer.
1126 * The stop function is optional, and is only invoked if the timer has been
1127 * initialized with one.
1128 *
1129 * @param timer Address of timer.
1130 *
1131 * @return N/A
1132 */
1133typedef void (*k_timer_stop_t)(struct k_timer *timer);
1134
1135/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001138 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001140 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141 *
1142 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * @param expiry_fn Function to invoke each time the timer expires.
1144 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001146#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001147 struct k_timer name \
1148 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001149 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001150
Allan Stephens45bfa372016-10-12 12:39:42 -05001151/**
1152 * @brief Initialize a timer.
1153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001155 *
1156 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001157 * @param expiry_fn Function to invoke each time the timer expires.
1158 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001159 *
1160 * @return N/A
1161 */
1162extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001163 k_timer_expiry_t expiry_fn,
1164 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001165
Allan Stephens45bfa372016-10-12 12:39:42 -05001166/**
1167 * @brief Start a timer.
1168 *
1169 * This routine starts a timer, and resets its status to zero. The timer
1170 * begins counting down using the specified duration and period values.
1171 *
1172 * Attempting to start a timer that is already running is permitted.
1173 * The timer's status is reset to zero and the timer begins counting down
1174 * using the new duration and period values.
1175 *
1176 * @param timer Address of timer.
1177 * @param duration Initial timer duration (in milliseconds).
1178 * @param period Timer period (in milliseconds).
1179 *
1180 * @return N/A
1181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001183 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001184
1185/**
1186 * @brief Stop a timer.
1187 *
1188 * This routine stops a running timer prematurely. The timer's stop function,
1189 * if one exists, is invoked by the caller.
1190 *
1191 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001192 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001193 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001194 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1195 * if @a k_timer_stop is to be called from ISRs.
1196 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001197 * @param timer Address of timer.
1198 *
1199 * @return N/A
1200 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001202
1203/**
1204 * @brief Read timer status.
1205 *
1206 * This routine reads the timer's status, which indicates the number of times
1207 * it has expired since its status was last read.
1208 *
1209 * Calling this routine resets the timer's status to zero.
1210 *
1211 * @param timer Address of timer.
1212 *
1213 * @return Timer status.
1214 */
Kumar Galacc334c72017-04-21 10:55:34 -05001215extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001216
1217/**
1218 * @brief Synchronize thread to timer expiration.
1219 *
1220 * This routine blocks the calling thread until the timer's status is non-zero
1221 * (indicating that it has expired at least once since it was last examined)
1222 * or the timer is stopped. If the timer status is already non-zero,
1223 * or the timer is already stopped, the caller continues without waiting.
1224 *
1225 * Calling this routine resets the timer's status to zero.
1226 *
1227 * This routine must not be used by interrupt handlers, since they are not
1228 * allowed to block.
1229 *
1230 * @param timer Address of timer.
1231 *
1232 * @return Timer status.
1233 */
Kumar Galacc334c72017-04-21 10:55:34 -05001234extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001235
1236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001238 *
1239 * This routine computes the (approximate) time remaining before a running
1240 * timer next expires. If the timer is not running, it returns zero.
1241 *
1242 * @param timer Address of timer.
1243 *
1244 * @return Remaining time (in milliseconds).
1245 */
Kumar Galacc334c72017-04-21 10:55:34 -05001246static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001247{
1248 return _timeout_remaining_get(&timer->timeout);
1249}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001250
Allan Stephensc98da842016-11-11 15:45:03 -05001251/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001252 * @brief Associate user-specific data with a timer.
1253 *
1254 * This routine records the @a user_data with the @a timer, to be retrieved
1255 * later.
1256 *
1257 * It can be used e.g. in a timer handler shared across multiple subsystems to
1258 * retrieve data specific to the subsystem this timer is associated with.
1259 *
1260 * @param timer Address of timer.
1261 * @param user_data User data to associate with the timer.
1262 *
1263 * @return N/A
1264 */
1265static inline void k_timer_user_data_set(struct k_timer *timer,
1266 void *user_data)
1267{
1268 timer->user_data = user_data;
1269}
1270
1271/**
1272 * @brief Retrieve the user-specific data from a timer.
1273 *
1274 * @param timer Address of timer.
1275 *
1276 * @return The user data.
1277 */
1278static inline void *k_timer_user_data_get(struct k_timer *timer)
1279{
1280 return timer->user_data;
1281}
1282
1283/**
Allan Stephensc98da842016-11-11 15:45:03 -05001284 * @} end defgroup timer_apis
1285 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001286
Allan Stephensc98da842016-11-11 15:45:03 -05001287/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001288 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001289 * @{
1290 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001291
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001295 * This routine returns the elapsed time since the system booted,
1296 * in milliseconds.
1297 *
1298 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001299 */
Kumar Galacc334c72017-04-21 10:55:34 -05001300extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001301
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001302#ifdef CONFIG_TICKLESS_KERNEL
1303/**
1304 * @brief Enable clock always on in tickless kernel
1305 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001306 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001307 * there are no timer events programmed in tickless kernel
1308 * scheduling. This is necessary if the clock is used to track
1309 * passage of time.
1310 *
1311 * @retval prev_status Previous status of always on flag
1312 */
1313static inline int k_enable_sys_clock_always_on(void)
1314{
1315 int prev_status = _sys_clock_always_on;
1316
1317 _sys_clock_always_on = 1;
1318 _enable_sys_clock();
1319
1320 return prev_status;
1321}
1322
1323/**
1324 * @brief Disable clock always on in tickless kernel
1325 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001326 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001327 * there are no timer events programmed in tickless kernel
1328 * scheduling. To save power, this routine should be called
1329 * immediately when clock is not used to track time.
1330 */
1331static inline void k_disable_sys_clock_always_on(void)
1332{
1333 _sys_clock_always_on = 0;
1334}
1335#else
1336#define k_enable_sys_clock_always_on() do { } while ((0))
1337#define k_disable_sys_clock_always_on() do { } while ((0))
1338#endif
1339
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001341 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 * This routine returns the lower 32-bits of the elapsed time since the system
1344 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001346 * This routine can be more efficient than k_uptime_get(), as it reduces the
1347 * need for interrupt locking and 64-bit math. However, the 32-bit result
1348 * cannot hold a system uptime time larger than approximately 50 days, so the
1349 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001351 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001352 */
Kumar Galacc334c72017-04-21 10:55:34 -05001353extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001354
1355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * This routine computes the elapsed time between the current system uptime
1359 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * @param reftime Pointer to a reference time, which is updated to the current
1362 * uptime upon return.
1363 *
1364 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001365 */
Kumar Galacc334c72017-04-21 10:55:34 -05001366extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001367
1368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001369 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001371 * This routine computes the elapsed time between the current system uptime
1372 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001374 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1375 * need for interrupt locking and 64-bit math. However, the 32-bit result
1376 * cannot hold an elapsed time larger than approximately 50 days, so the
1377 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001379 * @param reftime Pointer to a reference time, which is updated to the current
1380 * uptime upon return.
1381 *
1382 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001383 */
Kumar Galacc334c72017-04-21 10:55:34 -05001384extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001385
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001387 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * This routine returns the current time, as measured by the system's hardware
1390 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001392 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001394#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001397 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398 */
1399
Allan Stephensc98da842016-11-11 15:45:03 -05001400/**
1401 * @cond INTERNAL_HIDDEN
1402 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001404struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001405 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001406 union {
1407 _wait_q_t wait_q;
1408
1409 _POLL_EVENT;
1410 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001411
1412 _OBJECT_TRACING_NEXT_PTR(k_queue);
1413};
1414
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001415#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001416 { \
1417 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1418 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001419 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001420 _OBJECT_TRACING_INIT \
1421 }
1422
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001423#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1424
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001425/**
1426 * INTERNAL_HIDDEN @endcond
1427 */
1428
1429/**
1430 * @defgroup queue_apis Queue APIs
1431 * @ingroup kernel_apis
1432 * @{
1433 */
1434
1435/**
1436 * @brief Initialize a queue.
1437 *
1438 * This routine initializes a queue object, prior to its first use.
1439 *
1440 * @param queue Address of the queue.
1441 *
1442 * @return N/A
1443 */
1444extern void k_queue_init(struct k_queue *queue);
1445
1446/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001447 * @brief Cancel waiting on a queue.
1448 *
1449 * This routine causes first thread pending on @a queue, if any, to
1450 * return from k_queue_get() call with NULL value (as if timeout expired).
1451 *
1452 * @note Can be called by ISRs.
1453 *
1454 * @param queue Address of the queue.
1455 *
1456 * @return N/A
1457 */
1458extern void k_queue_cancel_wait(struct k_queue *queue);
1459
1460/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001461 * @brief Append an element to the end of a queue.
1462 *
1463 * This routine appends a data item to @a queue. A queue data item must be
1464 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1465 * reserved for the kernel's use.
1466 *
1467 * @note Can be called by ISRs.
1468 *
1469 * @param queue Address of the queue.
1470 * @param data Address of the data item.
1471 *
1472 * @return N/A
1473 */
1474extern void k_queue_append(struct k_queue *queue, void *data);
1475
1476/**
1477 * @brief Prepend an element to a queue.
1478 *
1479 * This routine prepends a data item to @a queue. A queue data item must be
1480 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1481 * reserved for the kernel's use.
1482 *
1483 * @note Can be called by ISRs.
1484 *
1485 * @param queue Address of the queue.
1486 * @param data Address of the data item.
1487 *
1488 * @return N/A
1489 */
1490extern void k_queue_prepend(struct k_queue *queue, void *data);
1491
1492/**
1493 * @brief Inserts an element to a queue.
1494 *
1495 * This routine inserts a data item to @a queue after previous item. A queue
1496 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1497 * item are reserved for the kernel's use.
1498 *
1499 * @note Can be called by ISRs.
1500 *
1501 * @param queue Address of the queue.
1502 * @param prev Address of the previous data item.
1503 * @param data Address of the data item.
1504 *
1505 * @return N/A
1506 */
1507extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1508
1509/**
1510 * @brief Atomically append a list of elements to a queue.
1511 *
1512 * This routine adds a list of data items to @a queue in one operation.
1513 * The data items must be in a singly-linked list, with the first 32 bits
1514 * in each data item pointing to the next data item; the list must be
1515 * NULL-terminated.
1516 *
1517 * @note Can be called by ISRs.
1518 *
1519 * @param queue Address of the queue.
1520 * @param head Pointer to first node in singly-linked list.
1521 * @param tail Pointer to last node in singly-linked list.
1522 *
1523 * @return N/A
1524 */
1525extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1526
1527/**
1528 * @brief Atomically add a list of elements to a queue.
1529 *
1530 * This routine adds a list of data items to @a queue in one operation.
1531 * The data items must be in a singly-linked list implemented using a
1532 * sys_slist_t object. Upon completion, the original list is empty.
1533 *
1534 * @note Can be called by ISRs.
1535 *
1536 * @param queue Address of the queue.
1537 * @param list Pointer to sys_slist_t object.
1538 *
1539 * @return N/A
1540 */
1541extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1542
1543/**
1544 * @brief Get an element from a queue.
1545 *
1546 * This routine removes first data item from @a queue. The first 32 bits of the
1547 * data item are reserved for the kernel's use.
1548 *
1549 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1550 *
1551 * @param queue Address of the queue.
1552 * @param timeout Waiting period to obtain a data item (in milliseconds),
1553 * or one of the special values K_NO_WAIT and K_FOREVER.
1554 *
1555 * @return Address of the data item if successful; NULL if returned
1556 * without waiting, or waiting period timed out.
1557 */
Kumar Galacc334c72017-04-21 10:55:34 -05001558extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001559
1560/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001561 * @brief Remove an element from a queue.
1562 *
1563 * This routine removes data item from @a queue. The first 32 bits of the
1564 * data item are reserved for the kernel's use. Removing elements from k_queue
1565 * rely on sys_slist_find_and_remove which is not a constant time operation.
1566 *
1567 * @note Can be called by ISRs
1568 *
1569 * @param queue Address of the queue.
1570 * @param data Address of the data item.
1571 *
1572 * @return true if data item was removed
1573 */
1574static inline bool k_queue_remove(struct k_queue *queue, void *data)
1575{
1576 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1577}
1578
1579/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001580 * @brief Query a queue to see if it has data available.
1581 *
1582 * Note that the data might be already gone by the time this function returns
1583 * if other threads are also trying to read from the queue.
1584 *
1585 * @note Can be called by ISRs.
1586 *
1587 * @param queue Address of the queue.
1588 *
1589 * @return Non-zero if the queue is empty.
1590 * @return 0 if data is available.
1591 */
1592static inline int k_queue_is_empty(struct k_queue *queue)
1593{
1594 return (int)sys_slist_is_empty(&queue->data_q);
1595}
1596
1597/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001598 * @brief Peek element at the head of queue.
1599 *
1600 * Return element from the head of queue without removing it.
1601 *
1602 * @param queue Address of the queue.
1603 *
1604 * @return Head element, or NULL if queue is empty.
1605 */
1606static inline void *k_queue_peek_head(struct k_queue *queue)
1607{
1608 return sys_slist_peek_head(&queue->data_q);
1609}
1610
1611/**
1612 * @brief Peek element at the tail of queue.
1613 *
1614 * Return element from the tail of queue without removing it.
1615 *
1616 * @param queue Address of the queue.
1617 *
1618 * @return Tail element, or NULL if queue is empty.
1619 */
1620static inline void *k_queue_peek_tail(struct k_queue *queue)
1621{
1622 return sys_slist_peek_tail(&queue->data_q);
1623}
1624
1625/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001626 * @brief Statically define and initialize a queue.
1627 *
1628 * The queue can be accessed outside the module where it is defined using:
1629 *
1630 * @code extern struct k_queue <name>; @endcode
1631 *
1632 * @param name Name of the queue.
1633 */
1634#define K_QUEUE_DEFINE(name) \
1635 struct k_queue name \
1636 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001637 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001638
1639/**
1640 * @} end defgroup queue_apis
1641 */
1642
1643/**
1644 * @cond INTERNAL_HIDDEN
1645 */
1646
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001647struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001648 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001649};
1650
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001651#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001652 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001653 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001654 }
1655
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001656#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1657
Allan Stephensc98da842016-11-11 15:45:03 -05001658/**
1659 * INTERNAL_HIDDEN @endcond
1660 */
1661
1662/**
1663 * @defgroup fifo_apis Fifo APIs
1664 * @ingroup kernel_apis
1665 * @{
1666 */
1667
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001668/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001669 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001671 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001674 *
1675 * @return N/A
1676 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001677#define k_fifo_init(fifo) \
1678 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001679
1680/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001681 * @brief Cancel waiting on a fifo.
1682 *
1683 * This routine causes first thread pending on @a fifo, if any, to
1684 * return from k_fifo_get() call with NULL value (as if timeout
1685 * expired).
1686 *
1687 * @note Can be called by ISRs.
1688 *
1689 * @param fifo Address of the fifo.
1690 *
1691 * @return N/A
1692 */
1693#define k_fifo_cancel_wait(fifo) \
1694 k_queue_cancel_wait((struct k_queue *) fifo)
1695
1696/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001697 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * This routine adds a data item to @a fifo. A fifo data item must be
1700 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1701 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001703 * @note Can be called by ISRs.
1704 *
1705 * @param fifo Address of the fifo.
1706 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
1708 * @return N/A
1709 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001710#define k_fifo_put(fifo, data) \
1711 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001712
1713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * This routine adds a list of data items to @a fifo in one operation.
1717 * The data items must be in a singly-linked list, with the first 32 bits
1718 * each data item pointing to the next data item; the list must be
1719 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @param fifo Address of the fifo.
1724 * @param head Pointer to first node in singly-linked list.
1725 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001726 *
1727 * @return N/A
1728 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001729#define k_fifo_put_list(fifo, head, tail) \
1730 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731
1732/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * This routine adds a list of data items to @a fifo in one operation.
1736 * The data items must be in a singly-linked list implemented using a
1737 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 * and must be re-initialized via sys_slist_init().
1739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @note Can be called by ISRs.
1741 *
1742 * @param fifo Address of the fifo.
1743 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 *
1745 * @return N/A
1746 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001747#define k_fifo_put_slist(fifo, list) \
1748 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749
1750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * This routine removes a data item from @a fifo in a "first in, first out"
1754 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001756 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1757 *
1758 * @param fifo Address of the fifo.
1759 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760 * or one of the special values K_NO_WAIT and K_FOREVER.
1761 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001762 * @return Address of the data item if successful; NULL if returned
1763 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001765#define k_fifo_get(fifo, timeout) \
1766 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001767
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001769 * @brief Query a fifo to see if it has data available.
1770 *
1771 * Note that the data might be already gone by the time this function returns
1772 * if other threads is also trying to read from the fifo.
1773 *
1774 * @note Can be called by ISRs.
1775 *
1776 * @param fifo Address of the fifo.
1777 *
1778 * @return Non-zero if the fifo is empty.
1779 * @return 0 if data is available.
1780 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001781#define k_fifo_is_empty(fifo) \
1782 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001783
1784/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001785 * @brief Peek element at the head of fifo.
1786 *
1787 * Return element from the head of fifo without removing it. A usecase
1788 * for this is if elements of the fifo are themselves containers. Then
1789 * on each iteration of processing, a head container will be peeked,
1790 * and some data processed out of it, and only if the container is empty,
1791 * it will be completely remove from the fifo.
1792 *
1793 * @param fifo Address of the fifo.
1794 *
1795 * @return Head element, or NULL if the fifo is empty.
1796 */
1797#define k_fifo_peek_head(fifo) \
1798 k_queue_peek_head((struct k_queue *) fifo)
1799
1800/**
1801 * @brief Peek element at the tail of fifo.
1802 *
1803 * Return element from the tail of fifo (without removing it). A usecase
1804 * for this is if elements of the fifo are themselves containers. Then
1805 * it may be useful to add more data to the last container in fifo.
1806 *
1807 * @param fifo Address of the fifo.
1808 *
1809 * @return Tail element, or NULL if fifo is empty.
1810 */
1811#define k_fifo_peek_tail(fifo) \
1812 k_queue_peek_tail((struct k_queue *) fifo)
1813
1814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001818 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001819 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001821 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001824 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001825 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001826 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001827
Allan Stephensc98da842016-11-11 15:45:03 -05001828/**
1829 * @} end defgroup fifo_apis
1830 */
1831
1832/**
1833 * @cond INTERNAL_HIDDEN
1834 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001835
1836struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001837 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001838};
1839
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001840#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001841 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001842 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001843 }
1844
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001845#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1846
Allan Stephensc98da842016-11-11 15:45:03 -05001847/**
1848 * INTERNAL_HIDDEN @endcond
1849 */
1850
1851/**
1852 * @defgroup lifo_apis Lifo APIs
1853 * @ingroup kernel_apis
1854 * @{
1855 */
1856
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001860 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 *
1864 * @return N/A
1865 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001866#define k_lifo_init(lifo) \
1867 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001868
1869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * This routine adds a data item to @a lifo. A lifo data item must be
1873 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1874 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @note Can be called by ISRs.
1877 *
1878 * @param lifo Address of the lifo.
1879 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001880 *
1881 * @return N/A
1882 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001883#define k_lifo_put(lifo, data) \
1884 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885
1886/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001887 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001889 * This routine removes a data item from @a lifo in a "last in, first out"
1890 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1893 *
1894 * @param lifo Address of the lifo.
1895 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001896 * or one of the special values K_NO_WAIT and K_FOREVER.
1897 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001898 * @return Address of the data item if successful; NULL if returned
1899 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001901#define k_lifo_get(lifo, timeout) \
1902 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001907 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001908 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001909 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001911 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001913#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001914 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001915 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001916 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001917
Allan Stephensc98da842016-11-11 15:45:03 -05001918/**
1919 * @} end defgroup lifo_apis
1920 */
1921
1922/**
1923 * @cond INTERNAL_HIDDEN
1924 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001925
1926struct k_stack {
1927 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001928 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929
Anas Nashif2f203c22016-12-18 06:57:45 -05001930 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931};
1932
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001933#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001934 { \
1935 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1936 .base = stack_buffer, \
1937 .next = stack_buffer, \
1938 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001939 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001940 }
1941
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001942#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1943
Allan Stephensc98da842016-11-11 15:45:03 -05001944/**
1945 * INTERNAL_HIDDEN @endcond
1946 */
1947
1948/**
1949 * @defgroup stack_apis Stack APIs
1950 * @ingroup kernel_apis
1951 * @{
1952 */
1953
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954/**
1955 * @brief Initialize a stack.
1956 *
1957 * This routine initializes a stack object, prior to its first use.
1958 *
1959 * @param stack Address of the stack.
1960 * @param buffer Address of array used to hold stacked values.
1961 * @param num_entries Maximum number of values that can be stacked.
1962 *
1963 * @return N/A
1964 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001965extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001966 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001967
1968/**
1969 * @brief Push an element onto a stack.
1970 *
1971 * This routine adds a 32-bit value @a data to @a stack.
1972 *
1973 * @note Can be called by ISRs.
1974 *
1975 * @param stack Address of the stack.
1976 * @param data Value to push onto the stack.
1977 *
1978 * @return N/A
1979 */
Kumar Galacc334c72017-04-21 10:55:34 -05001980extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001981
1982/**
1983 * @brief Pop an element from a stack.
1984 *
1985 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1986 * manner and stores the value in @a data.
1987 *
1988 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1989 *
1990 * @param stack Address of the stack.
1991 * @param data Address of area to hold the value popped from the stack.
1992 * @param timeout Waiting period to obtain a value (in milliseconds),
1993 * or one of the special values K_NO_WAIT and K_FOREVER.
1994 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001995 * @retval 0 Element popped from stack.
1996 * @retval -EBUSY Returned without waiting.
1997 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001998 */
Kumar Galacc334c72017-04-21 10:55:34 -05001999extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002000
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002004 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002006 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002008 * @param name Name of the stack.
2009 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002011#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002012 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002013 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002014 struct k_stack name \
2015 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002016 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002017 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002019/**
Allan Stephensc98da842016-11-11 15:45:03 -05002020 * @} end defgroup stack_apis
2021 */
2022
Allan Stephens6bba9b02016-11-16 14:56:54 -05002023struct k_work;
2024
Allan Stephensc98da842016-11-11 15:45:03 -05002025/**
2026 * @defgroup workqueue_apis Workqueue Thread APIs
2027 * @ingroup kernel_apis
2028 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002029 */
2030
Allan Stephens6bba9b02016-11-16 14:56:54 -05002031/**
2032 * @typedef k_work_handler_t
2033 * @brief Work item handler function type.
2034 *
2035 * A work item's handler function is executed by a workqueue's thread
2036 * when the work item is processed by the workqueue.
2037 *
2038 * @param work Address of the work item.
2039 *
2040 * @return N/A
2041 */
2042typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002043
2044/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002045 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002047
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002049 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002050 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002051};
2052
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002054 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055};
2056
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002058 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059 k_work_handler_t handler;
2060 atomic_t flags[1];
2061};
2062
Allan Stephens6bba9b02016-11-16 14:56:54 -05002063struct k_delayed_work {
2064 struct k_work work;
2065 struct _timeout timeout;
2066 struct k_work_q *work_q;
2067};
2068
2069extern struct k_work_q k_sys_work_q;
2070
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002071/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002072 * INTERNAL_HIDDEN @endcond
2073 */
2074
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002075#define _K_WORK_INITIALIZER(work_handler) \
2076 { \
2077 ._reserved = NULL, \
2078 .handler = work_handler, \
2079 .flags = { 0 } \
2080 }
2081
2082#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2083
Allan Stephens6bba9b02016-11-16 14:56:54 -05002084/**
2085 * @brief Initialize a statically-defined work item.
2086 *
2087 * This macro can be used to initialize a statically-defined workqueue work
2088 * item, prior to its first use. For example,
2089 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002090 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002091 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002092 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002093 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002095#define K_WORK_DEFINE(work, work_handler) \
2096 struct k_work work \
2097 __in_section(_k_work, static, work) = \
2098 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002099
2100/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002101 * @brief Initialize a work item.
2102 *
2103 * This routine initializes a workqueue work item, prior to its first use.
2104 *
2105 * @param work Address of work item.
2106 * @param handler Function to invoke each time work item is processed.
2107 *
2108 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002109 */
2110static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2111{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002112 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002113 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002114 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115}
2116
2117/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002118 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002119 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002120 * This routine submits work item @a work to be processed by workqueue
2121 * @a work_q. If the work item is already pending in the workqueue's queue
2122 * as a result of an earlier submission, this routine has no effect on the
2123 * work item. If the work item has already been processed, or is currently
2124 * being processed, its work is considered complete and the work item can be
2125 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002126 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127 * @warning
2128 * A submitted work item must not be modified until it has been processed
2129 * by the workqueue.
2130 *
2131 * @note Can be called by ISRs.
2132 *
2133 * @param work_q Address of workqueue.
2134 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002135 *
2136 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002137 */
2138static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2139 struct k_work *work)
2140{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002141 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002142 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143 }
2144}
2145
2146/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002147 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002149 * This routine indicates if work item @a work is pending in a workqueue's
2150 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002151 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002152 * @note Can be called by ISRs.
2153 *
2154 * @param work Address of work item.
2155 *
2156 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002157 */
2158static inline int k_work_pending(struct k_work *work)
2159{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002160 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002161}
2162
2163/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002164 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002165 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002166 * This routine starts workqueue @a work_q. The workqueue spawns its work
2167 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002169 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002170 * @param stack Pointer to work queue thread's stack space, as defined by
2171 * K_THREAD_STACK_DEFINE()
2172 * @param stack_size Size of the work queue thread's stack (in bytes), which
2173 * should either be the same constant passed to
2174 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002175 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
2177 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178 */
Andrew Boie507852a2017-07-25 18:47:07 -07002179extern void k_work_q_start(struct k_work_q *work_q,
2180 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002181 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002184 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002186 * This routine initializes a workqueue delayed work item, prior to
2187 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189 * @param work Address of delayed work item.
2190 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191 *
2192 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002194extern void k_delayed_work_init(struct k_delayed_work *work,
2195 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196
2197/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002198 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002199 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002200 * This routine schedules work item @a work to be processed by workqueue
2201 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002202 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002203 * Only when the countdown completes is the work item actually submitted to
2204 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002206 * Submitting a previously submitted delayed work item that is still
2207 * counting down cancels the existing submission and restarts the countdown
2208 * using the new delay. If the work item is currently pending on the
2209 * workqueue's queue because the countdown has completed it is too late to
2210 * resubmit the item, and resubmission fails without impacting the work item.
2211 * If the work item has already been processed, or is currently being processed,
2212 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * @warning
2215 * A delayed work item must not be modified until it has been processed
2216 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002218 * @note Can be called by ISRs.
2219 *
2220 * @param work_q Address of workqueue.
2221 * @param work Address of delayed work item.
2222 * @param delay Delay before submitting the work item (in milliseconds).
2223 *
2224 * @retval 0 Work item countdown started.
2225 * @retval -EINPROGRESS Work item is already pending.
2226 * @retval -EINVAL Work item is being processed or has completed its work.
2227 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002229extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2230 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002231 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232
2233/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002234 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002236 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002237 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002238 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002240 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002241 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002242 * @param work Address of delayed work item.
2243 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002244 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * @retval -EINPROGRESS Work item is already pending.
2246 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002247 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002248extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251 * @brief Submit a work item to the system workqueue.
2252 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253 * This routine submits work item @a work to be processed by the system
2254 * workqueue. If the work item is already pending in the workqueue's queue
2255 * as a result of an earlier submission, this routine has no effect on the
2256 * work item. If the work item has already been processed, or is currently
2257 * being processed, its work is considered complete and the work item can be
2258 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * @warning
2261 * Work items submitted to the system workqueue should avoid using handlers
2262 * that block or yield since this may prevent the system workqueue from
2263 * processing other work items in a timely manner.
2264 *
2265 * @note Can be called by ISRs.
2266 *
2267 * @param work Address of work item.
2268 *
2269 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270 */
2271static inline void k_work_submit(struct k_work *work)
2272{
2273 k_work_submit_to_queue(&k_sys_work_q, work);
2274}
2275
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277 * @brief Submit a delayed work item to the system workqueue.
2278 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002279 * This routine schedules work item @a work to be processed by the system
2280 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002281 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002282 * Only when the countdown completes is the work item actually submitted to
2283 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * Submitting a previously submitted delayed work item that is still
2286 * counting down cancels the existing submission and restarts the countdown
2287 * using the new delay. If the work item is currently pending on the
2288 * workqueue's queue because the countdown has completed it is too late to
2289 * resubmit the item, and resubmission fails without impacting the work item.
2290 * If the work item has already been processed, or is currently being processed,
2291 * its work is considered complete and the work item can be resubmitted.
2292 *
2293 * @warning
2294 * Work items submitted to the system workqueue should avoid using handlers
2295 * that block or yield since this may prevent the system workqueue from
2296 * processing other work items in a timely manner.
2297 *
2298 * @note Can be called by ISRs.
2299 *
2300 * @param work Address of delayed work item.
2301 * @param delay Delay before submitting the work item (in milliseconds).
2302 *
2303 * @retval 0 Work item countdown started.
2304 * @retval -EINPROGRESS Work item is already pending.
2305 * @retval -EINVAL Work item is being processed or has completed its work.
2306 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 */
2308static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002309 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002311 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312}
2313
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002315 * @brief Get time remaining before a delayed work gets scheduled.
2316 *
2317 * This routine computes the (approximate) time remaining before a
2318 * delayed work gets executed. If the delayed work is not waiting to be
2319 * schedules, it returns zero.
2320 *
2321 * @param work Delayed work item.
2322 *
2323 * @return Remaining time (in milliseconds).
2324 */
Kumar Galacc334c72017-04-21 10:55:34 -05002325static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002326{
2327 return _timeout_remaining_get(&work->timeout);
2328}
2329
2330/**
Allan Stephensc98da842016-11-11 15:45:03 -05002331 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332 */
2333
Allan Stephensc98da842016-11-11 15:45:03 -05002334/**
2335 * @cond INTERNAL_HIDDEN
2336 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337
2338struct k_mutex {
2339 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002340 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002341 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343
Anas Nashif2f203c22016-12-18 06:57:45 -05002344 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345};
2346
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002347#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348 { \
2349 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2350 .owner = NULL, \
2351 .lock_count = 0, \
2352 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002353 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354 }
2355
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002356#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2357
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002358/**
Allan Stephensc98da842016-11-11 15:45:03 -05002359 * INTERNAL_HIDDEN @endcond
2360 */
2361
2362/**
2363 * @defgroup mutex_apis Mutex APIs
2364 * @ingroup kernel_apis
2365 * @{
2366 */
2367
2368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002371 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002373 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002375 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002376 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002378 struct k_mutex name \
2379 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002380 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002382/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002387 * Upon completion, the mutex is available and does not have an owner.
2388 *
2389 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002390 *
2391 * @return N/A
2392 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002394
2395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002396 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 * This routine locks @a mutex. If the mutex is locked by another thread,
2399 * the calling thread waits until the mutex becomes available or until
2400 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 * A thread is permitted to lock a mutex it has already locked. The operation
2403 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * @param mutex Address of the mutex.
2406 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407 * or one of the special values K_NO_WAIT and K_FOREVER.
2408 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002409 * @retval 0 Mutex locked.
2410 * @retval -EBUSY Returned without waiting.
2411 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002412 */
Kumar Galacc334c72017-04-21 10:55:34 -05002413extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002414
2415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418 * This routine unlocks @a mutex. The mutex must already be locked by the
2419 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
2421 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 * thread.
2424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002426 *
2427 * @return N/A
2428 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429extern void k_mutex_unlock(struct k_mutex *mutex);
2430
Allan Stephensc98da842016-11-11 15:45:03 -05002431/**
2432 * @} end defgroup mutex_apis
2433 */
2434
2435/**
2436 * @cond INTERNAL_HIDDEN
2437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438
2439struct k_sem {
2440 _wait_q_t wait_q;
2441 unsigned int count;
2442 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002443 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444
Anas Nashif2f203c22016-12-18 06:57:45 -05002445 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446};
2447
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002448#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002449 { \
2450 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2451 .count = initial_count, \
2452 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002453 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002454 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002455 }
2456
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002457#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2458
Allan Stephensc98da842016-11-11 15:45:03 -05002459/**
2460 * INTERNAL_HIDDEN @endcond
2461 */
2462
2463/**
2464 * @defgroup semaphore_apis Semaphore APIs
2465 * @ingroup kernel_apis
2466 * @{
2467 */
2468
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002469/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002472 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * @param sem Address of the semaphore.
2475 * @param initial_count Initial semaphore count.
2476 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002477 *
2478 * @return N/A
2479 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002480__syscall static inline void k_sem_init(struct k_sem *sem,
2481 unsigned int initial_count,
2482 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002483
2484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002485 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2490 *
2491 * @param sem Address of the semaphore.
2492 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002493 * or one of the special values K_NO_WAIT and K_FOREVER.
2494 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002495 * @note When porting code from the nanokernel legacy API to the new API, be
2496 * careful with the return value of this function. The return value is the
2497 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2498 * non-zero means failure, while the nano_sem_take family returns 1 for success
2499 * and 0 for failure.
2500 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002501 * @retval 0 Semaphore taken.
2502 * @retval -EBUSY Returned without waiting.
2503 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002504 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002505__syscall static inline int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002506
2507/**
2508 * @brief Give a semaphore.
2509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * This routine gives @a sem, unless the semaphore is already at its maximum
2511 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002516 *
2517 * @return N/A
2518 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002519__syscall static inline void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002521/**
2522 * @brief Reset a semaphore's count to zero.
2523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002527 *
2528 * @return N/A
2529 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002530__syscall_inline static inline void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002531
2532static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533{
2534 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002535}
2536
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002537/**
2538 * @brief Get a semaphore's count.
2539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002544 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002545 */
Andrew Boiefa94ee72017-09-28 16:54:35 -07002546__syscall_inline static inline unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002547
2548static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549{
2550 return sem->count;
2551}
2552
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002553/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002557 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002558 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002560 * @param name Name of the semaphore.
2561 * @param initial_count Initial semaphore count.
2562 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002563 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002565 struct k_sem name \
2566 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002567 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568
Allan Stephensc98da842016-11-11 15:45:03 -05002569/**
2570 * @} end defgroup semaphore_apis
2571 */
2572
2573/**
2574 * @defgroup alert_apis Alert APIs
2575 * @ingroup kernel_apis
2576 * @{
2577 */
2578
Allan Stephens5eceb852016-11-16 10:16:30 -05002579/**
2580 * @typedef k_alert_handler_t
2581 * @brief Alert handler function type.
2582 *
2583 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002584 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002585 * and is only invoked if the alert has been initialized with one.
2586 *
2587 * @param alert Address of the alert.
2588 *
2589 * @return 0 if alert has been consumed; non-zero if alert should pend.
2590 */
2591typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002592
2593/**
2594 * @} end defgroup alert_apis
2595 */
2596
2597/**
2598 * @cond INTERNAL_HIDDEN
2599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002601#define K_ALERT_DEFAULT NULL
2602#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002604struct k_alert {
2605 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002606 atomic_t send_count;
2607 struct k_work work_item;
2608 struct k_sem sem;
2609
Anas Nashif2f203c22016-12-18 06:57:45 -05002610 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002611};
2612
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002613extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002615#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002617 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002618 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002619 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2620 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002621 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002622 }
2623
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002624#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2625
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626/**
Allan Stephensc98da842016-11-11 15:45:03 -05002627 * INTERNAL_HIDDEN @endcond
2628 */
2629
2630/**
2631 * @addtogroup alert_apis
2632 * @{
2633 */
2634
2635/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002637 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002638 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002640 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642 * @param name Name of the alert.
2643 * @param alert_handler Action to take when alert is sent. Specify either
2644 * the address of a function to be invoked by the system workqueue
2645 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2646 * K_ALERT_DEFAULT (which causes the alert to pend).
2647 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002648 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002649#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002650 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002651 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002652 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002653 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @param alert Address of the alert.
2661 * @param handler Action to take when alert is sent. Specify either the address
2662 * of a function to be invoked by the system workqueue thread,
2663 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2664 * K_ALERT_DEFAULT (which causes the alert to pend).
2665 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
2667 * @return N/A
2668 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002669extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2670 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002671
2672/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2678 *
2679 * @param alert Address of the alert.
2680 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 * or one of the special values K_NO_WAIT and K_FOREVER.
2682 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002683 * @retval 0 Alert received.
2684 * @retval -EBUSY Returned without waiting.
2685 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686 */
Kumar Galacc334c72017-04-21 10:55:34 -05002687extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002688
2689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * This routine signals @a alert. The action specified for @a alert will
2693 * be taken, which may trigger the execution of an alert handler function
2694 * and/or cause the alert to pend (assuming the alert has not reached its
2695 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697 * @note Can be called by ISRs.
2698 *
2699 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002700 *
2701 * @return N/A
2702 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002703extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704
2705/**
Allan Stephensc98da842016-11-11 15:45:03 -05002706 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 */
2708
Allan Stephensc98da842016-11-11 15:45:03 -05002709/**
2710 * @cond INTERNAL_HIDDEN
2711 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712
2713struct k_msgq {
2714 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002715 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002716 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 char *buffer_start;
2718 char *buffer_end;
2719 char *read_ptr;
2720 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002721 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722
Anas Nashif2f203c22016-12-18 06:57:45 -05002723 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724};
2725
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002726#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727 { \
2728 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002729 .max_msgs = q_max_msgs, \
2730 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002732 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733 .read_ptr = q_buffer, \
2734 .write_ptr = q_buffer, \
2735 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002736 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737 }
2738
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002739#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2740
Peter Mitsis1da807e2016-10-06 11:36:59 -04002741/**
Allan Stephensc98da842016-11-11 15:45:03 -05002742 * INTERNAL_HIDDEN @endcond
2743 */
2744
2745/**
2746 * @defgroup msgq_apis Message Queue APIs
2747 * @ingroup kernel_apis
2748 * @{
2749 */
2750
2751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2755 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002756 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2757 * message is similarly aligned to this boundary, @a q_msg_size must also be
2758 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * The message queue can be accessed outside the module where it is defined
2761 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002762 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002763 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @param q_name Name of the message queue.
2766 * @param q_msg_size Message size (in bytes).
2767 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002768 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002769 */
2770#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2771 static char __noinit __aligned(q_align) \
2772 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002773 struct k_msgq q_name \
2774 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002775 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002776 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777
Peter Mitsisd7a37502016-10-13 11:37:40 -04002778/**
2779 * @brief Initialize a message queue.
2780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002781 * This routine initializes a message queue object, prior to its first use.
2782 *
Allan Stephensda827222016-11-09 14:23:58 -06002783 * The message queue's ring buffer must contain space for @a max_msgs messages,
2784 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2785 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2786 * that each message is similarly aligned to this boundary, @a q_msg_size
2787 * must also be a multiple of N.
2788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002789 * @param q Address of the message queue.
2790 * @param buffer Pointer to ring buffer that holds queued messages.
2791 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002792 * @param max_msgs Maximum number of messages that can be queued.
2793 *
2794 * @return N/A
2795 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002796extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002797 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002798
2799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002802 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002803 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002804 * @note Can be called by ISRs.
2805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002806 * @param q Address of the message queue.
2807 * @param data Pointer to the message.
2808 * @param timeout Waiting period to add the message (in milliseconds),
2809 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002810 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002811 * @retval 0 Message sent.
2812 * @retval -ENOMSG Returned without waiting or queue purged.
2813 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002814 */
Kumar Galacc334c72017-04-21 10:55:34 -05002815extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002816
2817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002820 * This routine receives a message from message queue @a q in a "first in,
2821 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002822 *
Allan Stephensc98da842016-11-11 15:45:03 -05002823 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @param q Address of the message queue.
2826 * @param data Address of area to hold the received message.
2827 * @param timeout Waiting period to receive the message (in milliseconds),
2828 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002829 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002830 * @retval 0 Message received.
2831 * @retval -ENOMSG Returned without waiting.
2832 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002833 */
Kumar Galacc334c72017-04-21 10:55:34 -05002834extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002835
2836/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002837 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * This routine discards all unreceived messages in a message queue's ring
2840 * buffer. Any threads that are blocked waiting to send a message to the
2841 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002843 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002844 *
2845 * @return N/A
2846 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847extern void k_msgq_purge(struct k_msgq *q);
2848
Peter Mitsis67be2492016-10-07 11:44:34 -04002849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * This routine returns the number of unused entries in a message queue's
2853 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * @param q Address of the message queue.
2856 *
2857 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002858 */
Kumar Galacc334c72017-04-21 10:55:34 -05002859static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002860{
2861 return q->max_msgs - q->used_msgs;
2862}
2863
Peter Mitsisd7a37502016-10-13 11:37:40 -04002864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * @param q Address of the message queue.
2870 *
2871 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002872 */
Kumar Galacc334c72017-04-21 10:55:34 -05002873static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874{
2875 return q->used_msgs;
2876}
2877
Allan Stephensc98da842016-11-11 15:45:03 -05002878/**
2879 * @} end defgroup msgq_apis
2880 */
2881
2882/**
2883 * @defgroup mem_pool_apis Memory Pool APIs
2884 * @ingroup kernel_apis
2885 * @{
2886 */
2887
Andy Ross73cb9582017-05-09 10:42:39 -07002888/* Note on sizing: the use of a 20 bit field for block means that,
2889 * assuming a reasonable minimum block size of 16 bytes, we're limited
2890 * to 16M of memory managed by a single pool. Long term it would be
2891 * good to move to a variable bit size based on configuration.
2892 */
2893struct k_mem_block_id {
2894 u32_t pool : 8;
2895 u32_t level : 4;
2896 u32_t block : 20;
2897};
2898
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002899struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002901 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902};
2903
Allan Stephensc98da842016-11-11 15:45:03 -05002904/**
2905 * @} end defgroup mem_pool_apis
2906 */
2907
2908/**
2909 * @defgroup mailbox_apis Mailbox APIs
2910 * @ingroup kernel_apis
2911 * @{
2912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913
2914struct k_mbox_msg {
2915 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002916 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002918 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002920 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921 /** sender's message data buffer */
2922 void *tx_data;
2923 /** internal use only - needed for legacy API support */
2924 void *_rx_data;
2925 /** message data block descriptor */
2926 struct k_mem_block tx_block;
2927 /** source thread id */
2928 k_tid_t rx_source_thread;
2929 /** target thread id */
2930 k_tid_t tx_target_thread;
2931 /** internal use only - thread waiting on send (may be a dummy) */
2932 k_tid_t _syncing_thread;
2933#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2934 /** internal use only - semaphore used during asynchronous send */
2935 struct k_sem *_async_sem;
2936#endif
2937};
2938
Allan Stephensc98da842016-11-11 15:45:03 -05002939/**
2940 * @cond INTERNAL_HIDDEN
2941 */
2942
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943struct k_mbox {
2944 _wait_q_t tx_msg_queue;
2945 _wait_q_t rx_msg_queue;
2946
Anas Nashif2f203c22016-12-18 06:57:45 -05002947 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002948};
2949
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002950#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951 { \
2952 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2953 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002954 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955 }
2956
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002957#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2958
Peter Mitsis12092702016-10-14 12:57:23 -04002959/**
Allan Stephensc98da842016-11-11 15:45:03 -05002960 * INTERNAL_HIDDEN @endcond
2961 */
2962
2963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002964 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002966 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002967 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002968 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002971 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002973 struct k_mbox name \
2974 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002975 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976
Peter Mitsis12092702016-10-14 12:57:23 -04002977/**
2978 * @brief Initialize a mailbox.
2979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * This routine initializes a mailbox object, prior to its first use.
2981 *
2982 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002983 *
2984 * @return N/A
2985 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986extern void k_mbox_init(struct k_mbox *mbox);
2987
Peter Mitsis12092702016-10-14 12:57:23 -04002988/**
2989 * @brief Send a mailbox message in a synchronous manner.
2990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * This routine sends a message to @a mbox and waits for a receiver to both
2992 * receive and process it. The message data may be in a buffer, in a memory
2993 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * @param mbox Address of the mailbox.
2996 * @param tx_msg Address of the transmit message descriptor.
2997 * @param timeout Waiting period for the message to be received (in
2998 * milliseconds), or one of the special values K_NO_WAIT
2999 * and K_FOREVER. Once the message has been received,
3000 * this routine waits as long as necessary for the message
3001 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003002 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003003 * @retval 0 Message sent.
3004 * @retval -ENOMSG Returned without waiting.
3005 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003006 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003007extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003008 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003009
Peter Mitsis12092702016-10-14 12:57:23 -04003010/**
3011 * @brief Send a mailbox message in an asynchronous manner.
3012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * This routine sends a message to @a mbox without waiting for a receiver
3014 * to process it. The message data may be in a buffer, in a memory pool block,
3015 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3016 * will be given when the message has been both received and completely
3017 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * @param mbox Address of the mailbox.
3020 * @param tx_msg Address of the transmit message descriptor.
3021 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003022 *
3023 * @return N/A
3024 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003025extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026 struct k_sem *sem);
3027
Peter Mitsis12092702016-10-14 12:57:23 -04003028/**
3029 * @brief Receive a mailbox message.
3030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * This routine receives a message from @a mbox, then optionally retrieves
3032 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @param mbox Address of the mailbox.
3035 * @param rx_msg Address of the receive message descriptor.
3036 * @param buffer Address of the buffer to receive data, or NULL to defer data
3037 * retrieval and message disposal until later.
3038 * @param timeout Waiting period for a message to be received (in
3039 * milliseconds), or one of the special values K_NO_WAIT
3040 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003041 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003042 * @retval 0 Message received.
3043 * @retval -ENOMSG Returned without waiting.
3044 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003045 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003046extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003047 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003048
3049/**
3050 * @brief Retrieve mailbox message data into a buffer.
3051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * This routine completes the processing of a received message by retrieving
3053 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003054 *
3055 * Alternatively, this routine can be used to dispose of a received message
3056 * without retrieving its data.
3057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * @param rx_msg Address of the receive message descriptor.
3059 * @param buffer Address of the buffer to receive data, or NULL to discard
3060 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003061 *
3062 * @return N/A
3063 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003064extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003065
3066/**
3067 * @brief Retrieve mailbox message data into a memory pool block.
3068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * This routine completes the processing of a received message by retrieving
3070 * its data into a memory pool block, then disposing of the message.
3071 * The memory pool block that results from successful retrieval must be
3072 * returned to the pool once the data has been processed, even in cases
3073 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003074 *
3075 * Alternatively, this routine can be used to dispose of a received message
3076 * without retrieving its data. In this case there is no need to return a
3077 * memory pool block to the pool.
3078 *
3079 * This routine allocates a new memory pool block for the data only if the
3080 * data is not already in one. If a new block cannot be allocated, the routine
3081 * returns a failure code and the received message is left unchanged. This
3082 * permits the caller to reattempt data retrieval at a later time or to dispose
3083 * of the received message without retrieving its data.
3084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * @param rx_msg Address of a receive message descriptor.
3086 * @param pool Address of memory pool, or NULL to discard data.
3087 * @param block Address of the area to hold memory pool block info.
3088 * @param timeout Waiting period to wait for a memory pool block (in
3089 * milliseconds), or one of the special values K_NO_WAIT
3090 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003091 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003092 * @retval 0 Data retrieved.
3093 * @retval -ENOMEM Returned without waiting.
3094 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003095 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003096extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003097 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003098 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099
Allan Stephensc98da842016-11-11 15:45:03 -05003100/**
3101 * @} end defgroup mailbox_apis
3102 */
3103
3104/**
3105 * @cond INTERNAL_HIDDEN
3106 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107
3108struct k_pipe {
3109 unsigned char *buffer; /* Pipe buffer: may be NULL */
3110 size_t size; /* Buffer size */
3111 size_t bytes_used; /* # bytes used in buffer */
3112 size_t read_index; /* Where in buffer to read from */
3113 size_t write_index; /* Where in buffer to write */
3114
3115 struct {
3116 _wait_q_t readers; /* Reader wait queue */
3117 _wait_q_t writers; /* Writer wait queue */
3118 } wait_q;
3119
Anas Nashif2f203c22016-12-18 06:57:45 -05003120 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003121};
3122
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003123#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003124 { \
3125 .buffer = pipe_buffer, \
3126 .size = pipe_buffer_size, \
3127 .bytes_used = 0, \
3128 .read_index = 0, \
3129 .write_index = 0, \
3130 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3131 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003132 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003133 }
3134
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003135#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3136
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137/**
Allan Stephensc98da842016-11-11 15:45:03 -05003138 * INTERNAL_HIDDEN @endcond
3139 */
3140
3141/**
3142 * @defgroup pipe_apis Pipe APIs
3143 * @ingroup kernel_apis
3144 * @{
3145 */
3146
3147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003148 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003152 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003154 * @param name Name of the pipe.
3155 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3156 * or zero if no ring buffer is used.
3157 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003158 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003159#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3160 static unsigned char __noinit __aligned(pipe_align) \
3161 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003162 struct k_pipe name \
3163 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003164 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003169 * This routine initializes a pipe object, prior to its first use.
3170 *
3171 * @param pipe Address of the pipe.
3172 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3173 * is used.
3174 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3175 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176 *
3177 * @return N/A
3178 */
3179extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3180 size_t size);
3181
3182/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003183 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003185 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003187 * @param pipe Address of the pipe.
3188 * @param data Address of data to write.
3189 * @param bytes_to_write Size of data (in bytes).
3190 * @param bytes_written Address of area to hold the number of bytes written.
3191 * @param min_xfer Minimum number of bytes to write.
3192 * @param timeout Waiting period to wait for the data to be written (in
3193 * milliseconds), or one of the special values K_NO_WAIT
3194 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003195 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003196 * @retval 0 At least @a min_xfer bytes of data were written.
3197 * @retval -EIO Returned without waiting; zero data bytes were written.
3198 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003200 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003201extern int k_pipe_put(struct k_pipe *pipe, void *data,
3202 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003203 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204
3205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003206 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @param pipe Address of the pipe.
3211 * @param data Address to place the data read from pipe.
3212 * @param bytes_to_read Maximum number of data bytes to read.
3213 * @param bytes_read Address of area to hold the number of bytes read.
3214 * @param min_xfer Minimum number of data bytes to read.
3215 * @param timeout Waiting period to wait for the data to be read (in
3216 * milliseconds), or one of the special values K_NO_WAIT
3217 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003218 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003219 * @retval 0 At least @a min_xfer bytes of data were read.
3220 * @retval -EIO Returned without waiting; zero data bytes were read.
3221 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003222 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003224extern int k_pipe_get(struct k_pipe *pipe, void *data,
3225 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003226 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227
3228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * This routine writes the data contained in a memory block to @a pipe.
3232 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003235 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 * @param block Memory block containing data to send
3237 * @param size Number of data bytes in memory block to send
3238 * @param sem Semaphore to signal upon completion (else NULL)
3239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003240 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 */
3242extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3243 size_t size, struct k_sem *sem);
3244
3245/**
Allan Stephensc98da842016-11-11 15:45:03 -05003246 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 */
3248
Allan Stephensc98da842016-11-11 15:45:03 -05003249/**
3250 * @cond INTERNAL_HIDDEN
3251 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003252
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003253struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003255 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003256 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003257 char *buffer;
3258 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003259 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260
Anas Nashif2f203c22016-12-18 06:57:45 -05003261 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262};
3263
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003264#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003265 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266 { \
3267 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003268 .num_blocks = slab_num_blocks, \
3269 .block_size = slab_block_size, \
3270 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271 .free_list = NULL, \
3272 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003273 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 }
3275
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003276#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3277
3278
Peter Mitsis578f9112016-10-07 13:50:31 -04003279/**
Allan Stephensc98da842016-11-11 15:45:03 -05003280 * INTERNAL_HIDDEN @endcond
3281 */
3282
3283/**
3284 * @defgroup mem_slab_apis Memory Slab APIs
3285 * @ingroup kernel_apis
3286 * @{
3287 */
3288
3289/**
Allan Stephensda827222016-11-09 14:23:58 -06003290 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003291 *
Allan Stephensda827222016-11-09 14:23:58 -06003292 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003294 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3295 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003296 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003297 *
Allan Stephensda827222016-11-09 14:23:58 -06003298 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003300 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003301 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * @param name Name of the memory slab.
3304 * @param slab_block_size Size of each memory block (in bytes).
3305 * @param slab_num_blocks Number memory blocks.
3306 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003307 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003308#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3309 char __noinit __aligned(slab_align) \
3310 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3311 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003312 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003313 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003314 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003316/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003317 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003319 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003320 *
Allan Stephensda827222016-11-09 14:23:58 -06003321 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3322 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3323 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3324 * To ensure that each memory block is similarly aligned to this boundary,
3325 * @a slab_block_size must also be a multiple of N.
3326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * @param slab Address of the memory slab.
3328 * @param buffer Pointer to buffer used for the memory blocks.
3329 * @param block_size Size of each memory block (in bytes).
3330 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003331 *
3332 * @return N/A
3333 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003334extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003335 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003336
3337/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003340 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003342 * @param slab Address of the memory slab.
3343 * @param mem Pointer to block address area.
3344 * @param timeout Maximum time to wait for operation to complete
3345 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3346 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003347 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003348 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003350 * @retval -ENOMEM Returned without waiting.
3351 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003352 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003353extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003354 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003355
3356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * This routine releases a previously allocated memory block back to its
3360 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @param slab Address of the memory slab.
3363 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003364 *
3365 * @return N/A
3366 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003367extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003369/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003370 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * This routine gets the number of memory blocks that are currently
3373 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003378 */
Kumar Galacc334c72017-04-21 10:55:34 -05003379static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003380{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003381 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003382}
3383
Peter Mitsisc001aa82016-10-13 13:53:37 -04003384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * This routine gets the number of memory blocks that are currently
3388 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003393 */
Kumar Galacc334c72017-04-21 10:55:34 -05003394static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003395{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003396 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003397}
3398
Allan Stephensc98da842016-11-11 15:45:03 -05003399/**
3400 * @} end defgroup mem_slab_apis
3401 */
3402
3403/**
3404 * @cond INTERNAL_HIDDEN
3405 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003406
Andy Ross73cb9582017-05-09 10:42:39 -07003407struct k_mem_pool_lvl {
3408 union {
3409 u32_t *bits_p;
3410 u32_t bits;
3411 };
3412 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003413};
3414
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003415struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003416 void *buf;
3417 size_t max_sz;
3418 u16_t n_max;
3419 u8_t n_levels;
3420 u8_t max_inline_level;
3421 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003422 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003423};
3424
Andy Ross73cb9582017-05-09 10:42:39 -07003425#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003426
Andy Ross73cb9582017-05-09 10:42:39 -07003427#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3428
3429#define _MPOOL_LVLS(maxsz, minsz) \
3430 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3431 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3432 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3433 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3434 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3435 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3436 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3437 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3438 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3439 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3440 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3441 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3442 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3443 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3444 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3445 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3446
3447/* Rounds the needed bits up to integer multiples of u32_t */
3448#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3449 ((((n_max) << (2*(l))) + 31) / 32)
3450
3451/* One word gets stored free unioned with the pointer, otherwise the
3452 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003453 */
Andy Ross73cb9582017-05-09 10:42:39 -07003454#define _MPOOL_LBIT_WORDS(n_max, l) \
3455 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3456 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003457
Andy Ross73cb9582017-05-09 10:42:39 -07003458/* How many bytes for the bitfields of a single level? */
3459#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3460 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3461 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003462
Andy Ross73cb9582017-05-09 10:42:39 -07003463/* Size of the bitmap array that follows the buffer in allocated memory */
3464#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3465 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3466 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3467 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3468 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3469 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3470 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3471 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3472 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3473 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3474 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3475 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3476 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3477 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3478 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3479 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3480 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003481
3482/**
Allan Stephensc98da842016-11-11 15:45:03 -05003483 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003484 */
3485
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003486/**
Allan Stephensc98da842016-11-11 15:45:03 -05003487 * @addtogroup mem_pool_apis
3488 * @{
3489 */
3490
3491/**
3492 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003494 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3495 * long. The memory pool allows blocks to be repeatedly partitioned into
3496 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003497 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003498 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003499 * If the pool is to be accessed outside the module where it is defined, it
3500 * can be declared via
3501 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003502 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003504 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003505 * @param minsz Size of the smallest blocks in the pool (in bytes).
3506 * @param maxsz Size of the largest blocks in the pool (in bytes).
3507 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003508 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003509 */
Andy Ross73cb9582017-05-09 10:42:39 -07003510#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3511 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3512 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3513 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3514 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3515 .buf = _mpool_buf_##name, \
3516 .max_sz = maxsz, \
3517 .n_max = nmax, \
3518 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3519 .levels = _mpool_lvls_##name, \
3520 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003521
Peter Mitsis937042c2016-10-13 13:18:26 -04003522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003523 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003527 * @param pool Address of the memory pool.
3528 * @param block Pointer to block descriptor for the allocated memory.
3529 * @param size Amount of memory to allocate (in bytes).
3530 * @param timeout Maximum time to wait for operation to complete
3531 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3532 * or K_FOREVER to wait as long as necessary.
3533 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003534 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003536 * @retval -ENOMEM Returned without waiting.
3537 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003538 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003539extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003540 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003541
3542/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003543 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003545 * This routine releases a previously allocated memory block back to its
3546 * memory pool.
3547 *
3548 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003549 *
3550 * @return N/A
3551 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003552extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003553
3554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003556 *
Andy Ross73cb9582017-05-09 10:42:39 -07003557 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 *
Andy Ross73cb9582017-05-09 10:42:39 -07003559 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003560 *
3561 * @return N/A
3562 */
Andy Ross73cb9582017-05-09 10:42:39 -07003563static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003564
3565/**
Allan Stephensc98da842016-11-11 15:45:03 -05003566 * @} end addtogroup mem_pool_apis
3567 */
3568
3569/**
3570 * @defgroup heap_apis Heap Memory Pool APIs
3571 * @ingroup kernel_apis
3572 * @{
3573 */
3574
3575/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003576 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003579 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003581 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003584 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003585extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003586
3587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003588 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003589 *
3590 * This routine provides traditional free() semantics. The memory being
3591 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003592 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003593 * If @a ptr is NULL, no operation is performed.
3594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003596 *
3597 * @return N/A
3598 */
3599extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003600
Allan Stephensc98da842016-11-11 15:45:03 -05003601/**
3602 * @} end defgroup heap_apis
3603 */
3604
Benjamin Walshacc68c12017-01-29 18:57:45 -05003605/* polling API - PRIVATE */
3606
Benjamin Walshb0179862017-02-02 16:39:57 -05003607#ifdef CONFIG_POLL
3608#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3609#else
3610#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3611#endif
3612
Benjamin Walshacc68c12017-01-29 18:57:45 -05003613/* private - implementation data created as needed, per-type */
3614struct _poller {
3615 struct k_thread *thread;
3616};
3617
3618/* private - types bit positions */
3619enum _poll_types_bits {
3620 /* can be used to ignore an event */
3621 _POLL_TYPE_IGNORE,
3622
3623 /* to be signaled by k_poll_signal() */
3624 _POLL_TYPE_SIGNAL,
3625
3626 /* semaphore availability */
3627 _POLL_TYPE_SEM_AVAILABLE,
3628
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003629 /* queue/fifo/lifo data availability */
3630 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003631
3632 _POLL_NUM_TYPES
3633};
3634
3635#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3636
3637/* private - states bit positions */
3638enum _poll_states_bits {
3639 /* default state when creating event */
3640 _POLL_STATE_NOT_READY,
3641
Benjamin Walshacc68c12017-01-29 18:57:45 -05003642 /* signaled by k_poll_signal() */
3643 _POLL_STATE_SIGNALED,
3644
3645 /* semaphore is available */
3646 _POLL_STATE_SEM_AVAILABLE,
3647
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003648 /* data is available to read on queue/fifo/lifo */
3649 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003650
3651 _POLL_NUM_STATES
3652};
3653
3654#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3655
3656#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003657 (32 - (0 \
3658 + 8 /* tag */ \
3659 + _POLL_NUM_TYPES \
3660 + _POLL_NUM_STATES \
3661 + 1 /* modes */ \
3662 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003663
3664#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3665#error overflow of 32-bit word in struct k_poll_event
3666#endif
3667
3668/* end of polling API - PRIVATE */
3669
3670
3671/**
3672 * @defgroup poll_apis Async polling APIs
3673 * @ingroup kernel_apis
3674 * @{
3675 */
3676
3677/* Public polling API */
3678
3679/* public - values for k_poll_event.type bitfield */
3680#define K_POLL_TYPE_IGNORE 0
3681#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3682#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003683#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3684#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003685
3686/* public - polling modes */
3687enum k_poll_modes {
3688 /* polling thread does not take ownership of objects when available */
3689 K_POLL_MODE_NOTIFY_ONLY = 0,
3690
3691 K_POLL_NUM_MODES
3692};
3693
3694/* public - values for k_poll_event.state bitfield */
3695#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003696#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3697#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003698#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3699#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003700
3701/* public - poll signal object */
3702struct k_poll_signal {
3703 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003704 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003705
3706 /*
3707 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3708 * user resets it to 0.
3709 */
3710 unsigned int signaled;
3711
3712 /* custom result value passed to k_poll_signal() if needed */
3713 int result;
3714};
3715
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003716#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003717 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003718 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003719 .signaled = 0, \
3720 .result = 0, \
3721 }
3722
3723struct k_poll_event {
3724 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003725 sys_dnode_t _node;
3726
3727 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003728 struct _poller *poller;
3729
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003730 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003731 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003732
Benjamin Walshacc68c12017-01-29 18:57:45 -05003733 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003734 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003735
3736 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003737 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003738
3739 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003740 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003741
3742 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003743 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003744
3745 /* per-type data */
3746 union {
3747 void *obj;
3748 struct k_poll_signal *signal;
3749 struct k_sem *sem;
3750 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003751 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003752 };
3753};
3754
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003755#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003756 { \
3757 .poller = NULL, \
3758 .type = event_type, \
3759 .state = K_POLL_STATE_NOT_READY, \
3760 .mode = event_mode, \
3761 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003762 { .obj = event_obj }, \
3763 }
3764
3765#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3766 event_tag) \
3767 { \
3768 .type = event_type, \
3769 .tag = event_tag, \
3770 .state = K_POLL_STATE_NOT_READY, \
3771 .mode = event_mode, \
3772 .unused = 0, \
3773 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003774 }
3775
3776/**
3777 * @brief Initialize one struct k_poll_event instance
3778 *
3779 * After this routine is called on a poll event, the event it ready to be
3780 * placed in an event array to be passed to k_poll().
3781 *
3782 * @param event The event to initialize.
3783 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3784 * values. Only values that apply to the same object being polled
3785 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3786 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003787 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003788 * @param obj Kernel object or poll signal.
3789 *
3790 * @return N/A
3791 */
3792
Kumar Galacc334c72017-04-21 10:55:34 -05003793extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003794 int mode, void *obj);
3795
3796/**
3797 * @brief Wait for one or many of multiple poll events to occur
3798 *
3799 * This routine allows a thread to wait concurrently for one or many of
3800 * multiple poll events to have occurred. Such events can be a kernel object
3801 * being available, like a semaphore, or a poll signal event.
3802 *
3803 * When an event notifies that a kernel object is available, the kernel object
3804 * is not "given" to the thread calling k_poll(): it merely signals the fact
3805 * that the object was available when the k_poll() call was in effect. Also,
3806 * all threads trying to acquire an object the regular way, i.e. by pending on
3807 * the object, have precedence over the thread polling on the object. This
3808 * means that the polling thread will never get the poll event on an object
3809 * until the object becomes available and its pend queue is empty. For this
3810 * reason, the k_poll() call is more effective when the objects being polled
3811 * only have one thread, the polling thread, trying to acquire them.
3812 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003813 * When k_poll() returns 0, the caller should loop on all the events that were
3814 * passed to k_poll() and check the state field for the values that were
3815 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003816 *
3817 * Before being reused for another call to k_poll(), the user has to reset the
3818 * state field to K_POLL_STATE_NOT_READY.
3819 *
3820 * @param events An array of pointers to events to be polled for.
3821 * @param num_events The number of events in the array.
3822 * @param timeout Waiting period for an event to be ready (in milliseconds),
3823 * or one of the special values K_NO_WAIT and K_FOREVER.
3824 *
3825 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003826 * @retval -EAGAIN Waiting period timed out.
3827 */
3828
3829extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003830 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003831
3832/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003833 * @brief Initialize a poll signal object.
3834 *
3835 * Ready a poll signal object to be signaled via k_poll_signal().
3836 *
3837 * @param signal A poll signal.
3838 *
3839 * @return N/A
3840 */
3841
3842extern void k_poll_signal_init(struct k_poll_signal *signal);
3843
3844/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003845 * @brief Signal a poll signal object.
3846 *
3847 * This routine makes ready a poll signal, which is basically a poll event of
3848 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3849 * made ready to run. A @a result value can be specified.
3850 *
3851 * The poll signal contains a 'signaled' field that, when set by
3852 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3853 * be reset by the user before being passed again to k_poll() or k_poll() will
3854 * consider it being signaled, and will return immediately.
3855 *
3856 * @param signal A poll signal.
3857 * @param result The value to store in the result field of the signal.
3858 *
3859 * @retval 0 The signal was delivered successfully.
3860 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3861 */
3862
3863extern int k_poll_signal(struct k_poll_signal *signal, int result);
3864
3865/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003866extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003867
3868/**
3869 * @} end defgroup poll_apis
3870 */
3871
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003872/**
3873 * @brief Make the CPU idle.
3874 *
3875 * This function makes the CPU idle until an event wakes it up.
3876 *
3877 * In a regular system, the idle thread should be the only thread responsible
3878 * for making the CPU idle and triggering any type of power management.
3879 * However, in some more constrained systems, such as a single-threaded system,
3880 * the only thread would be responsible for this if needed.
3881 *
3882 * @return N/A
3883 */
3884extern void k_cpu_idle(void);
3885
3886/**
3887 * @brief Make the CPU idle in an atomic fashion.
3888 *
3889 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3890 * must be done atomically before making the CPU idle.
3891 *
3892 * @param key Interrupt locking key obtained from irq_lock().
3893 *
3894 * @return N/A
3895 */
3896extern void k_cpu_atomic_idle(unsigned int key);
3897
Kumar Galacc334c72017-04-21 10:55:34 -05003898extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003899
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003900#include <arch/cpu.h>
3901
Andrew Boiecdb94d62017-04-18 15:22:05 -07003902#ifdef _ARCH_EXCEPT
3903/* This archtecture has direct support for triggering a CPU exception */
3904#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3905#else
3906
3907#include <misc/printk.h>
3908
3909/* NOTE: This is the implementation for arches that do not implement
3910 * _ARCH_EXCEPT() to generate a real CPU exception.
3911 *
3912 * We won't have a real exception frame to determine the PC value when
3913 * the oops occurred, so print file and line number before we jump into
3914 * the fatal error handler.
3915 */
3916#define _k_except_reason(reason) do { \
3917 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3918 _NanoFatalErrorHandler(reason, &_default_esf); \
3919 CODE_UNREACHABLE; \
3920 } while (0)
3921
3922#endif /* _ARCH__EXCEPT */
3923
3924/**
3925 * @brief Fatally terminate a thread
3926 *
3927 * This should be called when a thread has encountered an unrecoverable
3928 * runtime condition and needs to terminate. What this ultimately
3929 * means is determined by the _fatal_error_handler() implementation, which
3930 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3931 *
3932 * If this is called from ISR context, the default system fatal error handler
3933 * will treat it as an unrecoverable system error, just like k_panic().
3934 */
3935#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3936
3937/**
3938 * @brief Fatally terminate the system
3939 *
3940 * This should be called when the Zephyr kernel has encountered an
3941 * unrecoverable runtime condition and needs to terminate. What this ultimately
3942 * means is determined by the _fatal_error_handler() implementation, which
3943 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3944 */
3945#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3946
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003947/*
3948 * private APIs that are utilized by one or more public APIs
3949 */
3950
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003951#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003952extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003953#else
3954#define _init_static_threads() do { } while ((0))
3955#endif
3956
3957extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003958extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003959
Andrew Boiedc5d9352017-06-02 12:56:47 -07003960/* arch/cpu.h may declare an architecture or platform-specific macro
3961 * for properly declaring stacks, compatible with MMU/MPU constraints if
3962 * enabled
3963 */
3964#ifdef _ARCH_THREAD_STACK_DEFINE
3965#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3966#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3967 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3968#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3969#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003970static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3971{
3972 return _ARCH_THREAD_STACK_BUFFER(sym);
3973}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003974#else
3975/**
3976 * @brief Declare a toplevel thread stack memory region
3977 *
3978 * This declares a region of memory suitable for use as a thread's stack.
3979 *
3980 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3981 * 'noinit' section so that it isn't zeroed at boot
3982 *
Andrew Boie507852a2017-07-25 18:47:07 -07003983 * The declared symbol will always be a k_thread_stack_t which can be passed to
3984 * k_thread_create, but should otherwise not be manipulated. If the buffer
3985 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003986 *
3987 * It is legal to precede this definition with the 'static' keyword.
3988 *
3989 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
3990 * parameter of k_thread_create(), it may not be the same as the
3991 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
3992 *
3993 * @param sym Thread stack symbol name
3994 * @param size Size of the stack memory region
3995 */
3996#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07003997 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07003998
3999/**
4000 * @brief Declare a toplevel array of thread stack memory regions
4001 *
4002 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4003 * definition for additional details and constraints.
4004 *
4005 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4006 * 'noinit' section so that it isn't zeroed at boot
4007 *
4008 * @param sym Thread stack symbol name
4009 * @param nmemb Number of stacks to declare
4010 * @param size Size of the stack memory region
4011 */
4012
4013#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004014 struct _k_thread_stack_element __noinit \
4015 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004016
4017/**
4018 * @brief Declare an embedded stack memory region
4019 *
4020 * Used for stacks embedded within other data structures. Use is highly
4021 * discouraged but in some cases necessary. For memory protection scenarios,
4022 * it is very important that any RAM preceding this member not be writable
4023 * by threads else a stack overflow will lead to silent corruption. In other
4024 * words, the containing data structure should live in RAM owned by the kernel.
4025 *
4026 * @param sym Thread stack symbol name
4027 * @param size Size of the stack memory region
4028 */
4029#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004030 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004031
4032/**
4033 * @brief Return the size in bytes of a stack memory region
4034 *
4035 * Convenience macro for passing the desired stack size to k_thread_create()
4036 * since the underlying implementation may actually create something larger
4037 * (for instance a guard area).
4038 *
4039 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004040 * passed to K_THREAD_STACK_DEFINE.
4041 *
4042 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4043 * it is not guaranteed to return the original value since each array
4044 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004045 *
4046 * @param sym Stack memory symbol
4047 * @return Size of the stack
4048 */
4049#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4050
4051/**
4052 * @brief Get a pointer to the physical stack buffer
4053 *
4054 * Convenience macro to get at the real underlying stack buffer used by
4055 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4056 * This is really only intended for diagnostic tools which want to examine
4057 * stack memory contents.
4058 *
4059 * @param sym Declared stack symbol name
4060 * @return The buffer itself, a char *
4061 */
Andrew Boie507852a2017-07-25 18:47:07 -07004062static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4063{
4064 return (char *)sym;
4065}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004066
4067#endif /* _ARCH_DECLARE_STACK */
4068
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004069#ifdef __cplusplus
4070}
4071#endif
4072
Andrew Boiee004dec2016-11-07 09:01:19 -08004073#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4074/*
4075 * Define new and delete operators.
4076 * At this moment, the operators do nothing since objects are supposed
4077 * to be statically allocated.
4078 */
4079inline void operator delete(void *ptr)
4080{
4081 (void)ptr;
4082}
4083
4084inline void operator delete[](void *ptr)
4085{
4086 (void)ptr;
4087}
4088
4089inline void *operator new(size_t size)
4090{
4091 (void)size;
4092 return NULL;
4093}
4094
4095inline void *operator new[](size_t size)
4096{
4097 (void)size;
4098 return NULL;
4099}
4100
4101/* Placement versions of operator new and delete */
4102inline void operator delete(void *ptr1, void *ptr2)
4103{
4104 (void)ptr1;
4105 (void)ptr2;
4106}
4107
4108inline void operator delete[](void *ptr1, void *ptr2)
4109{
4110 (void)ptr1;
4111 (void)ptr2;
4112}
4113
4114inline void *operator new(size_t size, void *ptr)
4115{
4116 (void)size;
4117 return ptr;
4118}
4119
4120inline void *operator new[](size_t size, void *ptr)
4121{
4122 (void)size;
4123 return ptr;
4124}
4125
4126#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4127
Andrew Boiefa94ee72017-09-28 16:54:35 -07004128#include <syscalls/kernel.h>
4129
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004130#endif /* !_ASMLANGUAGE */
4131
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004132#endif /* _kernel__h_ */