blob: cbdb1da130accba8f3a0bde55e8dd483112dafee [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 Boiefc273c02017-09-23 12:51:23 -07002480static inline void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2481 unsigned int limit);
2482
2483K_SYSCALL_DECLARE3_VOID(K_SYSCALL_SEM_INIT, k_sem_init,
2484 struct k_sem *, sem,
2485 unsigned int, initial_count,
2486 unsigned int, limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002487
2488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002491 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2494 *
2495 * @param sem Address of the semaphore.
2496 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002497 * or one of the special values K_NO_WAIT and K_FOREVER.
2498 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002499 * @note When porting code from the nanokernel legacy API to the new API, be
2500 * careful with the return value of this function. The return value is the
2501 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2502 * non-zero means failure, while the nano_sem_take family returns 1 for success
2503 * and 0 for failure.
2504 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002505 * @retval 0 Semaphore taken.
2506 * @retval -EBUSY Returned without waiting.
2507 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002508 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002509static inline int k_sem_take(struct k_sem *sem, s32_t timeout);
2510
2511K_SYSCALL_DECLARE2(K_SYSCALL_SEM_TAKE, k_sem_take, int,
2512 struct k_sem *, sem,
2513 s32_t, timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514
2515/**
2516 * @brief Give a semaphore.
2517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * This routine gives @a sem, unless the semaphore is already at its maximum
2519 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002524 *
2525 * @return N/A
2526 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002527static inline void k_sem_give(struct k_sem *sem);
2528
2529K_SYSCALL_DECLARE1_VOID(K_SYSCALL_SEM_GIVE, k_sem_give,
2530 struct k_sem *, sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002532/**
2533 * @brief Reset a semaphore's count to zero.
2534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002535 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002538 *
2539 * @return N/A
2540 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002541static inline void k_sem_reset(struct k_sem *sem);
2542
2543static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544{
2545 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546}
2547
Andrew Boiefc273c02017-09-23 12:51:23 -07002548K_SYSCALL_DECLARE1_VOID_INLINE(K_SYSCALL_SEM_RESET, k_sem_reset,
2549 struct k_sem *, sem);
2550
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002551/**
2552 * @brief Get a semaphore's count.
2553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002556 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002558 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002559 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002560static inline unsigned int k_sem_count_get(struct k_sem *sem);
2561
2562static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002563{
2564 return sem->count;
2565}
2566
Andrew Boiefc273c02017-09-23 12:51:23 -07002567K_SYSCALL_DECLARE1_INLINE(K_SYSCALL_SEM_COUNT_GET, k_sem_count_get,
2568 unsigned int, struct k_sem *, sem);
2569
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002574 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002575 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @param name Name of the semaphore.
2578 * @param initial_count Initial semaphore count.
2579 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002580 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002582 struct k_sem name \
2583 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002584 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585
Allan Stephensc98da842016-11-11 15:45:03 -05002586/**
2587 * @} end defgroup semaphore_apis
2588 */
2589
2590/**
2591 * @defgroup alert_apis Alert APIs
2592 * @ingroup kernel_apis
2593 * @{
2594 */
2595
Allan Stephens5eceb852016-11-16 10:16:30 -05002596/**
2597 * @typedef k_alert_handler_t
2598 * @brief Alert handler function type.
2599 *
2600 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002601 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002602 * and is only invoked if the alert has been initialized with one.
2603 *
2604 * @param alert Address of the alert.
2605 *
2606 * @return 0 if alert has been consumed; non-zero if alert should pend.
2607 */
2608typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002609
2610/**
2611 * @} end defgroup alert_apis
2612 */
2613
2614/**
2615 * @cond INTERNAL_HIDDEN
2616 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002618#define K_ALERT_DEFAULT NULL
2619#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002621struct k_alert {
2622 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002623 atomic_t send_count;
2624 struct k_work work_item;
2625 struct k_sem sem;
2626
Anas Nashif2f203c22016-12-18 06:57:45 -05002627 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628};
2629
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002630extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002632#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002634 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002636 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2637 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002638 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639 }
2640
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002641#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2642
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002643/**
Allan Stephensc98da842016-11-11 15:45:03 -05002644 * INTERNAL_HIDDEN @endcond
2645 */
2646
2647/**
2648 * @addtogroup alert_apis
2649 * @{
2650 */
2651
2652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002653 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002655 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002657 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002659 * @param name Name of the alert.
2660 * @param alert_handler Action to take when alert is sent. Specify either
2661 * the address of a function to be invoked by the system workqueue
2662 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2663 * K_ALERT_DEFAULT (which causes the alert to pend).
2664 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002666#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002667 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002668 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002669 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002670 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * @param alert Address of the alert.
2678 * @param handler Action to take when alert is sent. Specify either the address
2679 * of a function to be invoked by the system workqueue thread,
2680 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2681 * K_ALERT_DEFAULT (which causes the alert to pend).
2682 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683 *
2684 * @return N/A
2685 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002686extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2687 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002688
2689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002690 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002692 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002694 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2695 *
2696 * @param alert Address of the alert.
2697 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002698 * or one of the special values K_NO_WAIT and K_FOREVER.
2699 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002700 * @retval 0 Alert received.
2701 * @retval -EBUSY Returned without waiting.
2702 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002703 */
Kumar Galacc334c72017-04-21 10:55:34 -05002704extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705
2706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002709 * This routine signals @a alert. The action specified for @a alert will
2710 * be taken, which may trigger the execution of an alert handler function
2711 * and/or cause the alert to pend (assuming the alert has not reached its
2712 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * @note Can be called by ISRs.
2715 *
2716 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717 *
2718 * @return N/A
2719 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002720extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721
2722/**
Allan Stephensc98da842016-11-11 15:45:03 -05002723 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 */
2725
Allan Stephensc98da842016-11-11 15:45:03 -05002726/**
2727 * @cond INTERNAL_HIDDEN
2728 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729
2730struct k_msgq {
2731 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002732 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002733 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734 char *buffer_start;
2735 char *buffer_end;
2736 char *read_ptr;
2737 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002738 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739
Anas Nashif2f203c22016-12-18 06:57:45 -05002740 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741};
2742
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002743#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 { \
2745 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002746 .max_msgs = q_max_msgs, \
2747 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002749 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 .read_ptr = q_buffer, \
2751 .write_ptr = q_buffer, \
2752 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002753 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754 }
2755
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002756#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2757
Peter Mitsis1da807e2016-10-06 11:36:59 -04002758/**
Allan Stephensc98da842016-11-11 15:45:03 -05002759 * INTERNAL_HIDDEN @endcond
2760 */
2761
2762/**
2763 * @defgroup msgq_apis Message Queue APIs
2764 * @ingroup kernel_apis
2765 * @{
2766 */
2767
2768/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002771 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2772 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002773 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2774 * message is similarly aligned to this boundary, @a q_msg_size must also be
2775 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002777 * The message queue can be accessed outside the module where it is defined
2778 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002779 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002780 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002782 * @param q_name Name of the message queue.
2783 * @param q_msg_size Message size (in bytes).
2784 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002785 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002786 */
2787#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2788 static char __noinit __aligned(q_align) \
2789 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002790 struct k_msgq q_name \
2791 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002792 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002793 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794
Peter Mitsisd7a37502016-10-13 11:37:40 -04002795/**
2796 * @brief Initialize a message queue.
2797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * This routine initializes a message queue object, prior to its first use.
2799 *
Allan Stephensda827222016-11-09 14:23:58 -06002800 * The message queue's ring buffer must contain space for @a max_msgs messages,
2801 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2802 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2803 * that each message is similarly aligned to this boundary, @a q_msg_size
2804 * must also be a multiple of N.
2805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002806 * @param q Address of the message queue.
2807 * @param buffer Pointer to ring buffer that holds queued messages.
2808 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002809 * @param max_msgs Maximum number of messages that can be queued.
2810 *
2811 * @return N/A
2812 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002813extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002814 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002815
2816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002820 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002821 * @note Can be called by ISRs.
2822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @param q Address of the message queue.
2824 * @param data Pointer to the message.
2825 * @param timeout Waiting period to add the message (in milliseconds),
2826 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002827 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002828 * @retval 0 Message sent.
2829 * @retval -ENOMSG Returned without waiting or queue purged.
2830 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002831 */
Kumar Galacc334c72017-04-21 10:55:34 -05002832extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002833
2834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002835 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002837 * This routine receives a message from message queue @a q in a "first in,
2838 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002839 *
Allan Stephensc98da842016-11-11 15:45:03 -05002840 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002842 * @param q Address of the message queue.
2843 * @param data Address of area to hold the received message.
2844 * @param timeout Waiting period to receive the message (in milliseconds),
2845 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002846 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002847 * @retval 0 Message received.
2848 * @retval -ENOMSG Returned without waiting.
2849 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002850 */
Kumar Galacc334c72017-04-21 10:55:34 -05002851extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002852
2853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * This routine discards all unreceived messages in a message queue's ring
2857 * buffer. Any threads that are blocked waiting to send a message to the
2858 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002861 *
2862 * @return N/A
2863 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864extern void k_msgq_purge(struct k_msgq *q);
2865
Peter Mitsis67be2492016-10-07 11:44:34 -04002866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * This routine returns the number of unused entries in a message queue's
2870 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * @param q Address of the message queue.
2873 *
2874 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002875 */
Kumar Galacc334c72017-04-21 10:55:34 -05002876static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002877{
2878 return q->max_msgs - q->used_msgs;
2879}
2880
Peter Mitsisd7a37502016-10-13 11:37:40 -04002881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * @param q Address of the message queue.
2887 *
2888 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002889 */
Kumar Galacc334c72017-04-21 10:55:34 -05002890static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891{
2892 return q->used_msgs;
2893}
2894
Allan Stephensc98da842016-11-11 15:45:03 -05002895/**
2896 * @} end defgroup msgq_apis
2897 */
2898
2899/**
2900 * @defgroup mem_pool_apis Memory Pool APIs
2901 * @ingroup kernel_apis
2902 * @{
2903 */
2904
Andy Ross73cb9582017-05-09 10:42:39 -07002905/* Note on sizing: the use of a 20 bit field for block means that,
2906 * assuming a reasonable minimum block size of 16 bytes, we're limited
2907 * to 16M of memory managed by a single pool. Long term it would be
2908 * good to move to a variable bit size based on configuration.
2909 */
2910struct k_mem_block_id {
2911 u32_t pool : 8;
2912 u32_t level : 4;
2913 u32_t block : 20;
2914};
2915
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002918 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919};
2920
Allan Stephensc98da842016-11-11 15:45:03 -05002921/**
2922 * @} end defgroup mem_pool_apis
2923 */
2924
2925/**
2926 * @defgroup mailbox_apis Mailbox APIs
2927 * @ingroup kernel_apis
2928 * @{
2929 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930
2931struct k_mbox_msg {
2932 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002933 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002935 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002936 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002937 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938 /** sender's message data buffer */
2939 void *tx_data;
2940 /** internal use only - needed for legacy API support */
2941 void *_rx_data;
2942 /** message data block descriptor */
2943 struct k_mem_block tx_block;
2944 /** source thread id */
2945 k_tid_t rx_source_thread;
2946 /** target thread id */
2947 k_tid_t tx_target_thread;
2948 /** internal use only - thread waiting on send (may be a dummy) */
2949 k_tid_t _syncing_thread;
2950#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2951 /** internal use only - semaphore used during asynchronous send */
2952 struct k_sem *_async_sem;
2953#endif
2954};
2955
Allan Stephensc98da842016-11-11 15:45:03 -05002956/**
2957 * @cond INTERNAL_HIDDEN
2958 */
2959
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960struct k_mbox {
2961 _wait_q_t tx_msg_queue;
2962 _wait_q_t rx_msg_queue;
2963
Anas Nashif2f203c22016-12-18 06:57:45 -05002964 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965};
2966
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002967#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002968 { \
2969 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2970 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002971 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972 }
2973
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002974#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2975
Peter Mitsis12092702016-10-14 12:57:23 -04002976/**
Allan Stephensc98da842016-11-11 15:45:03 -05002977 * INTERNAL_HIDDEN @endcond
2978 */
2979
2980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002984 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002985 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002988 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002990 struct k_mbox name \
2991 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002992 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002993
Peter Mitsis12092702016-10-14 12:57:23 -04002994/**
2995 * @brief Initialize a mailbox.
2996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * This routine initializes a mailbox object, prior to its first use.
2998 *
2999 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003000 *
3001 * @return N/A
3002 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003003extern void k_mbox_init(struct k_mbox *mbox);
3004
Peter Mitsis12092702016-10-14 12:57:23 -04003005/**
3006 * @brief Send a mailbox message in a synchronous manner.
3007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * This routine sends a message to @a mbox and waits for a receiver to both
3009 * receive and process it. The message data may be in a buffer, in a memory
3010 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003012 * @param mbox Address of the mailbox.
3013 * @param tx_msg Address of the transmit message descriptor.
3014 * @param timeout Waiting period for the message to be received (in
3015 * milliseconds), or one of the special values K_NO_WAIT
3016 * and K_FOREVER. Once the message has been received,
3017 * this routine waits as long as necessary for the message
3018 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003019 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003020 * @retval 0 Message sent.
3021 * @retval -ENOMSG Returned without waiting.
3022 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003023 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003024extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003025 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003026
Peter Mitsis12092702016-10-14 12:57:23 -04003027/**
3028 * @brief Send a mailbox message in an asynchronous manner.
3029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * This routine sends a message to @a mbox without waiting for a receiver
3031 * to process it. The message data may be in a buffer, in a memory pool block,
3032 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3033 * will be given when the message has been both received and completely
3034 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * @param mbox Address of the mailbox.
3037 * @param tx_msg Address of the transmit message descriptor.
3038 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003039 *
3040 * @return N/A
3041 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003042extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003043 struct k_sem *sem);
3044
Peter Mitsis12092702016-10-14 12:57:23 -04003045/**
3046 * @brief Receive a mailbox message.
3047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003048 * This routine receives a message from @a mbox, then optionally retrieves
3049 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * @param mbox Address of the mailbox.
3052 * @param rx_msg Address of the receive message descriptor.
3053 * @param buffer Address of the buffer to receive data, or NULL to defer data
3054 * retrieval and message disposal until later.
3055 * @param timeout Waiting period for a message to be received (in
3056 * milliseconds), or one of the special values K_NO_WAIT
3057 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003058 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003059 * @retval 0 Message received.
3060 * @retval -ENOMSG Returned without waiting.
3061 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003062 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003063extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003064 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003065
3066/**
3067 * @brief Retrieve mailbox message data into a buffer.
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 buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003071 *
3072 * Alternatively, this routine can be used to dispose of a received message
3073 * without retrieving its data.
3074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003075 * @param rx_msg Address of the receive message descriptor.
3076 * @param buffer Address of the buffer to receive data, or NULL to discard
3077 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003078 *
3079 * @return N/A
3080 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003081extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003082
3083/**
3084 * @brief Retrieve mailbox message data into a memory pool block.
3085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * This routine completes the processing of a received message by retrieving
3087 * its data into a memory pool block, then disposing of the message.
3088 * The memory pool block that results from successful retrieval must be
3089 * returned to the pool once the data has been processed, even in cases
3090 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003091 *
3092 * Alternatively, this routine can be used to dispose of a received message
3093 * without retrieving its data. In this case there is no need to return a
3094 * memory pool block to the pool.
3095 *
3096 * This routine allocates a new memory pool block for the data only if the
3097 * data is not already in one. If a new block cannot be allocated, the routine
3098 * returns a failure code and the received message is left unchanged. This
3099 * permits the caller to reattempt data retrieval at a later time or to dispose
3100 * of the received message without retrieving its data.
3101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * @param rx_msg Address of a receive message descriptor.
3103 * @param pool Address of memory pool, or NULL to discard data.
3104 * @param block Address of the area to hold memory pool block info.
3105 * @param timeout Waiting period to wait for a memory pool block (in
3106 * milliseconds), or one of the special values K_NO_WAIT
3107 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003108 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003109 * @retval 0 Data retrieved.
3110 * @retval -ENOMEM Returned without waiting.
3111 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003112 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003113extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003114 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003115 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116
Allan Stephensc98da842016-11-11 15:45:03 -05003117/**
3118 * @} end defgroup mailbox_apis
3119 */
3120
3121/**
3122 * @cond INTERNAL_HIDDEN
3123 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003124
3125struct k_pipe {
3126 unsigned char *buffer; /* Pipe buffer: may be NULL */
3127 size_t size; /* Buffer size */
3128 size_t bytes_used; /* # bytes used in buffer */
3129 size_t read_index; /* Where in buffer to read from */
3130 size_t write_index; /* Where in buffer to write */
3131
3132 struct {
3133 _wait_q_t readers; /* Reader wait queue */
3134 _wait_q_t writers; /* Writer wait queue */
3135 } wait_q;
3136
Anas Nashif2f203c22016-12-18 06:57:45 -05003137 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138};
3139
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003140#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141 { \
3142 .buffer = pipe_buffer, \
3143 .size = pipe_buffer_size, \
3144 .bytes_used = 0, \
3145 .read_index = 0, \
3146 .write_index = 0, \
3147 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3148 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003149 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150 }
3151
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003152#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3153
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003154/**
Allan Stephensc98da842016-11-11 15:45:03 -05003155 * INTERNAL_HIDDEN @endcond
3156 */
3157
3158/**
3159 * @defgroup pipe_apis Pipe APIs
3160 * @ingroup kernel_apis
3161 * @{
3162 */
3163
3164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003165 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003168 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003169 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003171 * @param name Name of the pipe.
3172 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3173 * or zero if no ring buffer is used.
3174 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003176#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3177 static unsigned char __noinit __aligned(pipe_align) \
3178 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003179 struct k_pipe name \
3180 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003181 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003184 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * This routine initializes a pipe object, prior to its first use.
3187 *
3188 * @param pipe Address of the pipe.
3189 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3190 * is used.
3191 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3192 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193 *
3194 * @return N/A
3195 */
3196extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3197 size_t size);
3198
3199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * @param pipe Address of the pipe.
3205 * @param data Address of data to write.
3206 * @param bytes_to_write Size of data (in bytes).
3207 * @param bytes_written Address of area to hold the number of bytes written.
3208 * @param min_xfer Minimum number of bytes to write.
3209 * @param timeout Waiting period to wait for the data to be written (in
3210 * milliseconds), or one of the special values K_NO_WAIT
3211 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003213 * @retval 0 At least @a min_xfer bytes of data were written.
3214 * @retval -EIO Returned without waiting; zero data bytes were written.
3215 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003218extern int k_pipe_put(struct k_pipe *pipe, void *data,
3219 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003220 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221
3222/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * @param pipe Address of the pipe.
3228 * @param data Address to place the data read from pipe.
3229 * @param bytes_to_read Maximum number of data bytes to read.
3230 * @param bytes_read Address of area to hold the number of bytes read.
3231 * @param min_xfer Minimum number of data bytes to read.
3232 * @param timeout Waiting period to wait for the data to be read (in
3233 * milliseconds), or one of the special values K_NO_WAIT
3234 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003236 * @retval 0 At least @a min_xfer bytes of data were read.
3237 * @retval -EIO Returned without waiting; zero data bytes were read.
3238 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003239 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003240 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003241extern int k_pipe_get(struct k_pipe *pipe, void *data,
3242 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003243 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244
3245/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * This routine writes the data contained in a memory block to @a pipe.
3249 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003250 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003253 * @param block Memory block containing data to send
3254 * @param size Number of data bytes in memory block to send
3255 * @param sem Semaphore to signal upon completion (else NULL)
3256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258 */
3259extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3260 size_t size, struct k_sem *sem);
3261
3262/**
Allan Stephensc98da842016-11-11 15:45:03 -05003263 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264 */
3265
Allan Stephensc98da842016-11-11 15:45:03 -05003266/**
3267 * @cond INTERNAL_HIDDEN
3268 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003270struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003272 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003273 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 char *buffer;
3275 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003276 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277
Anas Nashif2f203c22016-12-18 06:57:45 -05003278 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279};
3280
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003281#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003282 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003283 { \
3284 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003285 .num_blocks = slab_num_blocks, \
3286 .block_size = slab_block_size, \
3287 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288 .free_list = NULL, \
3289 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003290 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291 }
3292
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003293#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3294
3295
Peter Mitsis578f9112016-10-07 13:50:31 -04003296/**
Allan Stephensc98da842016-11-11 15:45:03 -05003297 * INTERNAL_HIDDEN @endcond
3298 */
3299
3300/**
3301 * @defgroup mem_slab_apis Memory Slab APIs
3302 * @ingroup kernel_apis
3303 * @{
3304 */
3305
3306/**
Allan Stephensda827222016-11-09 14:23:58 -06003307 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003308 *
Allan Stephensda827222016-11-09 14:23:58 -06003309 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003311 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3312 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003313 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003314 *
Allan Stephensda827222016-11-09 14:23:58 -06003315 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003317 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003318 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003320 * @param name Name of the memory slab.
3321 * @param slab_block_size Size of each memory block (in bytes).
3322 * @param slab_num_blocks Number memory blocks.
3323 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003324 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003325#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3326 char __noinit __aligned(slab_align) \
3327 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3328 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003329 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003330 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003331 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003333/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003334 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003337 *
Allan Stephensda827222016-11-09 14:23:58 -06003338 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3339 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3340 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3341 * To ensure that each memory block is similarly aligned to this boundary,
3342 * @a slab_block_size must also be a multiple of N.
3343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * @param slab Address of the memory slab.
3345 * @param buffer Pointer to buffer used for the memory blocks.
3346 * @param block_size Size of each memory block (in bytes).
3347 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003348 *
3349 * @return N/A
3350 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003351extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003352 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003353
3354/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * @param slab Address of the memory slab.
3360 * @param mem Pointer to block address area.
3361 * @param timeout Maximum time to wait for operation to complete
3362 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3363 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003364 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003365 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003367 * @retval -ENOMEM Returned without waiting.
3368 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003369 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003370extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003371 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003372
3373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * This routine releases a previously allocated memory block back to its
3377 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @param slab Address of the memory slab.
3380 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003381 *
3382 * @return N/A
3383 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003384extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * This routine gets the number of memory blocks that are currently
3390 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003395 */
Kumar Galacc334c72017-04-21 10:55:34 -05003396static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003398 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399}
3400
Peter Mitsisc001aa82016-10-13 13:53:37 -04003401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * This routine gets the number of memory blocks that are currently
3405 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003410 */
Kumar Galacc334c72017-04-21 10:55:34 -05003411static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003412{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003413 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003414}
3415
Allan Stephensc98da842016-11-11 15:45:03 -05003416/**
3417 * @} end defgroup mem_slab_apis
3418 */
3419
3420/**
3421 * @cond INTERNAL_HIDDEN
3422 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003423
Andy Ross73cb9582017-05-09 10:42:39 -07003424struct k_mem_pool_lvl {
3425 union {
3426 u32_t *bits_p;
3427 u32_t bits;
3428 };
3429 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003430};
3431
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003432struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003433 void *buf;
3434 size_t max_sz;
3435 u16_t n_max;
3436 u8_t n_levels;
3437 u8_t max_inline_level;
3438 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003439 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003440};
3441
Andy Ross73cb9582017-05-09 10:42:39 -07003442#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003443
Andy Ross73cb9582017-05-09 10:42:39 -07003444#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3445
3446#define _MPOOL_LVLS(maxsz, minsz) \
3447 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3448 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3449 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3450 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3451 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3452 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3453 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3454 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3455 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3456 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3457 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3458 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3459 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3460 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3461 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3462 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3463
3464/* Rounds the needed bits up to integer multiples of u32_t */
3465#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3466 ((((n_max) << (2*(l))) + 31) / 32)
3467
3468/* One word gets stored free unioned with the pointer, otherwise the
3469 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003470 */
Andy Ross73cb9582017-05-09 10:42:39 -07003471#define _MPOOL_LBIT_WORDS(n_max, l) \
3472 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3473 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003474
Andy Ross73cb9582017-05-09 10:42:39 -07003475/* How many bytes for the bitfields of a single level? */
3476#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3477 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3478 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003479
Andy Ross73cb9582017-05-09 10:42:39 -07003480/* Size of the bitmap array that follows the buffer in allocated memory */
3481#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3482 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3483 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3484 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3485 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3486 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3487 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3488 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3489 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3490 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3491 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3492 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3493 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3494 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3495 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3496 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3497 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003498
3499/**
Allan Stephensc98da842016-11-11 15:45:03 -05003500 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003501 */
3502
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003503/**
Allan Stephensc98da842016-11-11 15:45:03 -05003504 * @addtogroup mem_pool_apis
3505 * @{
3506 */
3507
3508/**
3509 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003511 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3512 * long. The memory pool allows blocks to be repeatedly partitioned into
3513 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003514 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003515 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003516 * If the pool is to be accessed outside the module where it is defined, it
3517 * can be declared via
3518 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003519 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003522 * @param minsz Size of the smallest blocks in the pool (in bytes).
3523 * @param maxsz Size of the largest blocks in the pool (in bytes).
3524 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003526 */
Andy Ross73cb9582017-05-09 10:42:39 -07003527#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3528 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3529 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3530 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3531 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3532 .buf = _mpool_buf_##name, \
3533 .max_sz = maxsz, \
3534 .n_max = nmax, \
3535 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3536 .levels = _mpool_lvls_##name, \
3537 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003538
Peter Mitsis937042c2016-10-13 13:18:26 -04003539/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003540 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @param pool Address of the memory pool.
3545 * @param block Pointer to block descriptor for the allocated memory.
3546 * @param size Amount of memory to allocate (in bytes).
3547 * @param timeout Maximum time to wait for operation to complete
3548 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3549 * or K_FOREVER to wait as long as necessary.
3550 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003551 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003553 * @retval -ENOMEM Returned without waiting.
3554 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003555 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003556extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003557 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003558
3559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * This routine releases a previously allocated memory block back to its
3563 * memory pool.
3564 *
3565 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003566 *
3567 * @return N/A
3568 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003570
3571/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003573 *
Andy Ross73cb9582017-05-09 10:42:39 -07003574 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003575 *
Andy Ross73cb9582017-05-09 10:42:39 -07003576 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003577 *
3578 * @return N/A
3579 */
Andy Ross73cb9582017-05-09 10:42:39 -07003580static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003581
3582/**
Allan Stephensc98da842016-11-11 15:45:03 -05003583 * @} end addtogroup mem_pool_apis
3584 */
3585
3586/**
3587 * @defgroup heap_apis Heap Memory Pool APIs
3588 * @ingroup kernel_apis
3589 * @{
3590 */
3591
3592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003593 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003596 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003600 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003601 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003602extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003603
3604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003606 *
3607 * This routine provides traditional free() semantics. The memory being
3608 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003609 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003610 * If @a ptr is NULL, no operation is performed.
3611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003612 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003613 *
3614 * @return N/A
3615 */
3616extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003617
Allan Stephensc98da842016-11-11 15:45:03 -05003618/**
3619 * @} end defgroup heap_apis
3620 */
3621
Benjamin Walshacc68c12017-01-29 18:57:45 -05003622/* polling API - PRIVATE */
3623
Benjamin Walshb0179862017-02-02 16:39:57 -05003624#ifdef CONFIG_POLL
3625#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3626#else
3627#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3628#endif
3629
Benjamin Walshacc68c12017-01-29 18:57:45 -05003630/* private - implementation data created as needed, per-type */
3631struct _poller {
3632 struct k_thread *thread;
3633};
3634
3635/* private - types bit positions */
3636enum _poll_types_bits {
3637 /* can be used to ignore an event */
3638 _POLL_TYPE_IGNORE,
3639
3640 /* to be signaled by k_poll_signal() */
3641 _POLL_TYPE_SIGNAL,
3642
3643 /* semaphore availability */
3644 _POLL_TYPE_SEM_AVAILABLE,
3645
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003646 /* queue/fifo/lifo data availability */
3647 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003648
3649 _POLL_NUM_TYPES
3650};
3651
3652#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3653
3654/* private - states bit positions */
3655enum _poll_states_bits {
3656 /* default state when creating event */
3657 _POLL_STATE_NOT_READY,
3658
Benjamin Walshacc68c12017-01-29 18:57:45 -05003659 /* signaled by k_poll_signal() */
3660 _POLL_STATE_SIGNALED,
3661
3662 /* semaphore is available */
3663 _POLL_STATE_SEM_AVAILABLE,
3664
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003665 /* data is available to read on queue/fifo/lifo */
3666 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003667
3668 _POLL_NUM_STATES
3669};
3670
3671#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3672
3673#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003674 (32 - (0 \
3675 + 8 /* tag */ \
3676 + _POLL_NUM_TYPES \
3677 + _POLL_NUM_STATES \
3678 + 1 /* modes */ \
3679 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003680
3681#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3682#error overflow of 32-bit word in struct k_poll_event
3683#endif
3684
3685/* end of polling API - PRIVATE */
3686
3687
3688/**
3689 * @defgroup poll_apis Async polling APIs
3690 * @ingroup kernel_apis
3691 * @{
3692 */
3693
3694/* Public polling API */
3695
3696/* public - values for k_poll_event.type bitfield */
3697#define K_POLL_TYPE_IGNORE 0
3698#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3699#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003700#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3701#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003702
3703/* public - polling modes */
3704enum k_poll_modes {
3705 /* polling thread does not take ownership of objects when available */
3706 K_POLL_MODE_NOTIFY_ONLY = 0,
3707
3708 K_POLL_NUM_MODES
3709};
3710
3711/* public - values for k_poll_event.state bitfield */
3712#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003713#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3714#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003715#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3716#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003717
3718/* public - poll signal object */
3719struct k_poll_signal {
3720 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003721 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003722
3723 /*
3724 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3725 * user resets it to 0.
3726 */
3727 unsigned int signaled;
3728
3729 /* custom result value passed to k_poll_signal() if needed */
3730 int result;
3731};
3732
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003733#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003734 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003735 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003736 .signaled = 0, \
3737 .result = 0, \
3738 }
3739
3740struct k_poll_event {
3741 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003742 sys_dnode_t _node;
3743
3744 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003745 struct _poller *poller;
3746
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003747 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003748 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003749
Benjamin Walshacc68c12017-01-29 18:57:45 -05003750 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003751 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003752
3753 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003754 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003755
3756 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003757 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003758
3759 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003760 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003761
3762 /* per-type data */
3763 union {
3764 void *obj;
3765 struct k_poll_signal *signal;
3766 struct k_sem *sem;
3767 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003768 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003769 };
3770};
3771
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003772#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003773 { \
3774 .poller = NULL, \
3775 .type = event_type, \
3776 .state = K_POLL_STATE_NOT_READY, \
3777 .mode = event_mode, \
3778 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003779 { .obj = event_obj }, \
3780 }
3781
3782#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3783 event_tag) \
3784 { \
3785 .type = event_type, \
3786 .tag = event_tag, \
3787 .state = K_POLL_STATE_NOT_READY, \
3788 .mode = event_mode, \
3789 .unused = 0, \
3790 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003791 }
3792
3793/**
3794 * @brief Initialize one struct k_poll_event instance
3795 *
3796 * After this routine is called on a poll event, the event it ready to be
3797 * placed in an event array to be passed to k_poll().
3798 *
3799 * @param event The event to initialize.
3800 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3801 * values. Only values that apply to the same object being polled
3802 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3803 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003804 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003805 * @param obj Kernel object or poll signal.
3806 *
3807 * @return N/A
3808 */
3809
Kumar Galacc334c72017-04-21 10:55:34 -05003810extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811 int mode, void *obj);
3812
3813/**
3814 * @brief Wait for one or many of multiple poll events to occur
3815 *
3816 * This routine allows a thread to wait concurrently for one or many of
3817 * multiple poll events to have occurred. Such events can be a kernel object
3818 * being available, like a semaphore, or a poll signal event.
3819 *
3820 * When an event notifies that a kernel object is available, the kernel object
3821 * is not "given" to the thread calling k_poll(): it merely signals the fact
3822 * that the object was available when the k_poll() call was in effect. Also,
3823 * all threads trying to acquire an object the regular way, i.e. by pending on
3824 * the object, have precedence over the thread polling on the object. This
3825 * means that the polling thread will never get the poll event on an object
3826 * until the object becomes available and its pend queue is empty. For this
3827 * reason, the k_poll() call is more effective when the objects being polled
3828 * only have one thread, the polling thread, trying to acquire them.
3829 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003830 * When k_poll() returns 0, the caller should loop on all the events that were
3831 * passed to k_poll() and check the state field for the values that were
3832 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003833 *
3834 * Before being reused for another call to k_poll(), the user has to reset the
3835 * state field to K_POLL_STATE_NOT_READY.
3836 *
3837 * @param events An array of pointers to events to be polled for.
3838 * @param num_events The number of events in the array.
3839 * @param timeout Waiting period for an event to be ready (in milliseconds),
3840 * or one of the special values K_NO_WAIT and K_FOREVER.
3841 *
3842 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003843 * @retval -EAGAIN Waiting period timed out.
3844 */
3845
3846extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003847 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003848
3849/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003850 * @brief Initialize a poll signal object.
3851 *
3852 * Ready a poll signal object to be signaled via k_poll_signal().
3853 *
3854 * @param signal A poll signal.
3855 *
3856 * @return N/A
3857 */
3858
3859extern void k_poll_signal_init(struct k_poll_signal *signal);
3860
3861/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003862 * @brief Signal a poll signal object.
3863 *
3864 * This routine makes ready a poll signal, which is basically a poll event of
3865 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3866 * made ready to run. A @a result value can be specified.
3867 *
3868 * The poll signal contains a 'signaled' field that, when set by
3869 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3870 * be reset by the user before being passed again to k_poll() or k_poll() will
3871 * consider it being signaled, and will return immediately.
3872 *
3873 * @param signal A poll signal.
3874 * @param result The value to store in the result field of the signal.
3875 *
3876 * @retval 0 The signal was delivered successfully.
3877 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3878 */
3879
3880extern int k_poll_signal(struct k_poll_signal *signal, int result);
3881
3882/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003883extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003884
3885/**
3886 * @} end defgroup poll_apis
3887 */
3888
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003889/**
3890 * @brief Make the CPU idle.
3891 *
3892 * This function makes the CPU idle until an event wakes it up.
3893 *
3894 * In a regular system, the idle thread should be the only thread responsible
3895 * for making the CPU idle and triggering any type of power management.
3896 * However, in some more constrained systems, such as a single-threaded system,
3897 * the only thread would be responsible for this if needed.
3898 *
3899 * @return N/A
3900 */
3901extern void k_cpu_idle(void);
3902
3903/**
3904 * @brief Make the CPU idle in an atomic fashion.
3905 *
3906 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3907 * must be done atomically before making the CPU idle.
3908 *
3909 * @param key Interrupt locking key obtained from irq_lock().
3910 *
3911 * @return N/A
3912 */
3913extern void k_cpu_atomic_idle(unsigned int key);
3914
Kumar Galacc334c72017-04-21 10:55:34 -05003915extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003916
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003917#include <arch/cpu.h>
3918
Andrew Boiecdb94d62017-04-18 15:22:05 -07003919#ifdef _ARCH_EXCEPT
3920/* This archtecture has direct support for triggering a CPU exception */
3921#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3922#else
3923
3924#include <misc/printk.h>
3925
3926/* NOTE: This is the implementation for arches that do not implement
3927 * _ARCH_EXCEPT() to generate a real CPU exception.
3928 *
3929 * We won't have a real exception frame to determine the PC value when
3930 * the oops occurred, so print file and line number before we jump into
3931 * the fatal error handler.
3932 */
3933#define _k_except_reason(reason) do { \
3934 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3935 _NanoFatalErrorHandler(reason, &_default_esf); \
3936 CODE_UNREACHABLE; \
3937 } while (0)
3938
3939#endif /* _ARCH__EXCEPT */
3940
3941/**
3942 * @brief Fatally terminate a thread
3943 *
3944 * This should be called when a thread has encountered an unrecoverable
3945 * runtime condition and needs to terminate. What this ultimately
3946 * means is determined by the _fatal_error_handler() implementation, which
3947 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3948 *
3949 * If this is called from ISR context, the default system fatal error handler
3950 * will treat it as an unrecoverable system error, just like k_panic().
3951 */
3952#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3953
3954/**
3955 * @brief Fatally terminate the system
3956 *
3957 * This should be called when the Zephyr kernel has encountered an
3958 * unrecoverable runtime condition and needs to terminate. What this ultimately
3959 * means is determined by the _fatal_error_handler() implementation, which
3960 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3961 */
3962#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3963
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964/*
3965 * private APIs that are utilized by one or more public APIs
3966 */
3967
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003968#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003969extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003970#else
3971#define _init_static_threads() do { } while ((0))
3972#endif
3973
3974extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003975extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976
Andrew Boiedc5d9352017-06-02 12:56:47 -07003977/* arch/cpu.h may declare an architecture or platform-specific macro
3978 * for properly declaring stacks, compatible with MMU/MPU constraints if
3979 * enabled
3980 */
3981#ifdef _ARCH_THREAD_STACK_DEFINE
3982#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3983#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3984 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3985#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3986#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003987static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3988{
3989 return _ARCH_THREAD_STACK_BUFFER(sym);
3990}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003991#else
3992/**
3993 * @brief Declare a toplevel thread stack memory region
3994 *
3995 * This declares a region of memory suitable for use as a thread's stack.
3996 *
3997 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3998 * 'noinit' section so that it isn't zeroed at boot
3999 *
Andrew Boie507852a2017-07-25 18:47:07 -07004000 * The declared symbol will always be a k_thread_stack_t which can be passed to
4001 * k_thread_create, but should otherwise not be manipulated. If the buffer
4002 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004003 *
4004 * It is legal to precede this definition with the 'static' keyword.
4005 *
4006 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4007 * parameter of k_thread_create(), it may not be the same as the
4008 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4009 *
4010 * @param sym Thread stack symbol name
4011 * @param size Size of the stack memory region
4012 */
4013#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004014 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004015
4016/**
4017 * @brief Declare a toplevel array of thread stack memory regions
4018 *
4019 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4020 * definition for additional details and constraints.
4021 *
4022 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4023 * 'noinit' section so that it isn't zeroed at boot
4024 *
4025 * @param sym Thread stack symbol name
4026 * @param nmemb Number of stacks to declare
4027 * @param size Size of the stack memory region
4028 */
4029
4030#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004031 struct _k_thread_stack_element __noinit \
4032 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004033
4034/**
4035 * @brief Declare an embedded stack memory region
4036 *
4037 * Used for stacks embedded within other data structures. Use is highly
4038 * discouraged but in some cases necessary. For memory protection scenarios,
4039 * it is very important that any RAM preceding this member not be writable
4040 * by threads else a stack overflow will lead to silent corruption. In other
4041 * words, the containing data structure should live in RAM owned by the kernel.
4042 *
4043 * @param sym Thread stack symbol name
4044 * @param size Size of the stack memory region
4045 */
4046#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004047 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004048
4049/**
4050 * @brief Return the size in bytes of a stack memory region
4051 *
4052 * Convenience macro for passing the desired stack size to k_thread_create()
4053 * since the underlying implementation may actually create something larger
4054 * (for instance a guard area).
4055 *
4056 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004057 * passed to K_THREAD_STACK_DEFINE.
4058 *
4059 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4060 * it is not guaranteed to return the original value since each array
4061 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004062 *
4063 * @param sym Stack memory symbol
4064 * @return Size of the stack
4065 */
4066#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4067
4068/**
4069 * @brief Get a pointer to the physical stack buffer
4070 *
4071 * Convenience macro to get at the real underlying stack buffer used by
4072 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4073 * This is really only intended for diagnostic tools which want to examine
4074 * stack memory contents.
4075 *
4076 * @param sym Declared stack symbol name
4077 * @return The buffer itself, a char *
4078 */
Andrew Boie507852a2017-07-25 18:47:07 -07004079static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4080{
4081 return (char *)sym;
4082}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004083
4084#endif /* _ARCH_DECLARE_STACK */
4085
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004086#ifdef __cplusplus
4087}
4088#endif
4089
Andrew Boiee004dec2016-11-07 09:01:19 -08004090#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4091/*
4092 * Define new and delete operators.
4093 * At this moment, the operators do nothing since objects are supposed
4094 * to be statically allocated.
4095 */
4096inline void operator delete(void *ptr)
4097{
4098 (void)ptr;
4099}
4100
4101inline void operator delete[](void *ptr)
4102{
4103 (void)ptr;
4104}
4105
4106inline void *operator new(size_t size)
4107{
4108 (void)size;
4109 return NULL;
4110}
4111
4112inline void *operator new[](size_t size)
4113{
4114 (void)size;
4115 return NULL;
4116}
4117
4118/* Placement versions of operator new and delete */
4119inline void operator delete(void *ptr1, void *ptr2)
4120{
4121 (void)ptr1;
4122 (void)ptr2;
4123}
4124
4125inline void operator delete[](void *ptr1, void *ptr2)
4126{
4127 (void)ptr1;
4128 (void)ptr2;
4129}
4130
4131inline void *operator new(size_t size, void *ptr)
4132{
4133 (void)size;
4134 return ptr;
4135}
4136
4137inline void *operator new[](size_t size, void *ptr)
4138{
4139 (void)size;
4140 return ptr;
4141}
4142
4143#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4144
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004145#endif /* !_ASMLANGUAGE */
4146
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004147#endif /* _kernel__h_ */