blob: 716e7b16c7b8cf2d5ac0715744abbd66ebd414db [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031
32#ifdef __cplusplus
33extern "C" {
34#endif
35
Anas Nashifbbb157d2017-01-15 08:46:31 -050036/**
37 * @brief Kernel APIs
38 * @defgroup kernel_apis Kernel APIs
39 * @{
40 * @}
41 */
42
Anas Nashif61f4b242016-11-18 10:53:59 -050043#ifdef CONFIG_KERNEL_DEBUG
44#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040045#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
46#else
47#define K_DEBUG(fmt, ...)
48#endif
49
Benjamin Walsh2f280412017-01-14 19:23:46 -050050#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#elif defined(CONFIG_COOP_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
55#define _NUM_PREEMPT_PRIO (0)
56#elif defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (0)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#else
60#error "invalid configuration"
61#endif
62
63#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_PRIO_PREEMPT(x) (x)
65
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_ANY NULL
67#define K_END NULL
68
Benjamin Walshedb35702017-01-14 18:47:22 -050069#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050071#elif defined(CONFIG_COOP_ENABLED)
72#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
73#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050075#else
76#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#endif
78
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050079#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
81#else
82#define K_LOWEST_THREAD_PRIO -1
83#endif
84
Benjamin Walshfab8d922016-11-08 15:36:36 -050085#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
86
Benjamin Walsh456c6da2016-09-02 18:55:39 -040087#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
88#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
89
90typedef sys_dlist_t _wait_q_t;
91
Anas Nashif2f203c22016-12-18 06:57:45 -050092#ifdef CONFIG_OBJECT_TRACING
93#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
94#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095#else
Anas Nashif2f203c22016-12-18 06:57:45 -050096#define _OBJECT_TRACING_INIT
97#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#endif
99
Benjamin Walshacc68c12017-01-29 18:57:45 -0500100#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300101#define _POLL_EVENT_OBJ_INIT(obj) \
102 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
103#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500104#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300105#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#define _POLL_EVENT
107#endif
108
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500109struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110struct k_mutex;
111struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400112struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_msgq;
114struct k_mbox;
115struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200116struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_fifo;
118struct k_lifo;
119struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400120struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mem_pool;
122struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500123struct k_poll_event;
124struct k_poll_signal;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125
Andrew Boie945af952017-08-22 13:15:23 -0700126enum k_objects {
127 K_OBJ_ALERT,
128 K_OBJ_DELAYED_WORK,
129 K_OBJ_MEM_SLAB,
130 K_OBJ_MSGQ,
131 K_OBJ_MUTEX,
132 K_OBJ_PIPE,
133 K_OBJ_SEM,
134 K_OBJ_STACK,
135 K_OBJ_THREAD,
136 K_OBJ_TIMER,
137 K_OBJ_WORK,
138 K_OBJ_WORK_Q,
139
140 K_OBJ_LAST
141};
142
143#ifdef CONFIG_USERSPACE
144/* Table generated by gperf, these objects are retrieved via
145 * _k_object_find() */
146struct _k_object {
147 char *name;
148 char perms[CONFIG_MAX_THREAD_BYTES];
149 char type;
150 char flags;
151} __packed;
152
153#define K_OBJ_FLAG_INITIALIZED BIT(0)
154/**
155 * Ensure a system object is a valid object of the expected type
156 *
157 * Searches for the object and ensures that it is indeed an object
158 * of the expected type, that the caller has the right permissions on it,
159 * and that the object has been initialized.
160 *
161 * This function is intended to be called on the kernel-side system
162 * call handlers to validate kernel object pointers passed in from
163 * userspace.
164 *
165 * @param obj Address of the kernel object
166 * @param otype Expected type of the kernel object
167 * @param init If true, this is for an init function and we will not error
168 * out if the object is not initialized
169 * @return 0 If the object is valid
170 * -EBADF if not a valid object of the specified type
171 * -EPERM If the caller does not have permissions
172 * -EINVAL Object is not intitialized
173 */
174int _k_object_validate(void *obj, enum k_objects otype, int init);
175
176
177/**
178 * Lookup a kernel object and init its metadata if it exists
179 *
180 * Calling this on an object will make it usable from userspace.
181 * Intended to be called as the last statement in kernel object init
182 * functions.
183 *
184 * @param object Address of the kernel object
185 */
186void _k_object_init(void *obj);
187
188
189/**
190 * grant a thread access to a kernel object
191 *
192 * The thread will be granted access to the object if the caller is from
193 * supervisor mode, or the caller is from user mode AND has permissions
194 * on the object already.
195 *
196 * @param object Address of kernel object
197 * @param thread Thread to grant access to the object
198 */
199void k_object_grant_access(void *object, struct k_thread *thread);
200
201#else
202static inline int _k_object_validate(void *obj, enum k_objects otype, int init)
203{
204 ARG_UNUSED(obj);
205 ARG_UNUSED(otype);
206 ARG_UNUSED(init);
207
208 return 0;
209}
210
211static inline void _k_object_init(void *obj)
212{
213 ARG_UNUSED(obj);
214}
215
216static inline void k_object_grant_access(void *object, struct k_thread *thread)
217{
218 ARG_UNUSED(object);
219 ARG_UNUSED(thread);
220}
221#endif /* CONFIG_USERSPACE */
222
Andrew Boie73abd322017-04-04 13:19:13 -0700223/* timeouts */
224
225struct _timeout;
226typedef void (*_timeout_func_t)(struct _timeout *t);
227
228struct _timeout {
229 sys_dnode_t node;
230 struct k_thread *thread;
231 sys_dlist_t *wait_q;
232 s32_t delta_ticks_from_prev;
233 _timeout_func_t func;
234};
235
236extern s32_t _timeout_remaining_get(struct _timeout *timeout);
237
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700238/**
239 * @typedef k_thread_entry_t
240 * @brief Thread entry point function type.
241 *
242 * A thread's entry point function is invoked when the thread starts executing.
243 * Up to 3 argument values can be passed to the function.
244 *
245 * The thread terminates execution permanently if the entry point function
246 * returns. The thread is responsible for releasing any shared resources
247 * it may own (such as mutexes and dynamically allocated memory), prior to
248 * returning.
249 *
250 * @param p1 First argument.
251 * @param p2 Second argument.
252 * @param p3 Third argument.
253 *
254 * @return N/A
255 */
256typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700257
258#ifdef CONFIG_THREAD_MONITOR
259struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700260 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700261 void *parameter1;
262 void *parameter2;
263 void *parameter3;
264};
265#endif
266
267/* can be used for creating 'dummy' threads, e.g. for pending on objects */
268struct _thread_base {
269
270 /* this thread's entry in a ready/wait queue */
271 sys_dnode_t k_q_node;
272
273 /* user facing 'thread options'; values defined in include/kernel.h */
274 u8_t user_options;
275
276 /* thread state */
277 u8_t thread_state;
278
279 /*
280 * scheduler lock count and thread priority
281 *
282 * These two fields control the preemptibility of a thread.
283 *
284 * When the scheduler is locked, sched_locked is decremented, which
285 * means that the scheduler is locked for values from 0xff to 0x01. A
286 * thread is coop if its prio is negative, thus 0x80 to 0xff when
287 * looked at the value as unsigned.
288 *
289 * By putting them end-to-end, this means that a thread is
290 * non-preemptible if the bundled value is greater than or equal to
291 * 0x0080.
292 */
293 union {
294 struct {
295#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
296 u8_t sched_locked;
297 s8_t prio;
298#else /* LITTLE and PDP */
299 s8_t prio;
300 u8_t sched_locked;
301#endif
302 };
303 u16_t preempt;
304 };
305
306 /* data returned by APIs */
307 void *swap_data;
308
309#ifdef CONFIG_SYS_CLOCK_EXISTS
310 /* this thread's entry in a timeout queue */
311 struct _timeout timeout;
312#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700313
314#ifdef CONFIG_USERSPACE
315 /* Bit position in kernel object permissions bitfield for this thread */
316 unsigned int perm_index;
317#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700318};
319
320typedef struct _thread_base _thread_base_t;
321
322#if defined(CONFIG_THREAD_STACK_INFO)
323/* Contains the stack information of a thread */
324struct _thread_stack_info {
325 /* Stack Start */
326 u32_t start;
327 /* Stack Size */
328 u32_t size;
329};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700330
331typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700332#endif /* CONFIG_THREAD_STACK_INFO */
333
334struct k_thread {
335
336 struct _thread_base base;
337
338 /* defined by the architecture, but all archs need these */
339 struct _caller_saved caller_saved;
340 struct _callee_saved callee_saved;
341
342 /* static thread init data */
343 void *init_data;
344
345 /* abort function */
346 void (*fn_abort)(void);
347
348#if defined(CONFIG_THREAD_MONITOR)
349 /* thread entry and parameters description */
350 struct __thread_entry *entry;
351
352 /* next item in list of all threads */
353 struct k_thread *next_thread;
354#endif
355
356#ifdef CONFIG_THREAD_CUSTOM_DATA
357 /* crude thread-local storage */
358 void *custom_data;
359#endif
360
361#ifdef CONFIG_ERRNO
362 /* per-thread errno variable */
363 int errno_var;
364#endif
365
366#if defined(CONFIG_THREAD_STACK_INFO)
367 /* Stack Info */
368 struct _thread_stack_info stack_info;
369#endif /* CONFIG_THREAD_STACK_INFO */
370
371 /* arch-specifics: must always be at the end */
372 struct _thread_arch arch;
373};
374
375typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400376typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700377#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400378
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400379enum execution_context_types {
380 K_ISR = 0,
381 K_COOP_THREAD,
382 K_PREEMPT_THREAD,
383};
384
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400385/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100386 * @defgroup profiling_apis Profiling APIs
387 * @ingroup kernel_apis
388 * @{
389 */
390
391/**
392 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
393 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700394 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100395 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
396 *
397 * CONFIG_MAIN_STACK_SIZE
398 * CONFIG_IDLE_STACK_SIZE
399 * CONFIG_ISR_STACK_SIZE
400 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
401 *
402 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
403 * produce output.
404 *
405 * @return N/A
406 */
407extern void k_call_stacks_analyze(void);
408
409/**
410 * @} end defgroup profiling_apis
411 */
412
413/**
Allan Stephensc98da842016-11-11 15:45:03 -0500414 * @defgroup thread_apis Thread APIs
415 * @ingroup kernel_apis
416 * @{
417 */
418
Benjamin Walshed240f22017-01-22 13:05:08 -0500419#endif /* !_ASMLANGUAGE */
420
421
422/*
423 * Thread user options. May be needed by assembly code. Common part uses low
424 * bits, arch-specific use high bits.
425 */
426
427/* system thread that must not abort */
428#define K_ESSENTIAL (1 << 0)
429
430#if defined(CONFIG_FP_SHARING)
431/* thread uses floating point registers */
432#define K_FP_REGS (1 << 1)
433#endif
434
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700435/* This thread has dropped from supervisor mode to user mode and consequently
436 * has additional restrictions
437 */
438#define K_USER (1 << 2)
439
Benjamin Walshed240f22017-01-22 13:05:08 -0500440#ifdef CONFIG_X86
441/* x86 Bitmask definitions for threads user options */
442
443#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
444/* thread uses SSEx (and also FP) registers */
445#define K_SSE_REGS (1 << 7)
446#endif
447#endif
448
449/* end - thread options */
450
451#if !defined(_ASMLANGUAGE)
452
Andrew Boie507852a2017-07-25 18:47:07 -0700453/* Using typedef deliberately here, this is quite intended to be an opaque
454 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
455 *
456 * The purpose of this data type is to clearly distinguish between the
457 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
458 * buffer which composes the stack data actually used by the underlying
459 * thread; they cannot be used interchangably as some arches precede the
460 * stack buffer region with guard areas that trigger a MPU or MMU fault
461 * if written to.
462 *
463 * APIs that want to work with the buffer inside should continue to use
464 * char *.
465 *
466 * Stacks should always be created with K_THREAD_STACK_DEFINE().
467 */
468struct __packed _k_thread_stack_element {
469 char data;
470};
471typedef struct _k_thread_stack_element *k_thread_stack_t;
472
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400473
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400474/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700475 * @brief Create a thread.
476 *
477 * This routine initializes a thread, then schedules it for execution.
478 *
479 * The new thread may be scheduled for immediate execution or a delayed start.
480 * If the newly spawned thread does not have a delayed start the kernel
481 * scheduler may preempt the current thread to allow the new thread to
482 * execute.
483 *
484 * Thread options are architecture-specific, and can include K_ESSENTIAL,
485 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
486 * them using "|" (the logical OR operator).
487 *
488 * Historically, users often would use the beginning of the stack memory region
489 * to store the struct k_thread data, although corruption will occur if the
490 * stack overflows this region and stack protection features may not detect this
491 * situation.
492 *
493 * @param new_thread Pointer to uninitialized struct k_thread
494 * @param stack Pointer to the stack space.
495 * @param stack_size Stack size in bytes.
496 * @param entry Thread entry function.
497 * @param p1 1st entry point parameter.
498 * @param p2 2nd entry point parameter.
499 * @param p3 3rd entry point parameter.
500 * @param prio Thread priority.
501 * @param options Thread options.
502 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
503 *
504 * @return ID of new thread.
505 */
Andrew Boie507852a2017-07-25 18:47:07 -0700506extern k_tid_t k_thread_create(struct k_thread *new_thread,
507 k_thread_stack_t stack,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700508 size_t stack_size,
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700509 k_thread_entry_t entry,
Andrew Boied26cf2d2017-03-30 13:07:02 -0700510 void *p1, void *p2, void *p3,
511 int prio, u32_t options, s32_t delay);
512
513/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500514 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400515 *
Allan Stephensc98da842016-11-11 15:45:03 -0500516 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500517 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500519 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400520 *
521 * @return N/A
522 */
Kumar Galacc334c72017-04-21 10:55:34 -0500523extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400524
525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500526 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400527 *
528 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400530 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400531 * @return N/A
532 */
Kumar Galacc334c72017-04-21 10:55:34 -0500533extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534
535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500536 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500538 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400539 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500540 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 *
542 * @return N/A
543 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400544extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400545
546/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500547 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500549 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500551 * If @a thread is not currently sleeping, the routine has no effect.
552 *
553 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400554 *
555 * @return N/A
556 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400557extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558
559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500560 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500562 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400563 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400564extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400565
566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500567 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500569 * This routine prevents @a thread from executing if it has not yet started
570 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500572 * @param thread ID of thread to cancel.
573 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700574 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500575 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400576 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400577extern int k_thread_cancel(k_tid_t thread);
578
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400579/**
Allan Stephensc98da842016-11-11 15:45:03 -0500580 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500582 * This routine permanently stops execution of @a thread. The thread is taken
583 * off all kernel queues it is part of (i.e. the ready queue, the timeout
584 * queue, or a kernel object wait queue). However, any kernel resources the
585 * thread might currently own (such as mutexes or memory blocks) are not
586 * released. It is the responsibility of the caller of this routine to ensure
587 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500589 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 *
591 * @return N/A
592 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593extern void k_thread_abort(k_tid_t thread);
594
Andrew Boie7d627c52017-08-30 11:01:56 -0700595
596/**
597 * @brief Start an inactive thread
598 *
599 * If a thread was created with K_FOREVER in the delay parameter, it will
600 * not be added to the scheduling queue until this function is called
601 * on it.
602 *
603 * @param thread thread to start
604 */
605extern void k_thread_start(k_tid_t thread);
606
Allan Stephensc98da842016-11-11 15:45:03 -0500607/**
608 * @cond INTERNAL_HIDDEN
609 */
610
Benjamin Walshd211a522016-12-06 11:44:01 -0500611/* timeout has timed out and is not on _timeout_q anymore */
612#define _EXPIRED (-2)
613
614/* timeout is not in use */
615#define _INACTIVE (-1)
616
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400617struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700618 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700619 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400620 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700621 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500622 void *init_p1;
623 void *init_p2;
624 void *init_p3;
625 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500626 u32_t init_options;
627 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500628 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500629 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400630};
631
Andrew Boied26cf2d2017-03-30 13:07:02 -0700632#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400633 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500634 prio, options, delay, abort, groups) \
635 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700636 .init_thread = (thread), \
637 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500638 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700639 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400640 .init_p1 = (void *)p1, \
641 .init_p2 = (void *)p2, \
642 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500643 .init_prio = (prio), \
644 .init_options = (options), \
645 .init_delay = (delay), \
646 .init_abort = (abort), \
647 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400648 }
649
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400650/**
Allan Stephensc98da842016-11-11 15:45:03 -0500651 * INTERNAL_HIDDEN @endcond
652 */
653
654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500655 * @brief Statically define and initialize a thread.
656 *
657 * The thread may be scheduled for immediate execution or a delayed start.
658 *
659 * Thread options are architecture-specific, and can include K_ESSENTIAL,
660 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
661 * them using "|" (the logical OR operator).
662 *
663 * The ID of the thread can be accessed using:
664 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500665 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500666 *
667 * @param name Name of the thread.
668 * @param stack_size Stack size in bytes.
669 * @param entry Thread entry function.
670 * @param p1 1st entry point parameter.
671 * @param p2 2nd entry point parameter.
672 * @param p3 3rd entry point parameter.
673 * @param prio Thread priority.
674 * @param options Thread options.
675 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400676 *
677 * @internal It has been observed that the x86 compiler by default aligns
678 * these _static_thread_data structures to 32-byte boundaries, thereby
679 * wasting space. To work around this, force a 4-byte alignment.
680 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500681#define K_THREAD_DEFINE(name, stack_size, \
682 entry, p1, p2, p3, \
683 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700684 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700685 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500686 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500687 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700688 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
689 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500690 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700691 NULL, 0); \
692 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400693
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500695 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500697 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500699 * @param thread ID of thread whose priority is needed.
700 *
701 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500703extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704
705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500706 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500708 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400709 *
710 * Rescheduling can occur immediately depending on the priority @a thread is
711 * set to:
712 *
713 * - If its priority is raised above the priority of the caller of this
714 * function, and the caller is preemptible, @a thread will be scheduled in.
715 *
716 * - If the caller operates on itself, it lowers its priority below that of
717 * other threads in the system, and the caller is preemptible, the thread of
718 * highest priority will be scheduled in.
719 *
720 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
721 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
722 * highest priority.
723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500724 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400725 * @param prio New priority.
726 *
727 * @warning Changing the priority of a thread currently involved in mutex
728 * priority inheritance may result in undefined behavior.
729 *
730 * @return N/A
731 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400732extern void k_thread_priority_set(k_tid_t thread, int prio);
733
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400734/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500735 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500737 * This routine prevents the kernel scheduler from making @a thread the
738 * current thread. All other internal operations on @a thread are still
739 * performed; for example, any timeout it is waiting on keeps ticking,
740 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500742 * If @a thread is already suspended, the routine has no effect.
743 *
744 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745 *
746 * @return N/A
747 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400748extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400749
750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500751 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500753 * This routine allows the kernel scheduler to make @a thread the current
754 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500756 * If @a thread is not currently suspended, the routine has no effect.
757 *
758 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 *
760 * @return N/A
761 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400762extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400763
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500765 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500767 * This routine specifies how the scheduler will perform time slicing of
768 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * To enable time slicing, @a slice must be non-zero. The scheduler
771 * ensures that no thread runs for more than the specified time limit
772 * before other threads of that priority are given a chance to execute.
773 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700774 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 * execute. Once the scheduler selects a thread for execution, there is no
778 * minimum guaranteed time the thread will execute before threads of greater or
779 * equal priority are scheduled.
780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 * for execution, this routine has no effect; the thread is immediately
783 * rescheduled after the slice period expires.
784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500785 * To disable timeslicing, set both @a slice and @a prio to zero.
786 *
787 * @param slice Maximum time slice length (in milliseconds).
788 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
790 * @return N/A
791 */
Kumar Galacc334c72017-04-21 10:55:34 -0500792extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400793
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794/**
Allan Stephensc98da842016-11-11 15:45:03 -0500795 * @} end defgroup thread_apis
796 */
797
798/**
799 * @addtogroup isr_apis
800 * @{
801 */
802
803/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
Allan Stephensc98da842016-11-11 15:45:03 -0500806 * This routine allows the caller to customize its actions, depending on
807 * whether it is a thread or an ISR.
808 *
809 * @note Can be called by ISRs.
810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @return 0 if invoked by a thread.
812 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500814extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400815
Benjamin Walsh445830d2016-11-10 15:54:27 -0500816/**
817 * @brief Determine if code is running in a preemptible thread.
818 *
Allan Stephensc98da842016-11-11 15:45:03 -0500819 * This routine allows the caller to customize its actions, depending on
820 * whether it can be preempted by another thread. The routine returns a 'true'
821 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500822 *
Allan Stephensc98da842016-11-11 15:45:03 -0500823 * - The code is running in a thread, not at ISR.
824 * - The thread's priority is in the preemptible range.
825 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500826 *
Allan Stephensc98da842016-11-11 15:45:03 -0500827 * @note Can be called by ISRs.
828 *
829 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500830 * @return Non-zero if invoked by a preemptible thread.
831 */
832extern int k_is_preempt_thread(void);
833
Allan Stephensc98da842016-11-11 15:45:03 -0500834/**
835 * @} end addtogroup isr_apis
836 */
837
838/**
839 * @addtogroup thread_apis
840 * @{
841 */
842
843/**
844 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500845 *
Allan Stephensc98da842016-11-11 15:45:03 -0500846 * This routine prevents the current thread from being preempted by another
847 * thread by instructing the scheduler to treat it as a cooperative thread.
848 * If the thread subsequently performs an operation that makes it unready,
849 * it will be context switched out in the normal manner. When the thread
850 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500851 *
Allan Stephensc98da842016-11-11 15:45:03 -0500852 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500853 *
Allan Stephensc98da842016-11-11 15:45:03 -0500854 * @note k_sched_lock() and k_sched_unlock() should normally be used
855 * when the operation being performed can be safely interrupted by ISRs.
856 * However, if the amount of processing involved is very small, better
857 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500858 *
859 * @return N/A
860 */
861extern void k_sched_lock(void);
862
Allan Stephensc98da842016-11-11 15:45:03 -0500863/**
864 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500865 *
Allan Stephensc98da842016-11-11 15:45:03 -0500866 * This routine reverses the effect of a previous call to k_sched_lock().
867 * A thread must call the routine once for each time it called k_sched_lock()
868 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500869 *
870 * @return N/A
871 */
872extern void k_sched_unlock(void);
873
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * Custom data is not used by the kernel itself, and is freely available
880 * for a thread to use as it sees fit. It can be used as a framework
881 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
885 * @return N/A
886 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400887extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888
889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500894 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400895 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400896extern void *k_thread_custom_data_get(void);
897
898/**
Allan Stephensc98da842016-11-11 15:45:03 -0500899 * @} end addtogroup thread_apis
900 */
901
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400902#include <sys_clock.h>
903
Allan Stephensc2f15a42016-11-17 12:24:22 -0500904/**
905 * @addtogroup clock_apis
906 * @{
907 */
908
909/**
910 * @brief Generate null timeout delay.
911 *
912 * This macro generates a timeout delay that that instructs a kernel API
913 * not to wait if the requested operation cannot be performed immediately.
914 *
915 * @return Timeout delay value.
916 */
917#define K_NO_WAIT 0
918
919/**
920 * @brief Generate timeout delay from milliseconds.
921 *
922 * This macro generates a timeout delay that that instructs a kernel API
923 * to wait up to @a ms milliseconds to perform the requested operation.
924 *
925 * @param ms Duration in milliseconds.
926 *
927 * @return Timeout delay value.
928 */
Johan Hedberg14471692016-11-13 10:52:15 +0200929#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500930
931/**
932 * @brief Generate timeout delay from seconds.
933 *
934 * This macro generates a timeout delay that that instructs a kernel API
935 * to wait up to @a s seconds to perform the requested operation.
936 *
937 * @param s Duration in seconds.
938 *
939 * @return Timeout delay value.
940 */
Johan Hedberg14471692016-11-13 10:52:15 +0200941#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500942
943/**
944 * @brief Generate timeout delay from minutes.
945 *
946 * This macro generates a timeout delay that that instructs a kernel API
947 * to wait up to @a m minutes to perform the requested operation.
948 *
949 * @param m Duration in minutes.
950 *
951 * @return Timeout delay value.
952 */
Johan Hedberg14471692016-11-13 10:52:15 +0200953#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500954
955/**
956 * @brief Generate timeout delay from hours.
957 *
958 * This macro generates a timeout delay that that instructs a kernel API
959 * to wait up to @a h hours to perform the requested operation.
960 *
961 * @param h Duration in hours.
962 *
963 * @return Timeout delay value.
964 */
Johan Hedberg14471692016-11-13 10:52:15 +0200965#define K_HOURS(h) K_MINUTES((h) * 60)
966
Allan Stephensc98da842016-11-11 15:45:03 -0500967/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500968 * @brief Generate infinite timeout delay.
969 *
970 * This macro generates a timeout delay that that instructs a kernel API
971 * to wait as long as necessary to perform the requested operation.
972 *
973 * @return Timeout delay value.
974 */
975#define K_FOREVER (-1)
976
977/**
978 * @} end addtogroup clock_apis
979 */
980
981/**
Allan Stephensc98da842016-11-11 15:45:03 -0500982 * @cond INTERNAL_HIDDEN
983 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400984
Benjamin Walsh62092182016-12-20 14:39:08 -0500985/* kernel clocks */
986
987#if (sys_clock_ticks_per_sec == 1000) || \
988 (sys_clock_ticks_per_sec == 500) || \
989 (sys_clock_ticks_per_sec == 250) || \
990 (sys_clock_ticks_per_sec == 125) || \
991 (sys_clock_ticks_per_sec == 100) || \
992 (sys_clock_ticks_per_sec == 50) || \
993 (sys_clock_ticks_per_sec == 25) || \
994 (sys_clock_ticks_per_sec == 20) || \
995 (sys_clock_ticks_per_sec == 10) || \
996 (sys_clock_ticks_per_sec == 1)
997
998 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
999#else
1000 /* yields horrible 64-bit math on many architectures: try to avoid */
1001 #define _NON_OPTIMIZED_TICKS_PER_SEC
1002#endif
1003
1004#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001005extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001006#else
Kumar Galacc334c72017-04-21 10:55:34 -05001007static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001008{
Kumar Galacc334c72017-04-21 10:55:34 -05001009 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001010}
1011#endif
1012
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001013/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001014#ifdef CONFIG_TICKLESS_KERNEL
1015#define _TICK_ALIGN 0
1016#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001017#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001018#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001019
Kumar Galacc334c72017-04-21 10:55:34 -05001020static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001021{
Benjamin Walsh62092182016-12-20 14:39:08 -05001022#ifdef CONFIG_SYS_CLOCK_EXISTS
1023
1024#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001025 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001026#else
Kumar Galacc334c72017-04-21 10:55:34 -05001027 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001028#endif
1029
1030#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001031 __ASSERT(ticks == 0, "");
1032 return 0;
1033#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001034}
1035
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001036struct k_timer {
1037 /*
1038 * _timeout structure must be first here if we want to use
1039 * dynamic timer allocation. timeout.node is used in the double-linked
1040 * list of free timers
1041 */
1042 struct _timeout timeout;
1043
Allan Stephens45bfa372016-10-12 12:39:42 -05001044 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001045 _wait_q_t wait_q;
1046
1047 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001048 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001049
1050 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001051 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001052
1053 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001054 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001055
Allan Stephens45bfa372016-10-12 12:39:42 -05001056 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001057 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001058
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001059 /* user-specific data, also used to support legacy features */
1060 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001061
Anas Nashif2f203c22016-12-18 06:57:45 -05001062 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063};
1064
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001065#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001067 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001068 .timeout.wait_q = NULL, \
1069 .timeout.thread = NULL, \
1070 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001071 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001072 .expiry_fn = expiry, \
1073 .stop_fn = stop, \
1074 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001075 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001076 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077 }
1078
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001079#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1080
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081/**
Allan Stephensc98da842016-11-11 15:45:03 -05001082 * INTERNAL_HIDDEN @endcond
1083 */
1084
1085/**
1086 * @defgroup timer_apis Timer APIs
1087 * @ingroup kernel_apis
1088 * @{
1089 */
1090
1091/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001092 * @typedef k_timer_expiry_t
1093 * @brief Timer expiry function type.
1094 *
1095 * A timer's expiry function is executed by the system clock interrupt handler
1096 * each time the timer expires. The expiry function is optional, and is only
1097 * invoked if the timer has been initialized with one.
1098 *
1099 * @param timer Address of timer.
1100 *
1101 * @return N/A
1102 */
1103typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1104
1105/**
1106 * @typedef k_timer_stop_t
1107 * @brief Timer stop function type.
1108 *
1109 * A timer's stop function is executed if the timer is stopped prematurely.
1110 * The function runs in the context of the thread that stops the timer.
1111 * The stop function is optional, and is only invoked if the timer has been
1112 * initialized with one.
1113 *
1114 * @param timer Address of timer.
1115 *
1116 * @return N/A
1117 */
1118typedef void (*k_timer_stop_t)(struct k_timer *timer);
1119
1120/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001121 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001125 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
1127 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001128 * @param expiry_fn Function to invoke each time the timer expires.
1129 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001131#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001132 struct k_timer name \
1133 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001134 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135
Allan Stephens45bfa372016-10-12 12:39:42 -05001136/**
1137 * @brief Initialize a timer.
1138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001140 *
1141 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001142 * @param expiry_fn Function to invoke each time the timer expires.
1143 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001144 *
1145 * @return N/A
1146 */
1147extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001148 k_timer_expiry_t expiry_fn,
1149 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001150
Allan Stephens45bfa372016-10-12 12:39:42 -05001151/**
1152 * @brief Start a timer.
1153 *
1154 * This routine starts a timer, and resets its status to zero. The timer
1155 * begins counting down using the specified duration and period values.
1156 *
1157 * Attempting to start a timer that is already running is permitted.
1158 * The timer's status is reset to zero and the timer begins counting down
1159 * using the new duration and period values.
1160 *
1161 * @param timer Address of timer.
1162 * @param duration Initial timer duration (in milliseconds).
1163 * @param period Timer period (in milliseconds).
1164 *
1165 * @return N/A
1166 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001167extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001168 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001169
1170/**
1171 * @brief Stop a timer.
1172 *
1173 * This routine stops a running timer prematurely. The timer's stop function,
1174 * if one exists, is invoked by the caller.
1175 *
1176 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001177 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001178 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001179 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1180 * if @a k_timer_stop is to be called from ISRs.
1181 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001182 * @param timer Address of timer.
1183 *
1184 * @return N/A
1185 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001186extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001187
1188/**
1189 * @brief Read timer status.
1190 *
1191 * This routine reads the timer's status, which indicates the number of times
1192 * it has expired since its status was last read.
1193 *
1194 * Calling this routine resets the timer's status to zero.
1195 *
1196 * @param timer Address of timer.
1197 *
1198 * @return Timer status.
1199 */
Kumar Galacc334c72017-04-21 10:55:34 -05001200extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001201
1202/**
1203 * @brief Synchronize thread to timer expiration.
1204 *
1205 * This routine blocks the calling thread until the timer's status is non-zero
1206 * (indicating that it has expired at least once since it was last examined)
1207 * or the timer is stopped. If the timer status is already non-zero,
1208 * or the timer is already stopped, the caller continues without waiting.
1209 *
1210 * Calling this routine resets the timer's status to zero.
1211 *
1212 * This routine must not be used by interrupt handlers, since they are not
1213 * allowed to block.
1214 *
1215 * @param timer Address of timer.
1216 *
1217 * @return Timer status.
1218 */
Kumar Galacc334c72017-04-21 10:55:34 -05001219extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001220
1221/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001222 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001223 *
1224 * This routine computes the (approximate) time remaining before a running
1225 * timer next expires. If the timer is not running, it returns zero.
1226 *
1227 * @param timer Address of timer.
1228 *
1229 * @return Remaining time (in milliseconds).
1230 */
Kumar Galacc334c72017-04-21 10:55:34 -05001231static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001232{
1233 return _timeout_remaining_get(&timer->timeout);
1234}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001235
Allan Stephensc98da842016-11-11 15:45:03 -05001236/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001237 * @brief Associate user-specific data with a timer.
1238 *
1239 * This routine records the @a user_data with the @a timer, to be retrieved
1240 * later.
1241 *
1242 * It can be used e.g. in a timer handler shared across multiple subsystems to
1243 * retrieve data specific to the subsystem this timer is associated with.
1244 *
1245 * @param timer Address of timer.
1246 * @param user_data User data to associate with the timer.
1247 *
1248 * @return N/A
1249 */
1250static inline void k_timer_user_data_set(struct k_timer *timer,
1251 void *user_data)
1252{
1253 timer->user_data = user_data;
1254}
1255
1256/**
1257 * @brief Retrieve the user-specific data from a timer.
1258 *
1259 * @param timer Address of timer.
1260 *
1261 * @return The user data.
1262 */
1263static inline void *k_timer_user_data_get(struct k_timer *timer)
1264{
1265 return timer->user_data;
1266}
1267
1268/**
Allan Stephensc98da842016-11-11 15:45:03 -05001269 * @} end defgroup timer_apis
1270 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001271
Allan Stephensc98da842016-11-11 15:45:03 -05001272/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001273 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001274 * @{
1275 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001276
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001277/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001278 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001280 * This routine returns the elapsed time since the system booted,
1281 * in milliseconds.
1282 *
1283 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001284 */
Kumar Galacc334c72017-04-21 10:55:34 -05001285extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001286
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001287#ifdef CONFIG_TICKLESS_KERNEL
1288/**
1289 * @brief Enable clock always on in tickless kernel
1290 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001291 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001292 * there are no timer events programmed in tickless kernel
1293 * scheduling. This is necessary if the clock is used to track
1294 * passage of time.
1295 *
1296 * @retval prev_status Previous status of always on flag
1297 */
1298static inline int k_enable_sys_clock_always_on(void)
1299{
1300 int prev_status = _sys_clock_always_on;
1301
1302 _sys_clock_always_on = 1;
1303 _enable_sys_clock();
1304
1305 return prev_status;
1306}
1307
1308/**
1309 * @brief Disable clock always on in tickless kernel
1310 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001311 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001312 * there are no timer events programmed in tickless kernel
1313 * scheduling. To save power, this routine should be called
1314 * immediately when clock is not used to track time.
1315 */
1316static inline void k_disable_sys_clock_always_on(void)
1317{
1318 _sys_clock_always_on = 0;
1319}
1320#else
1321#define k_enable_sys_clock_always_on() do { } while ((0))
1322#define k_disable_sys_clock_always_on() do { } while ((0))
1323#endif
1324
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001325/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001328 * This routine returns the lower 32-bits of the elapsed time since the system
1329 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001331 * This routine can be more efficient than k_uptime_get(), as it reduces the
1332 * need for interrupt locking and 64-bit math. However, the 32-bit result
1333 * cannot hold a system uptime time larger than approximately 50 days, so the
1334 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001336 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001337 */
Kumar Galacc334c72017-04-21 10:55:34 -05001338extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001339
1340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001341 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 * This routine computes the elapsed time between the current system uptime
1344 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001346 * @param reftime Pointer to a reference time, which is updated to the current
1347 * uptime upon return.
1348 *
1349 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001350 */
Kumar Galacc334c72017-04-21 10:55:34 -05001351extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001352
1353/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001354 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * This routine computes the elapsed time between the current system uptime
1357 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1360 * need for interrupt locking and 64-bit math. However, the 32-bit result
1361 * cannot hold an elapsed time larger than approximately 50 days, so the
1362 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001364 * @param reftime Pointer to a reference time, which is updated to the current
1365 * uptime upon return.
1366 *
1367 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001368 */
Kumar Galacc334c72017-04-21 10:55:34 -05001369extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001370
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001372 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001374 * This routine returns the current time, as measured by the system's hardware
1375 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001377 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001378 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001379#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001381/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001382 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001383 */
1384
Allan Stephensc98da842016-11-11 15:45:03 -05001385/**
1386 * @cond INTERNAL_HIDDEN
1387 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001389struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001390 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001391 union {
1392 _wait_q_t wait_q;
1393
1394 _POLL_EVENT;
1395 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001396
1397 _OBJECT_TRACING_NEXT_PTR(k_queue);
1398};
1399
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001400#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001401 { \
1402 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1403 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001404 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001405 _OBJECT_TRACING_INIT \
1406 }
1407
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001408#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1409
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001410/**
1411 * INTERNAL_HIDDEN @endcond
1412 */
1413
1414/**
1415 * @defgroup queue_apis Queue APIs
1416 * @ingroup kernel_apis
1417 * @{
1418 */
1419
1420/**
1421 * @brief Initialize a queue.
1422 *
1423 * This routine initializes a queue object, prior to its first use.
1424 *
1425 * @param queue Address of the queue.
1426 *
1427 * @return N/A
1428 */
1429extern void k_queue_init(struct k_queue *queue);
1430
1431/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001432 * @brief Cancel waiting on a queue.
1433 *
1434 * This routine causes first thread pending on @a queue, if any, to
1435 * return from k_queue_get() call with NULL value (as if timeout expired).
1436 *
1437 * @note Can be called by ISRs.
1438 *
1439 * @param queue Address of the queue.
1440 *
1441 * @return N/A
1442 */
1443extern void k_queue_cancel_wait(struct k_queue *queue);
1444
1445/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001446 * @brief Append an element to the end of a queue.
1447 *
1448 * This routine appends a data item to @a queue. A queue data item must be
1449 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1450 * reserved for the kernel's use.
1451 *
1452 * @note Can be called by ISRs.
1453 *
1454 * @param queue Address of the queue.
1455 * @param data Address of the data item.
1456 *
1457 * @return N/A
1458 */
1459extern void k_queue_append(struct k_queue *queue, void *data);
1460
1461/**
1462 * @brief Prepend an element to a queue.
1463 *
1464 * This routine prepends a data item to @a queue. A queue data item must be
1465 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1466 * reserved for the kernel's use.
1467 *
1468 * @note Can be called by ISRs.
1469 *
1470 * @param queue Address of the queue.
1471 * @param data Address of the data item.
1472 *
1473 * @return N/A
1474 */
1475extern void k_queue_prepend(struct k_queue *queue, void *data);
1476
1477/**
1478 * @brief Inserts an element to a queue.
1479 *
1480 * This routine inserts a data item to @a queue after previous item. A queue
1481 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1482 * item are reserved for the kernel's use.
1483 *
1484 * @note Can be called by ISRs.
1485 *
1486 * @param queue Address of the queue.
1487 * @param prev Address of the previous data item.
1488 * @param data Address of the data item.
1489 *
1490 * @return N/A
1491 */
1492extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1493
1494/**
1495 * @brief Atomically append a list of elements to a queue.
1496 *
1497 * This routine adds a list of data items to @a queue in one operation.
1498 * The data items must be in a singly-linked list, with the first 32 bits
1499 * in each data item pointing to the next data item; the list must be
1500 * NULL-terminated.
1501 *
1502 * @note Can be called by ISRs.
1503 *
1504 * @param queue Address of the queue.
1505 * @param head Pointer to first node in singly-linked list.
1506 * @param tail Pointer to last node in singly-linked list.
1507 *
1508 * @return N/A
1509 */
1510extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1511
1512/**
1513 * @brief Atomically add a list of elements to a queue.
1514 *
1515 * This routine adds a list of data items to @a queue in one operation.
1516 * The data items must be in a singly-linked list implemented using a
1517 * sys_slist_t object. Upon completion, the original list is empty.
1518 *
1519 * @note Can be called by ISRs.
1520 *
1521 * @param queue Address of the queue.
1522 * @param list Pointer to sys_slist_t object.
1523 *
1524 * @return N/A
1525 */
1526extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1527
1528/**
1529 * @brief Get an element from a queue.
1530 *
1531 * This routine removes first data item from @a queue. The first 32 bits of the
1532 * data item are reserved for the kernel's use.
1533 *
1534 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1535 *
1536 * @param queue Address of the queue.
1537 * @param timeout Waiting period to obtain a data item (in milliseconds),
1538 * or one of the special values K_NO_WAIT and K_FOREVER.
1539 *
1540 * @return Address of the data item if successful; NULL if returned
1541 * without waiting, or waiting period timed out.
1542 */
Kumar Galacc334c72017-04-21 10:55:34 -05001543extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001544
1545/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001546 * @brief Remove an element from a queue.
1547 *
1548 * This routine removes data item from @a queue. The first 32 bits of the
1549 * data item are reserved for the kernel's use. Removing elements from k_queue
1550 * rely on sys_slist_find_and_remove which is not a constant time operation.
1551 *
1552 * @note Can be called by ISRs
1553 *
1554 * @param queue Address of the queue.
1555 * @param data Address of the data item.
1556 *
1557 * @return true if data item was removed
1558 */
1559static inline bool k_queue_remove(struct k_queue *queue, void *data)
1560{
1561 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1562}
1563
1564/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001565 * @brief Query a queue to see if it has data available.
1566 *
1567 * Note that the data might be already gone by the time this function returns
1568 * if other threads are also trying to read from the queue.
1569 *
1570 * @note Can be called by ISRs.
1571 *
1572 * @param queue Address of the queue.
1573 *
1574 * @return Non-zero if the queue is empty.
1575 * @return 0 if data is available.
1576 */
1577static inline int k_queue_is_empty(struct k_queue *queue)
1578{
1579 return (int)sys_slist_is_empty(&queue->data_q);
1580}
1581
1582/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001583 * @brief Peek element at the head of queue.
1584 *
1585 * Return element from the head of queue without removing it.
1586 *
1587 * @param queue Address of the queue.
1588 *
1589 * @return Head element, or NULL if queue is empty.
1590 */
1591static inline void *k_queue_peek_head(struct k_queue *queue)
1592{
1593 return sys_slist_peek_head(&queue->data_q);
1594}
1595
1596/**
1597 * @brief Peek element at the tail of queue.
1598 *
1599 * Return element from the tail of queue without removing it.
1600 *
1601 * @param queue Address of the queue.
1602 *
1603 * @return Tail element, or NULL if queue is empty.
1604 */
1605static inline void *k_queue_peek_tail(struct k_queue *queue)
1606{
1607 return sys_slist_peek_tail(&queue->data_q);
1608}
1609
1610/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001611 * @brief Statically define and initialize a queue.
1612 *
1613 * The queue can be accessed outside the module where it is defined using:
1614 *
1615 * @code extern struct k_queue <name>; @endcode
1616 *
1617 * @param name Name of the queue.
1618 */
1619#define K_QUEUE_DEFINE(name) \
1620 struct k_queue name \
1621 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001622 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001623
1624/**
1625 * @} end defgroup queue_apis
1626 */
1627
1628/**
1629 * @cond INTERNAL_HIDDEN
1630 */
1631
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001632struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001633 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001634};
1635
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001636#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001637 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001638 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001639 }
1640
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001641#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1642
Allan Stephensc98da842016-11-11 15:45:03 -05001643/**
1644 * INTERNAL_HIDDEN @endcond
1645 */
1646
1647/**
1648 * @defgroup fifo_apis Fifo APIs
1649 * @ingroup kernel_apis
1650 * @{
1651 */
1652
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001654 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001658 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001659 *
1660 * @return N/A
1661 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001662#define k_fifo_init(fifo) \
1663 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001664
1665/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001666 * @brief Cancel waiting on a fifo.
1667 *
1668 * This routine causes first thread pending on @a fifo, if any, to
1669 * return from k_fifo_get() call with NULL value (as if timeout
1670 * expired).
1671 *
1672 * @note Can be called by ISRs.
1673 *
1674 * @param fifo Address of the fifo.
1675 *
1676 * @return N/A
1677 */
1678#define k_fifo_cancel_wait(fifo) \
1679 k_queue_cancel_wait((struct k_queue *) fifo)
1680
1681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001682 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001684 * This routine adds a data item to @a fifo. A fifo data item must be
1685 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1686 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * @note Can be called by ISRs.
1689 *
1690 * @param fifo Address of the fifo.
1691 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001692 *
1693 * @return N/A
1694 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001695#define k_fifo_put(fifo, data) \
1696 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001697
1698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * This routine adds a list of data items to @a fifo in one operation.
1702 * The data items must be in a singly-linked list, with the first 32 bits
1703 * each data item pointing to the next data item; the list must be
1704 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @param fifo Address of the fifo.
1709 * @param head Pointer to first node in singly-linked list.
1710 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711 *
1712 * @return N/A
1713 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001714#define k_fifo_put_list(fifo, head, tail) \
1715 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001716
1717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * This routine adds a list of data items to @a fifo in one operation.
1721 * The data items must be in a singly-linked list implemented using a
1722 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723 * and must be re-initialized via sys_slist_init().
1724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * @note Can be called by ISRs.
1726 *
1727 * @param fifo Address of the fifo.
1728 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729 *
1730 * @return N/A
1731 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001732#define k_fifo_put_slist(fifo, list) \
1733 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734
1735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * This routine removes a data item from @a fifo in a "first in, first out"
1739 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1742 *
1743 * @param fifo Address of the fifo.
1744 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001745 * or one of the special values K_NO_WAIT and K_FOREVER.
1746 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001747 * @return Address of the data item if successful; NULL if returned
1748 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001750#define k_fifo_get(fifo, timeout) \
1751 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001752
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001754 * @brief Query a fifo to see if it has data available.
1755 *
1756 * Note that the data might be already gone by the time this function returns
1757 * if other threads is also trying to read from the fifo.
1758 *
1759 * @note Can be called by ISRs.
1760 *
1761 * @param fifo Address of the fifo.
1762 *
1763 * @return Non-zero if the fifo is empty.
1764 * @return 0 if data is available.
1765 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001766#define k_fifo_is_empty(fifo) \
1767 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001768
1769/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001770 * @brief Peek element at the head of fifo.
1771 *
1772 * Return element from the head of fifo without removing it. A usecase
1773 * for this is if elements of the fifo are themselves containers. Then
1774 * on each iteration of processing, a head container will be peeked,
1775 * and some data processed out of it, and only if the container is empty,
1776 * it will be completely remove from the fifo.
1777 *
1778 * @param fifo Address of the fifo.
1779 *
1780 * @return Head element, or NULL if the fifo is empty.
1781 */
1782#define k_fifo_peek_head(fifo) \
1783 k_queue_peek_head((struct k_queue *) fifo)
1784
1785/**
1786 * @brief Peek element at the tail of fifo.
1787 *
1788 * Return element from the tail of fifo (without removing it). A usecase
1789 * for this is if elements of the fifo are themselves containers. Then
1790 * it may be useful to add more data to the last container in fifo.
1791 *
1792 * @param fifo Address of the fifo.
1793 *
1794 * @return Tail element, or NULL if fifo is empty.
1795 */
1796#define k_fifo_peek_tail(fifo) \
1797 k_queue_peek_tail((struct k_queue *) fifo)
1798
1799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001804 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001806 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001807 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001808#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001809 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001810 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001811 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001812
Allan Stephensc98da842016-11-11 15:45:03 -05001813/**
1814 * @} end defgroup fifo_apis
1815 */
1816
1817/**
1818 * @cond INTERNAL_HIDDEN
1819 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001820
1821struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001822 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823};
1824
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001825#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001826 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001827 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001828 }
1829
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001830#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1831
Allan Stephensc98da842016-11-11 15:45:03 -05001832/**
1833 * INTERNAL_HIDDEN @endcond
1834 */
1835
1836/**
1837 * @defgroup lifo_apis Lifo APIs
1838 * @ingroup kernel_apis
1839 * @{
1840 */
1841
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001843 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848 *
1849 * @return N/A
1850 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001851#define k_lifo_init(lifo) \
1852 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853
1854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001855 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001857 * This routine adds a data item to @a lifo. A lifo data item must be
1858 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1859 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001861 * @note Can be called by ISRs.
1862 *
1863 * @param lifo Address of the lifo.
1864 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865 *
1866 * @return N/A
1867 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001868#define k_lifo_put(lifo, data) \
1869 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870
1871/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001874 * This routine removes a data item from @a lifo in a "last in, first out"
1875 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001877 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1878 *
1879 * @param lifo Address of the lifo.
1880 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001881 * or one of the special values K_NO_WAIT and K_FOREVER.
1882 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001883 * @return Address of the data item if successful; NULL if returned
1884 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001885 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001886#define k_lifo_get(lifo, timeout) \
1887 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001888
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001890 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001892 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001893 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001894 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001897 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001898#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001899 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001900 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001901 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
Allan Stephensc98da842016-11-11 15:45:03 -05001903/**
1904 * @} end defgroup lifo_apis
1905 */
1906
1907/**
1908 * @cond INTERNAL_HIDDEN
1909 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001910
1911struct k_stack {
1912 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001913 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001914
Anas Nashif2f203c22016-12-18 06:57:45 -05001915 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916};
1917
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001918#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001919 { \
1920 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1921 .base = stack_buffer, \
1922 .next = stack_buffer, \
1923 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001924 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001925 }
1926
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001927#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1928
Allan Stephensc98da842016-11-11 15:45:03 -05001929/**
1930 * INTERNAL_HIDDEN @endcond
1931 */
1932
1933/**
1934 * @defgroup stack_apis Stack APIs
1935 * @ingroup kernel_apis
1936 * @{
1937 */
1938
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001939/**
1940 * @brief Initialize a stack.
1941 *
1942 * This routine initializes a stack object, prior to its first use.
1943 *
1944 * @param stack Address of the stack.
1945 * @param buffer Address of array used to hold stacked values.
1946 * @param num_entries Maximum number of values that can be stacked.
1947 *
1948 * @return N/A
1949 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001950extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001951 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001952
1953/**
1954 * @brief Push an element onto a stack.
1955 *
1956 * This routine adds a 32-bit value @a data to @a stack.
1957 *
1958 * @note Can be called by ISRs.
1959 *
1960 * @param stack Address of the stack.
1961 * @param data Value to push onto the stack.
1962 *
1963 * @return N/A
1964 */
Kumar Galacc334c72017-04-21 10:55:34 -05001965extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001966
1967/**
1968 * @brief Pop an element from a stack.
1969 *
1970 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1971 * manner and stores the value in @a data.
1972 *
1973 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1974 *
1975 * @param stack Address of the stack.
1976 * @param data Address of area to hold the value popped from the stack.
1977 * @param timeout Waiting period to obtain a value (in milliseconds),
1978 * or one of the special values K_NO_WAIT and K_FOREVER.
1979 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001980 * @retval 0 Element popped from stack.
1981 * @retval -EBUSY Returned without waiting.
1982 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 */
Kumar Galacc334c72017-04-21 10:55:34 -05001984extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001987 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001991 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * @param name Name of the stack.
1994 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04001996#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05001997 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04001998 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001999 struct k_stack name \
2000 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002001 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002002 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002003
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004/**
Allan Stephensc98da842016-11-11 15:45:03 -05002005 * @} end defgroup stack_apis
2006 */
2007
Allan Stephens6bba9b02016-11-16 14:56:54 -05002008struct k_work;
2009
Allan Stephensc98da842016-11-11 15:45:03 -05002010/**
2011 * @defgroup workqueue_apis Workqueue Thread APIs
2012 * @ingroup kernel_apis
2013 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014 */
2015
Allan Stephens6bba9b02016-11-16 14:56:54 -05002016/**
2017 * @typedef k_work_handler_t
2018 * @brief Work item handler function type.
2019 *
2020 * A work item's handler function is executed by a workqueue's thread
2021 * when the work item is processed by the workqueue.
2022 *
2023 * @param work Address of the work item.
2024 *
2025 * @return N/A
2026 */
2027typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028
2029/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002030 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002032
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002034 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002035 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002036};
2037
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002038enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002039 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002040};
2041
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002042struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002043 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044 k_work_handler_t handler;
2045 atomic_t flags[1];
2046};
2047
Allan Stephens6bba9b02016-11-16 14:56:54 -05002048struct k_delayed_work {
2049 struct k_work work;
2050 struct _timeout timeout;
2051 struct k_work_q *work_q;
2052};
2053
2054extern struct k_work_q k_sys_work_q;
2055
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002057 * INTERNAL_HIDDEN @endcond
2058 */
2059
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002060#define _K_WORK_INITIALIZER(work_handler) \
2061 { \
2062 ._reserved = NULL, \
2063 .handler = work_handler, \
2064 .flags = { 0 } \
2065 }
2066
2067#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2068
Allan Stephens6bba9b02016-11-16 14:56:54 -05002069/**
2070 * @brief Initialize a statically-defined work item.
2071 *
2072 * This macro can be used to initialize a statically-defined workqueue work
2073 * item, prior to its first use. For example,
2074 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002075 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002076 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002077 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002078 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002079 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002080#define K_WORK_DEFINE(work, work_handler) \
2081 struct k_work work \
2082 __in_section(_k_work, static, work) = \
2083 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002084
2085/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002086 * @brief Initialize a work item.
2087 *
2088 * This routine initializes a workqueue work item, prior to its first use.
2089 *
2090 * @param work Address of work item.
2091 * @param handler Function to invoke each time work item is processed.
2092 *
2093 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002094 */
2095static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2096{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002097 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002099 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002100}
2101
2102/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002103 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002104 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002105 * This routine submits work item @a work to be processed by workqueue
2106 * @a work_q. If the work item is already pending in the workqueue's queue
2107 * as a result of an earlier submission, this routine has no effect on the
2108 * work item. If the work item has already been processed, or is currently
2109 * being processed, its work is considered complete and the work item can be
2110 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002111 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002112 * @warning
2113 * A submitted work item must not be modified until it has been processed
2114 * by the workqueue.
2115 *
2116 * @note Can be called by ISRs.
2117 *
2118 * @param work_q Address of workqueue.
2119 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002120 *
2121 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122 */
2123static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2124 struct k_work *work)
2125{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002126 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002127 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128 }
2129}
2130
2131/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002132 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002133 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002134 * This routine indicates if work item @a work is pending in a workqueue's
2135 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002137 * @note Can be called by ISRs.
2138 *
2139 * @param work Address of work item.
2140 *
2141 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002142 */
2143static inline int k_work_pending(struct k_work *work)
2144{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002145 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002146}
2147
2148/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002149 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002151 * This routine starts workqueue @a work_q. The workqueue spawns its work
2152 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002153 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002154 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002155 * @param stack Pointer to work queue thread's stack space, as defined by
2156 * K_THREAD_STACK_DEFINE()
2157 * @param stack_size Size of the work queue thread's stack (in bytes), which
2158 * should either be the same constant passed to
2159 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002160 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161 *
2162 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002163 */
Andrew Boie507852a2017-07-25 18:47:07 -07002164extern void k_work_q_start(struct k_work_q *work_q,
2165 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002166 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002169 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002170 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002171 * This routine initializes a workqueue delayed work item, prior to
2172 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002174 * @param work Address of delayed work item.
2175 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
2177 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002178 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002179extern void k_delayed_work_init(struct k_delayed_work *work,
2180 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181
2182/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002183 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002184 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002185 * This routine schedules work item @a work to be processed by workqueue
2186 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002187 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002188 * Only when the countdown completes is the work item actually submitted to
2189 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002191 * Submitting a previously submitted delayed work item that is still
2192 * counting down cancels the existing submission and restarts the countdown
2193 * using the new delay. If the work item is currently pending on the
2194 * workqueue's queue because the countdown has completed it is too late to
2195 * resubmit the item, and resubmission fails without impacting the work item.
2196 * If the work item has already been processed, or is currently being processed,
2197 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002199 * @warning
2200 * A delayed work item must not be modified until it has been processed
2201 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002203 * @note Can be called by ISRs.
2204 *
2205 * @param work_q Address of workqueue.
2206 * @param work Address of delayed work item.
2207 * @param delay Delay before submitting the work item (in milliseconds).
2208 *
2209 * @retval 0 Work item countdown started.
2210 * @retval -EINPROGRESS Work item is already pending.
2211 * @retval -EINVAL Work item is being processed or has completed its work.
2212 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002214extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2215 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002216 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217
2218/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002219 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002221 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002222 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002223 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002225 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002227 * @param work Address of delayed work item.
2228 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002229 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002230 * @retval -EINPROGRESS Work item is already pending.
2231 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002233extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002235/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002236 * @brief Submit a work item to the system workqueue.
2237 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002238 * This routine submits work item @a work to be processed by the system
2239 * workqueue. If the work item is already pending in the workqueue's queue
2240 * as a result of an earlier submission, this routine has no effect on the
2241 * work item. If the work item has already been processed, or is currently
2242 * being processed, its work is considered complete and the work item can be
2243 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * @warning
2246 * Work items submitted to the system workqueue should avoid using handlers
2247 * that block or yield since this may prevent the system workqueue from
2248 * processing other work items in a timely manner.
2249 *
2250 * @note Can be called by ISRs.
2251 *
2252 * @param work Address of work item.
2253 *
2254 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002255 */
2256static inline void k_work_submit(struct k_work *work)
2257{
2258 k_work_submit_to_queue(&k_sys_work_q, work);
2259}
2260
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002261/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262 * @brief Submit a delayed work item to the system workqueue.
2263 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002264 * This routine schedules work item @a work to be processed by the system
2265 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002266 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * Only when the countdown completes is the work item actually submitted to
2268 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002270 * Submitting a previously submitted delayed work item that is still
2271 * counting down cancels the existing submission and restarts the countdown
2272 * using the new delay. If the work item is currently pending on the
2273 * workqueue's queue because the countdown has completed it is too late to
2274 * resubmit the item, and resubmission fails without impacting the work item.
2275 * If the work item has already been processed, or is currently being processed,
2276 * its work is considered complete and the work item can be resubmitted.
2277 *
2278 * @warning
2279 * Work items submitted to the system workqueue should avoid using handlers
2280 * that block or yield since this may prevent the system workqueue from
2281 * processing other work items in a timely manner.
2282 *
2283 * @note Can be called by ISRs.
2284 *
2285 * @param work Address of delayed work item.
2286 * @param delay Delay before submitting the work item (in milliseconds).
2287 *
2288 * @retval 0 Work item countdown started.
2289 * @retval -EINPROGRESS Work item is already pending.
2290 * @retval -EINVAL Work item is being processed or has completed its work.
2291 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292 */
2293static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002294 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002296 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297}
2298
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002300 * @brief Get time remaining before a delayed work gets scheduled.
2301 *
2302 * This routine computes the (approximate) time remaining before a
2303 * delayed work gets executed. If the delayed work is not waiting to be
2304 * schedules, it returns zero.
2305 *
2306 * @param work Delayed work item.
2307 *
2308 * @return Remaining time (in milliseconds).
2309 */
Kumar Galacc334c72017-04-21 10:55:34 -05002310static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002311{
2312 return _timeout_remaining_get(&work->timeout);
2313}
2314
2315/**
Allan Stephensc98da842016-11-11 15:45:03 -05002316 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002317 */
2318
Allan Stephensc98da842016-11-11 15:45:03 -05002319/**
2320 * @cond INTERNAL_HIDDEN
2321 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322
2323struct k_mutex {
2324 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002325 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002326 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002327 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328
Anas Nashif2f203c22016-12-18 06:57:45 -05002329 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330};
2331
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002332#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002333 { \
2334 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2335 .owner = NULL, \
2336 .lock_count = 0, \
2337 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002338 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339 }
2340
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002341#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2342
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343/**
Allan Stephensc98da842016-11-11 15:45:03 -05002344 * INTERNAL_HIDDEN @endcond
2345 */
2346
2347/**
2348 * @defgroup mutex_apis Mutex APIs
2349 * @ingroup kernel_apis
2350 * @{
2351 */
2352
2353/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002358 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002362#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002363 struct k_mutex name \
2364 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002365 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002366
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002367/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002370 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * Upon completion, the mutex is available and does not have an owner.
2373 *
2374 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002375 *
2376 * @return N/A
2377 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379
2380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002383 * This routine locks @a mutex. If the mutex is locked by another thread,
2384 * the calling thread waits until the mutex becomes available or until
2385 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002387 * A thread is permitted to lock a mutex it has already locked. The operation
2388 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002390 * @param mutex Address of the mutex.
2391 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 * or one of the special values K_NO_WAIT and K_FOREVER.
2393 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002394 * @retval 0 Mutex locked.
2395 * @retval -EBUSY Returned without waiting.
2396 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397 */
Kumar Galacc334c72017-04-21 10:55:34 -05002398extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002399
2400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * This routine unlocks @a mutex. The mutex must already be locked by the
2404 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002405 *
2406 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002408 * thread.
2409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 *
2412 * @return N/A
2413 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414extern void k_mutex_unlock(struct k_mutex *mutex);
2415
Allan Stephensc98da842016-11-11 15:45:03 -05002416/**
2417 * @} end defgroup mutex_apis
2418 */
2419
2420/**
2421 * @cond INTERNAL_HIDDEN
2422 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002423
2424struct k_sem {
2425 _wait_q_t wait_q;
2426 unsigned int count;
2427 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002428 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002429
Anas Nashif2f203c22016-12-18 06:57:45 -05002430 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431};
2432
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002433#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002434 { \
2435 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2436 .count = initial_count, \
2437 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002438 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002439 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002440 }
2441
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002442#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2443
Allan Stephensc98da842016-11-11 15:45:03 -05002444/**
2445 * INTERNAL_HIDDEN @endcond
2446 */
2447
2448/**
2449 * @defgroup semaphore_apis Semaphore APIs
2450 * @ingroup kernel_apis
2451 * @{
2452 */
2453
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002455 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002457 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @param sem Address of the semaphore.
2460 * @param initial_count Initial semaphore count.
2461 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002462 *
2463 * @return N/A
2464 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2466 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002467
2468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2474 *
2475 * @param sem Address of the semaphore.
2476 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002477 * or one of the special values K_NO_WAIT and K_FOREVER.
2478 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002479 * @note When porting code from the nanokernel legacy API to the new API, be
2480 * careful with the return value of this function. The return value is the
2481 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2482 * non-zero means failure, while the nano_sem_take family returns 1 for success
2483 * and 0 for failure.
2484 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002485 * @retval 0 Semaphore taken.
2486 * @retval -EBUSY Returned without waiting.
2487 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002488 */
Kumar Galacc334c72017-04-21 10:55:34 -05002489extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002490
2491/**
2492 * @brief Give a semaphore.
2493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * This routine gives @a sem, unless the semaphore is already at its maximum
2495 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002500 *
2501 * @return N/A
2502 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503extern void k_sem_give(struct k_sem *sem);
2504
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002505/**
2506 * @brief Reset a semaphore's count to zero.
2507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002511 *
2512 * @return N/A
2513 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002514static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515{
2516 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517}
2518
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002519/**
2520 * @brief Get a semaphore's count.
2521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002527 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002528static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529{
2530 return sem->count;
2531}
2532
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002537 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002538 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @param name Name of the semaphore.
2541 * @param initial_count Initial semaphore count.
2542 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002543 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002545 struct k_sem name \
2546 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002547 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548
Allan Stephensc98da842016-11-11 15:45:03 -05002549/**
2550 * @} end defgroup semaphore_apis
2551 */
2552
2553/**
2554 * @defgroup alert_apis Alert APIs
2555 * @ingroup kernel_apis
2556 * @{
2557 */
2558
Allan Stephens5eceb852016-11-16 10:16:30 -05002559/**
2560 * @typedef k_alert_handler_t
2561 * @brief Alert handler function type.
2562 *
2563 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002564 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002565 * and is only invoked if the alert has been initialized with one.
2566 *
2567 * @param alert Address of the alert.
2568 *
2569 * @return 0 if alert has been consumed; non-zero if alert should pend.
2570 */
2571typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002572
2573/**
2574 * @} end defgroup alert_apis
2575 */
2576
2577/**
2578 * @cond INTERNAL_HIDDEN
2579 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002581#define K_ALERT_DEFAULT NULL
2582#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002584struct k_alert {
2585 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 atomic_t send_count;
2587 struct k_work work_item;
2588 struct k_sem sem;
2589
Anas Nashif2f203c22016-12-18 06:57:45 -05002590 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591};
2592
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002593extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002595#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002597 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002599 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2600 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002601 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602 }
2603
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002604#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2605
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002606/**
Allan Stephensc98da842016-11-11 15:45:03 -05002607 * INTERNAL_HIDDEN @endcond
2608 */
2609
2610/**
2611 * @addtogroup alert_apis
2612 * @{
2613 */
2614
2615/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002617 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002618 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002619 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002620 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002622 * @param name Name of the alert.
2623 * @param alert_handler Action to take when alert is sent. Specify either
2624 * the address of a function to be invoked by the system workqueue
2625 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2626 * K_ALERT_DEFAULT (which causes the alert to pend).
2627 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002628 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002629#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002630 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002631 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002632 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002633 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002635/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002640 * @param alert Address of the alert.
2641 * @param handler Action to take when alert is sent. Specify either the address
2642 * of a function to be invoked by the system workqueue thread,
2643 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2644 * K_ALERT_DEFAULT (which causes the alert to pend).
2645 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646 *
2647 * @return N/A
2648 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002649extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2650 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002651
2652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002653 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002655 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2658 *
2659 * @param alert Address of the alert.
2660 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661 * or one of the special values K_NO_WAIT and K_FOREVER.
2662 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002663 * @retval 0 Alert received.
2664 * @retval -EBUSY Returned without waiting.
2665 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 */
Kumar Galacc334c72017-04-21 10:55:34 -05002667extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668
2669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * This routine signals @a alert. The action specified for @a alert will
2673 * be taken, which may trigger the execution of an alert handler function
2674 * and/or cause the alert to pend (assuming the alert has not reached its
2675 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * @note Can be called by ISRs.
2678 *
2679 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002680 *
2681 * @return N/A
2682 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002683extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684
2685/**
Allan Stephensc98da842016-11-11 15:45:03 -05002686 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687 */
2688
Allan Stephensc98da842016-11-11 15:45:03 -05002689/**
2690 * @cond INTERNAL_HIDDEN
2691 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692
2693struct k_msgq {
2694 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002695 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002696 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697 char *buffer_start;
2698 char *buffer_end;
2699 char *read_ptr;
2700 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002701 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702
Anas Nashif2f203c22016-12-18 06:57:45 -05002703 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704};
2705
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002706#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 { \
2708 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002709 .max_msgs = q_max_msgs, \
2710 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002712 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 .read_ptr = q_buffer, \
2714 .write_ptr = q_buffer, \
2715 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002716 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 }
2718
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002719#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2720
Peter Mitsis1da807e2016-10-06 11:36:59 -04002721/**
Allan Stephensc98da842016-11-11 15:45:03 -05002722 * INTERNAL_HIDDEN @endcond
2723 */
2724
2725/**
2726 * @defgroup msgq_apis Message Queue APIs
2727 * @ingroup kernel_apis
2728 * @{
2729 */
2730
2731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002732 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002734 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2735 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002736 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2737 * message is similarly aligned to this boundary, @a q_msg_size must also be
2738 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002740 * The message queue can be accessed outside the module where it is defined
2741 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002743 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * @param q_name Name of the message queue.
2746 * @param q_msg_size Message size (in bytes).
2747 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002748 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002749 */
2750#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2751 static char __noinit __aligned(q_align) \
2752 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002753 struct k_msgq q_name \
2754 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002755 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002756 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757
Peter Mitsisd7a37502016-10-13 11:37:40 -04002758/**
2759 * @brief Initialize a message queue.
2760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 * This routine initializes a message queue object, prior to its first use.
2762 *
Allan Stephensda827222016-11-09 14:23:58 -06002763 * The message queue's ring buffer must contain space for @a max_msgs messages,
2764 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2765 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2766 * that each message is similarly aligned to this boundary, @a q_msg_size
2767 * must also be a multiple of N.
2768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 * @param q Address of the message queue.
2770 * @param buffer Pointer to ring buffer that holds queued messages.
2771 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002772 * @param max_msgs Maximum number of messages that can be queued.
2773 *
2774 * @return N/A
2775 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002776extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002777 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002778
2779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002782 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002783 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002784 * @note Can be called by ISRs.
2785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * @param q Address of the message queue.
2787 * @param data Pointer to the message.
2788 * @param timeout Waiting period to add the message (in milliseconds),
2789 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002790 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002791 * @retval 0 Message sent.
2792 * @retval -ENOMSG Returned without waiting or queue purged.
2793 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002794 */
Kumar Galacc334c72017-04-21 10:55:34 -05002795extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002796
2797/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * This routine receives a message from message queue @a q in a "first in,
2801 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002802 *
Allan Stephensc98da842016-11-11 15:45:03 -05002803 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002805 * @param q Address of the message queue.
2806 * @param data Address of area to hold the received message.
2807 * @param timeout Waiting period to receive the message (in milliseconds),
2808 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002809 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002810 * @retval 0 Message received.
2811 * @retval -ENOMSG Returned without waiting.
2812 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002813 */
Kumar Galacc334c72017-04-21 10:55:34 -05002814extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002815
2816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * This routine discards all unreceived messages in a message queue's ring
2820 * buffer. Any threads that are blocked waiting to send a message to the
2821 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002824 *
2825 * @return N/A
2826 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827extern void k_msgq_purge(struct k_msgq *q);
2828
Peter Mitsis67be2492016-10-07 11:44:34 -04002829/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002832 * This routine returns the number of unused entries in a message queue's
2833 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002835 * @param q Address of the message queue.
2836 *
2837 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002838 */
Kumar Galacc334c72017-04-21 10:55:34 -05002839static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002840{
2841 return q->max_msgs - q->used_msgs;
2842}
2843
Peter Mitsisd7a37502016-10-13 11:37:40 -04002844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * @param q Address of the message queue.
2850 *
2851 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002852 */
Kumar Galacc334c72017-04-21 10:55:34 -05002853static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854{
2855 return q->used_msgs;
2856}
2857
Allan Stephensc98da842016-11-11 15:45:03 -05002858/**
2859 * @} end defgroup msgq_apis
2860 */
2861
2862/**
2863 * @defgroup mem_pool_apis Memory Pool APIs
2864 * @ingroup kernel_apis
2865 * @{
2866 */
2867
Andy Ross73cb9582017-05-09 10:42:39 -07002868/* Note on sizing: the use of a 20 bit field for block means that,
2869 * assuming a reasonable minimum block size of 16 bytes, we're limited
2870 * to 16M of memory managed by a single pool. Long term it would be
2871 * good to move to a variable bit size based on configuration.
2872 */
2873struct k_mem_block_id {
2874 u32_t pool : 8;
2875 u32_t level : 4;
2876 u32_t block : 20;
2877};
2878
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002880 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002881 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002882};
2883
Allan Stephensc98da842016-11-11 15:45:03 -05002884/**
2885 * @} end defgroup mem_pool_apis
2886 */
2887
2888/**
2889 * @defgroup mailbox_apis Mailbox APIs
2890 * @ingroup kernel_apis
2891 * @{
2892 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002893
2894struct k_mbox_msg {
2895 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002896 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002897 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002898 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002899 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002900 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901 /** sender's message data buffer */
2902 void *tx_data;
2903 /** internal use only - needed for legacy API support */
2904 void *_rx_data;
2905 /** message data block descriptor */
2906 struct k_mem_block tx_block;
2907 /** source thread id */
2908 k_tid_t rx_source_thread;
2909 /** target thread id */
2910 k_tid_t tx_target_thread;
2911 /** internal use only - thread waiting on send (may be a dummy) */
2912 k_tid_t _syncing_thread;
2913#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2914 /** internal use only - semaphore used during asynchronous send */
2915 struct k_sem *_async_sem;
2916#endif
2917};
2918
Allan Stephensc98da842016-11-11 15:45:03 -05002919/**
2920 * @cond INTERNAL_HIDDEN
2921 */
2922
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923struct k_mbox {
2924 _wait_q_t tx_msg_queue;
2925 _wait_q_t rx_msg_queue;
2926
Anas Nashif2f203c22016-12-18 06:57:45 -05002927 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928};
2929
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002930#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931 { \
2932 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2933 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002934 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935 }
2936
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002937#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2938
Peter Mitsis12092702016-10-14 12:57:23 -04002939/**
Allan Stephensc98da842016-11-11 15:45:03 -05002940 * INTERNAL_HIDDEN @endcond
2941 */
2942
2943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002946 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002947 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002948 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002951 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002952#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002953 struct k_mbox name \
2954 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002955 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002956
Peter Mitsis12092702016-10-14 12:57:23 -04002957/**
2958 * @brief Initialize a mailbox.
2959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * This routine initializes a mailbox object, prior to its first use.
2961 *
2962 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002963 *
2964 * @return N/A
2965 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966extern void k_mbox_init(struct k_mbox *mbox);
2967
Peter Mitsis12092702016-10-14 12:57:23 -04002968/**
2969 * @brief Send a mailbox message in a synchronous manner.
2970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * This routine sends a message to @a mbox and waits for a receiver to both
2972 * receive and process it. The message data may be in a buffer, in a memory
2973 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @param mbox Address of the mailbox.
2976 * @param tx_msg Address of the transmit message descriptor.
2977 * @param timeout Waiting period for the message to be received (in
2978 * milliseconds), or one of the special values K_NO_WAIT
2979 * and K_FOREVER. Once the message has been received,
2980 * this routine waits as long as necessary for the message
2981 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002982 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002983 * @retval 0 Message sent.
2984 * @retval -ENOMSG Returned without waiting.
2985 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04002986 */
Peter Mitsis40680f62016-10-14 10:04:55 -04002987extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05002988 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04002989
Peter Mitsis12092702016-10-14 12:57:23 -04002990/**
2991 * @brief Send a mailbox message in an asynchronous manner.
2992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * This routine sends a message to @a mbox without waiting for a receiver
2994 * to process it. The message data may be in a buffer, in a memory pool block,
2995 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
2996 * will be given when the message has been both received and completely
2997 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * @param mbox Address of the mailbox.
3000 * @param tx_msg Address of the transmit message descriptor.
3001 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003002 *
3003 * @return N/A
3004 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003005extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006 struct k_sem *sem);
3007
Peter Mitsis12092702016-10-14 12:57:23 -04003008/**
3009 * @brief Receive a mailbox message.
3010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * This routine receives a message from @a mbox, then optionally retrieves
3012 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @param mbox Address of the mailbox.
3015 * @param rx_msg Address of the receive message descriptor.
3016 * @param buffer Address of the buffer to receive data, or NULL to defer data
3017 * retrieval and message disposal until later.
3018 * @param timeout Waiting period for a message to be received (in
3019 * milliseconds), or one of the special values K_NO_WAIT
3020 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003021 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003022 * @retval 0 Message received.
3023 * @retval -ENOMSG Returned without waiting.
3024 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003025 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003026extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003027 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003028
3029/**
3030 * @brief Retrieve mailbox message data into a buffer.
3031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * This routine completes the processing of a received message by retrieving
3033 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003034 *
3035 * Alternatively, this routine can be used to dispose of a received message
3036 * without retrieving its data.
3037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @param rx_msg Address of the receive message descriptor.
3039 * @param buffer Address of the buffer to receive data, or NULL to discard
3040 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003041 *
3042 * @return N/A
3043 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003044extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003045
3046/**
3047 * @brief Retrieve mailbox message data into a memory pool block.
3048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * This routine completes the processing of a received message by retrieving
3050 * its data into a memory pool block, then disposing of the message.
3051 * The memory pool block that results from successful retrieval must be
3052 * returned to the pool once the data has been processed, even in cases
3053 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003054 *
3055 * Alternatively, this routine can be used to dispose of a received message
3056 * without retrieving its data. In this case there is no need to return a
3057 * memory pool block to the pool.
3058 *
3059 * This routine allocates a new memory pool block for the data only if the
3060 * data is not already in one. If a new block cannot be allocated, the routine
3061 * returns a failure code and the received message is left unchanged. This
3062 * permits the caller to reattempt data retrieval at a later time or to dispose
3063 * of the received message without retrieving its data.
3064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * @param rx_msg Address of a receive message descriptor.
3066 * @param pool Address of memory pool, or NULL to discard data.
3067 * @param block Address of the area to hold memory pool block info.
3068 * @param timeout Waiting period to wait for a memory pool block (in
3069 * milliseconds), or one of the special values K_NO_WAIT
3070 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003071 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003072 * @retval 0 Data retrieved.
3073 * @retval -ENOMEM Returned without waiting.
3074 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003075 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003076extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003077 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003078 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079
Allan Stephensc98da842016-11-11 15:45:03 -05003080/**
3081 * @} end defgroup mailbox_apis
3082 */
3083
3084/**
3085 * @cond INTERNAL_HIDDEN
3086 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087
3088struct k_pipe {
3089 unsigned char *buffer; /* Pipe buffer: may be NULL */
3090 size_t size; /* Buffer size */
3091 size_t bytes_used; /* # bytes used in buffer */
3092 size_t read_index; /* Where in buffer to read from */
3093 size_t write_index; /* Where in buffer to write */
3094
3095 struct {
3096 _wait_q_t readers; /* Reader wait queue */
3097 _wait_q_t writers; /* Writer wait queue */
3098 } wait_q;
3099
Anas Nashif2f203c22016-12-18 06:57:45 -05003100 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101};
3102
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003103#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003104 { \
3105 .buffer = pipe_buffer, \
3106 .size = pipe_buffer_size, \
3107 .bytes_used = 0, \
3108 .read_index = 0, \
3109 .write_index = 0, \
3110 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3111 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003112 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113 }
3114
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003115#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3116
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003117/**
Allan Stephensc98da842016-11-11 15:45:03 -05003118 * INTERNAL_HIDDEN @endcond
3119 */
3120
3121/**
3122 * @defgroup pipe_apis Pipe APIs
3123 * @ingroup kernel_apis
3124 * @{
3125 */
3126
3127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003128 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003132 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003134 * @param name Name of the pipe.
3135 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3136 * or zero if no ring buffer is used.
3137 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003139#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3140 static unsigned char __noinit __aligned(pipe_align) \
3141 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003142 struct k_pipe name \
3143 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003144 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003145
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003149 * This routine initializes a pipe object, prior to its first use.
3150 *
3151 * @param pipe Address of the pipe.
3152 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3153 * is used.
3154 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3155 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156 *
3157 * @return N/A
3158 */
3159extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3160 size_t size);
3161
3162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003163 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003165 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * @param pipe Address of the pipe.
3168 * @param data Address of data to write.
3169 * @param bytes_to_write Size of data (in bytes).
3170 * @param bytes_written Address of area to hold the number of bytes written.
3171 * @param min_xfer Minimum number of bytes to write.
3172 * @param timeout Waiting period to wait for the data to be written (in
3173 * milliseconds), or one of the special values K_NO_WAIT
3174 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003176 * @retval 0 At least @a min_xfer bytes of data were written.
3177 * @retval -EIO Returned without waiting; zero data bytes were written.
3178 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003181extern int k_pipe_put(struct k_pipe *pipe, void *data,
3182 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003183 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184
3185/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * @param pipe Address of the pipe.
3191 * @param data Address to place the data read from pipe.
3192 * @param bytes_to_read Maximum number of data bytes to read.
3193 * @param bytes_read Address of area to hold the number of bytes read.
3194 * @param min_xfer Minimum number of data bytes to read.
3195 * @param timeout Waiting period to wait for the data to be read (in
3196 * milliseconds), or one of the special values K_NO_WAIT
3197 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003198 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003199 * @retval 0 At least @a min_xfer bytes of data were read.
3200 * @retval -EIO Returned without waiting; zero data bytes were read.
3201 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003204extern int k_pipe_get(struct k_pipe *pipe, void *data,
3205 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003206 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207
3208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * This routine writes the data contained in a memory block to @a pipe.
3212 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003216 * @param block Memory block containing data to send
3217 * @param size Number of data bytes in memory block to send
3218 * @param sem Semaphore to signal upon completion (else NULL)
3219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221 */
3222extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3223 size_t size, struct k_sem *sem);
3224
3225/**
Allan Stephensc98da842016-11-11 15:45:03 -05003226 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 */
3228
Allan Stephensc98da842016-11-11 15:45:03 -05003229/**
3230 * @cond INTERNAL_HIDDEN
3231 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003233struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003235 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003236 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237 char *buffer;
3238 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003239 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003240
Anas Nashif2f203c22016-12-18 06:57:45 -05003241 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242};
3243
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003244#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003245 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 { \
3247 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003248 .num_blocks = slab_num_blocks, \
3249 .block_size = slab_block_size, \
3250 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 .free_list = NULL, \
3252 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003253 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 }
3255
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003256#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3257
3258
Peter Mitsis578f9112016-10-07 13:50:31 -04003259/**
Allan Stephensc98da842016-11-11 15:45:03 -05003260 * INTERNAL_HIDDEN @endcond
3261 */
3262
3263/**
3264 * @defgroup mem_slab_apis Memory Slab APIs
3265 * @ingroup kernel_apis
3266 * @{
3267 */
3268
3269/**
Allan Stephensda827222016-11-09 14:23:58 -06003270 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003271 *
Allan Stephensda827222016-11-09 14:23:58 -06003272 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003274 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3275 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003276 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003277 *
Allan Stephensda827222016-11-09 14:23:58 -06003278 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003280 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003281 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003283 * @param name Name of the memory slab.
3284 * @param slab_block_size Size of each memory block (in bytes).
3285 * @param slab_num_blocks Number memory blocks.
3286 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003287 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003288#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3289 char __noinit __aligned(slab_align) \
3290 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3291 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003292 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003293 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003294 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003295
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003296/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003297 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003300 *
Allan Stephensda827222016-11-09 14:23:58 -06003301 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3302 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3303 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3304 * To ensure that each memory block is similarly aligned to this boundary,
3305 * @a slab_block_size must also be a multiple of N.
3306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * @param slab Address of the memory slab.
3308 * @param buffer Pointer to buffer used for the memory blocks.
3309 * @param block_size Size of each memory block (in bytes).
3310 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003311 *
3312 * @return N/A
3313 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003314extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003315 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003316
3317/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003320 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003322 * @param slab Address of the memory slab.
3323 * @param mem Pointer to block address area.
3324 * @param timeout Maximum time to wait for operation to complete
3325 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3326 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003327 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003328 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003330 * @retval -ENOMEM Returned without waiting.
3331 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003332 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003333extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003334 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003335
3336/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * This routine releases a previously allocated memory block back to its
3340 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003342 * @param slab Address of the memory slab.
3343 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003344 *
3345 * @return N/A
3346 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003347extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * This routine gets the number of memory blocks that are currently
3353 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358 */
Kumar Galacc334c72017-04-21 10:55:34 -05003359static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003361 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362}
3363
Peter Mitsisc001aa82016-10-13 13:53:37 -04003364/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * This routine gets the number of memory blocks that are currently
3368 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003370 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003373 */
Kumar Galacc334c72017-04-21 10:55:34 -05003374static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003375{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003376 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003377}
3378
Allan Stephensc98da842016-11-11 15:45:03 -05003379/**
3380 * @} end defgroup mem_slab_apis
3381 */
3382
3383/**
3384 * @cond INTERNAL_HIDDEN
3385 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003386
Andy Ross73cb9582017-05-09 10:42:39 -07003387struct k_mem_pool_lvl {
3388 union {
3389 u32_t *bits_p;
3390 u32_t bits;
3391 };
3392 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003393};
3394
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003395struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003396 void *buf;
3397 size_t max_sz;
3398 u16_t n_max;
3399 u8_t n_levels;
3400 u8_t max_inline_level;
3401 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003402 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003403};
3404
Andy Ross73cb9582017-05-09 10:42:39 -07003405#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003406
Andy Ross73cb9582017-05-09 10:42:39 -07003407#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3408
3409#define _MPOOL_LVLS(maxsz, minsz) \
3410 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3411 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3412 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3413 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3414 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3415 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3416 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3417 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3418 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3419 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3420 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3421 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3422 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3423 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3424 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3425 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3426
3427/* Rounds the needed bits up to integer multiples of u32_t */
3428#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3429 ((((n_max) << (2*(l))) + 31) / 32)
3430
3431/* One word gets stored free unioned with the pointer, otherwise the
3432 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003433 */
Andy Ross73cb9582017-05-09 10:42:39 -07003434#define _MPOOL_LBIT_WORDS(n_max, l) \
3435 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3436 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003437
Andy Ross73cb9582017-05-09 10:42:39 -07003438/* How many bytes for the bitfields of a single level? */
3439#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3440 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3441 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003442
Andy Ross73cb9582017-05-09 10:42:39 -07003443/* Size of the bitmap array that follows the buffer in allocated memory */
3444#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3445 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3446 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3447 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3448 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3449 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3450 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3451 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3452 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3453 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3454 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3455 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3456 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3457 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3458 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3459 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3460 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003461
3462/**
Allan Stephensc98da842016-11-11 15:45:03 -05003463 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003464 */
3465
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003466/**
Allan Stephensc98da842016-11-11 15:45:03 -05003467 * @addtogroup mem_pool_apis
3468 * @{
3469 */
3470
3471/**
3472 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3475 * long. The memory pool allows blocks to be repeatedly partitioned into
3476 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003477 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003478 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003479 * If the pool is to be accessed outside the module where it is defined, it
3480 * can be declared via
3481 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003482 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003484 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003485 * @param minsz Size of the smallest blocks in the pool (in bytes).
3486 * @param maxsz Size of the largest blocks in the pool (in bytes).
3487 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003488 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003489 */
Andy Ross73cb9582017-05-09 10:42:39 -07003490#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3491 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3492 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3493 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3494 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3495 .buf = _mpool_buf_##name, \
3496 .max_sz = maxsz, \
3497 .n_max = nmax, \
3498 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3499 .levels = _mpool_lvls_##name, \
3500 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003501
Peter Mitsis937042c2016-10-13 13:18:26 -04003502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003503 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003505 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * @param pool Address of the memory pool.
3508 * @param block Pointer to block descriptor for the allocated memory.
3509 * @param size Amount of memory to allocate (in bytes).
3510 * @param timeout Maximum time to wait for operation to complete
3511 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3512 * or K_FOREVER to wait as long as necessary.
3513 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003514 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003516 * @retval -ENOMEM Returned without waiting.
3517 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003518 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003519extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003520 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003521
3522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003523 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * This routine releases a previously allocated memory block back to its
3526 * memory pool.
3527 *
3528 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003529 *
3530 * @return N/A
3531 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003532extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003533
3534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003536 *
Andy Ross73cb9582017-05-09 10:42:39 -07003537 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 *
Andy Ross73cb9582017-05-09 10:42:39 -07003539 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003540 *
3541 * @return N/A
3542 */
Andy Ross73cb9582017-05-09 10:42:39 -07003543static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003544
3545/**
Allan Stephensc98da842016-11-11 15:45:03 -05003546 * @} end addtogroup mem_pool_apis
3547 */
3548
3549/**
3550 * @defgroup heap_apis Heap Memory Pool APIs
3551 * @ingroup kernel_apis
3552 * @{
3553 */
3554
3555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003559 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003564 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003565extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003566
3567/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003569 *
3570 * This routine provides traditional free() semantics. The memory being
3571 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003572 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003573 * If @a ptr is NULL, no operation is performed.
3574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003575 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003576 *
3577 * @return N/A
3578 */
3579extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003580
Allan Stephensc98da842016-11-11 15:45:03 -05003581/**
3582 * @} end defgroup heap_apis
3583 */
3584
Benjamin Walshacc68c12017-01-29 18:57:45 -05003585/* polling API - PRIVATE */
3586
Benjamin Walshb0179862017-02-02 16:39:57 -05003587#ifdef CONFIG_POLL
3588#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3589#else
3590#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3591#endif
3592
Benjamin Walshacc68c12017-01-29 18:57:45 -05003593/* private - implementation data created as needed, per-type */
3594struct _poller {
3595 struct k_thread *thread;
3596};
3597
3598/* private - types bit positions */
3599enum _poll_types_bits {
3600 /* can be used to ignore an event */
3601 _POLL_TYPE_IGNORE,
3602
3603 /* to be signaled by k_poll_signal() */
3604 _POLL_TYPE_SIGNAL,
3605
3606 /* semaphore availability */
3607 _POLL_TYPE_SEM_AVAILABLE,
3608
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003609 /* queue/fifo/lifo data availability */
3610 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003611
3612 _POLL_NUM_TYPES
3613};
3614
3615#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3616
3617/* private - states bit positions */
3618enum _poll_states_bits {
3619 /* default state when creating event */
3620 _POLL_STATE_NOT_READY,
3621
Benjamin Walshacc68c12017-01-29 18:57:45 -05003622 /* signaled by k_poll_signal() */
3623 _POLL_STATE_SIGNALED,
3624
3625 /* semaphore is available */
3626 _POLL_STATE_SEM_AVAILABLE,
3627
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003628 /* data is available to read on queue/fifo/lifo */
3629 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003630
3631 _POLL_NUM_STATES
3632};
3633
3634#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3635
3636#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003637 (32 - (0 \
3638 + 8 /* tag */ \
3639 + _POLL_NUM_TYPES \
3640 + _POLL_NUM_STATES \
3641 + 1 /* modes */ \
3642 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003643
3644#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3645#error overflow of 32-bit word in struct k_poll_event
3646#endif
3647
3648/* end of polling API - PRIVATE */
3649
3650
3651/**
3652 * @defgroup poll_apis Async polling APIs
3653 * @ingroup kernel_apis
3654 * @{
3655 */
3656
3657/* Public polling API */
3658
3659/* public - values for k_poll_event.type bitfield */
3660#define K_POLL_TYPE_IGNORE 0
3661#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3662#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003663#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3664#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003665
3666/* public - polling modes */
3667enum k_poll_modes {
3668 /* polling thread does not take ownership of objects when available */
3669 K_POLL_MODE_NOTIFY_ONLY = 0,
3670
3671 K_POLL_NUM_MODES
3672};
3673
3674/* public - values for k_poll_event.state bitfield */
3675#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003676#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3677#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003678#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3679#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003680
3681/* public - poll signal object */
3682struct k_poll_signal {
3683 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003684 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003685
3686 /*
3687 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3688 * user resets it to 0.
3689 */
3690 unsigned int signaled;
3691
3692 /* custom result value passed to k_poll_signal() if needed */
3693 int result;
3694};
3695
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003696#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003697 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003698 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003699 .signaled = 0, \
3700 .result = 0, \
3701 }
3702
3703struct k_poll_event {
3704 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003705 sys_dnode_t _node;
3706
3707 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003708 struct _poller *poller;
3709
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003710 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003711 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003712
Benjamin Walshacc68c12017-01-29 18:57:45 -05003713 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003714 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003715
3716 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003717 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003718
3719 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003720 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003721
3722 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003723 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003724
3725 /* per-type data */
3726 union {
3727 void *obj;
3728 struct k_poll_signal *signal;
3729 struct k_sem *sem;
3730 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003731 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003732 };
3733};
3734
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003735#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003736 { \
3737 .poller = NULL, \
3738 .type = event_type, \
3739 .state = K_POLL_STATE_NOT_READY, \
3740 .mode = event_mode, \
3741 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003742 { .obj = event_obj }, \
3743 }
3744
3745#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3746 event_tag) \
3747 { \
3748 .type = event_type, \
3749 .tag = event_tag, \
3750 .state = K_POLL_STATE_NOT_READY, \
3751 .mode = event_mode, \
3752 .unused = 0, \
3753 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003754 }
3755
3756/**
3757 * @brief Initialize one struct k_poll_event instance
3758 *
3759 * After this routine is called on a poll event, the event it ready to be
3760 * placed in an event array to be passed to k_poll().
3761 *
3762 * @param event The event to initialize.
3763 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3764 * values. Only values that apply to the same object being polled
3765 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3766 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003767 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003768 * @param obj Kernel object or poll signal.
3769 *
3770 * @return N/A
3771 */
3772
Kumar Galacc334c72017-04-21 10:55:34 -05003773extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003774 int mode, void *obj);
3775
3776/**
3777 * @brief Wait for one or many of multiple poll events to occur
3778 *
3779 * This routine allows a thread to wait concurrently for one or many of
3780 * multiple poll events to have occurred. Such events can be a kernel object
3781 * being available, like a semaphore, or a poll signal event.
3782 *
3783 * When an event notifies that a kernel object is available, the kernel object
3784 * is not "given" to the thread calling k_poll(): it merely signals the fact
3785 * that the object was available when the k_poll() call was in effect. Also,
3786 * all threads trying to acquire an object the regular way, i.e. by pending on
3787 * the object, have precedence over the thread polling on the object. This
3788 * means that the polling thread will never get the poll event on an object
3789 * until the object becomes available and its pend queue is empty. For this
3790 * reason, the k_poll() call is more effective when the objects being polled
3791 * only have one thread, the polling thread, trying to acquire them.
3792 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003793 * When k_poll() returns 0, the caller should loop on all the events that were
3794 * passed to k_poll() and check the state field for the values that were
3795 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003796 *
3797 * Before being reused for another call to k_poll(), the user has to reset the
3798 * state field to K_POLL_STATE_NOT_READY.
3799 *
3800 * @param events An array of pointers to events to be polled for.
3801 * @param num_events The number of events in the array.
3802 * @param timeout Waiting period for an event to be ready (in milliseconds),
3803 * or one of the special values K_NO_WAIT and K_FOREVER.
3804 *
3805 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003806 * @retval -EAGAIN Waiting period timed out.
3807 */
3808
3809extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003810 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003811
3812/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003813 * @brief Initialize a poll signal object.
3814 *
3815 * Ready a poll signal object to be signaled via k_poll_signal().
3816 *
3817 * @param signal A poll signal.
3818 *
3819 * @return N/A
3820 */
3821
3822extern void k_poll_signal_init(struct k_poll_signal *signal);
3823
3824/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003825 * @brief Signal a poll signal object.
3826 *
3827 * This routine makes ready a poll signal, which is basically a poll event of
3828 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3829 * made ready to run. A @a result value can be specified.
3830 *
3831 * The poll signal contains a 'signaled' field that, when set by
3832 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3833 * be reset by the user before being passed again to k_poll() or k_poll() will
3834 * consider it being signaled, and will return immediately.
3835 *
3836 * @param signal A poll signal.
3837 * @param result The value to store in the result field of the signal.
3838 *
3839 * @retval 0 The signal was delivered successfully.
3840 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3841 */
3842
3843extern int k_poll_signal(struct k_poll_signal *signal, int result);
3844
3845/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003846extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847
3848/**
3849 * @} end defgroup poll_apis
3850 */
3851
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003852/**
3853 * @brief Make the CPU idle.
3854 *
3855 * This function makes the CPU idle until an event wakes it up.
3856 *
3857 * In a regular system, the idle thread should be the only thread responsible
3858 * for making the CPU idle and triggering any type of power management.
3859 * However, in some more constrained systems, such as a single-threaded system,
3860 * the only thread would be responsible for this if needed.
3861 *
3862 * @return N/A
3863 */
3864extern void k_cpu_idle(void);
3865
3866/**
3867 * @brief Make the CPU idle in an atomic fashion.
3868 *
3869 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3870 * must be done atomically before making the CPU idle.
3871 *
3872 * @param key Interrupt locking key obtained from irq_lock().
3873 *
3874 * @return N/A
3875 */
3876extern void k_cpu_atomic_idle(unsigned int key);
3877
Kumar Galacc334c72017-04-21 10:55:34 -05003878extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003879
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003880#include <arch/cpu.h>
3881
Andrew Boie1f32d092017-08-30 14:00:26 -07003882#ifdef CONFIG_USERSPACE
3883/* Architecture-specific inline functions that may be indirectly called by
3884 * application code due to their appearance in macros or other inline functions.
3885 *
3886 * Each arch should implement these in <arch/cpu.h>
3887 */
3888
3889/* Indicate whether we are currently running in user mode
3890 *
3891 * @return nonzero if the CPU is currently running with user permissions
3892 */
3893static inline int _arch_is_user_context(void);
3894
3895/* Interfaces for invoking system calls */
3896static inline u32_t _arch_syscall_invoke5(u32_t arg1, u32_t arg2, u32_t arg3,
3897 u32_t arg4, u32_t arg5,
3898 u32_t call_id);
3899
3900static inline u32_t _arch_syscall_invoke4(u32_t arg1, u32_t arg2, u32_t arg3,
3901 u32_t arg4, u32_t call_id);
3902
3903static inline u32_t _arch_syscall_invoke3(u32_t arg1, u32_t arg2, u32_t arg3,
3904 u32_t call_id);
3905
3906static inline u32_t _arch_syscall_invoke2(u32_t arg1, u32_t arg2,
3907 u32_t call_id);
3908
3909static inline u32_t _arch_syscall_invoke1(u32_t arg1, u32_t call_id);
3910
3911static inline u32_t _arch_syscall_invoke0(u32_t call_id);
3912#endif
3913
Andrew Boiecdb94d62017-04-18 15:22:05 -07003914#ifdef _ARCH_EXCEPT
3915/* This archtecture has direct support for triggering a CPU exception */
3916#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3917#else
3918
3919#include <misc/printk.h>
3920
3921/* NOTE: This is the implementation for arches that do not implement
3922 * _ARCH_EXCEPT() to generate a real CPU exception.
3923 *
3924 * We won't have a real exception frame to determine the PC value when
3925 * the oops occurred, so print file and line number before we jump into
3926 * the fatal error handler.
3927 */
3928#define _k_except_reason(reason) do { \
3929 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3930 _NanoFatalErrorHandler(reason, &_default_esf); \
3931 CODE_UNREACHABLE; \
3932 } while (0)
3933
3934#endif /* _ARCH__EXCEPT */
3935
3936/**
3937 * @brief Fatally terminate a thread
3938 *
3939 * This should be called when a thread has encountered an unrecoverable
3940 * runtime condition and needs to terminate. What this ultimately
3941 * means is determined by the _fatal_error_handler() implementation, which
3942 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3943 *
3944 * If this is called from ISR context, the default system fatal error handler
3945 * will treat it as an unrecoverable system error, just like k_panic().
3946 */
3947#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3948
3949/**
3950 * @brief Fatally terminate the system
3951 *
3952 * This should be called when the Zephyr kernel has encountered an
3953 * unrecoverable runtime condition and needs to terminate. What this ultimately
3954 * means is determined by the _fatal_error_handler() implementation, which
3955 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3956 */
3957#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3958
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003959/*
3960 * private APIs that are utilized by one or more public APIs
3961 */
3962
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003963#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003965#else
3966#define _init_static_threads() do { } while ((0))
3967#endif
3968
3969extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003970extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003971
Andrew Boiedc5d9352017-06-02 12:56:47 -07003972/* arch/cpu.h may declare an architecture or platform-specific macro
3973 * for properly declaring stacks, compatible with MMU/MPU constraints if
3974 * enabled
3975 */
3976#ifdef _ARCH_THREAD_STACK_DEFINE
3977#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
3978#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
3979 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
3980#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
3981#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07003982static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
3983{
3984 return _ARCH_THREAD_STACK_BUFFER(sym);
3985}
Andrew Boiedc5d9352017-06-02 12:56:47 -07003986#else
3987/**
3988 * @brief Declare a toplevel thread stack memory region
3989 *
3990 * This declares a region of memory suitable for use as a thread's stack.
3991 *
3992 * This is the generic, historical definition. Align to STACK_ALIGN and put in
3993 * 'noinit' section so that it isn't zeroed at boot
3994 *
Andrew Boie507852a2017-07-25 18:47:07 -07003995 * The declared symbol will always be a k_thread_stack_t which can be passed to
3996 * k_thread_create, but should otherwise not be manipulated. If the buffer
3997 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07003998 *
3999 * It is legal to precede this definition with the 'static' keyword.
4000 *
4001 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4002 * parameter of k_thread_create(), it may not be the same as the
4003 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4004 *
4005 * @param sym Thread stack symbol name
4006 * @param size Size of the stack memory region
4007 */
4008#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004009 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004010
4011/**
4012 * @brief Declare a toplevel array of thread stack memory regions
4013 *
4014 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4015 * definition for additional details and constraints.
4016 *
4017 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4018 * 'noinit' section so that it isn't zeroed at boot
4019 *
4020 * @param sym Thread stack symbol name
4021 * @param nmemb Number of stacks to declare
4022 * @param size Size of the stack memory region
4023 */
4024
4025#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004026 struct _k_thread_stack_element __noinit \
4027 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004028
4029/**
4030 * @brief Declare an embedded stack memory region
4031 *
4032 * Used for stacks embedded within other data structures. Use is highly
4033 * discouraged but in some cases necessary. For memory protection scenarios,
4034 * it is very important that any RAM preceding this member not be writable
4035 * by threads else a stack overflow will lead to silent corruption. In other
4036 * words, the containing data structure should live in RAM owned by the kernel.
4037 *
4038 * @param sym Thread stack symbol name
4039 * @param size Size of the stack memory region
4040 */
4041#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004042 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004043
4044/**
4045 * @brief Return the size in bytes of a stack memory region
4046 *
4047 * Convenience macro for passing the desired stack size to k_thread_create()
4048 * since the underlying implementation may actually create something larger
4049 * (for instance a guard area).
4050 *
4051 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004052 * passed to K_THREAD_STACK_DEFINE.
4053 *
4054 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4055 * it is not guaranteed to return the original value since each array
4056 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004057 *
4058 * @param sym Stack memory symbol
4059 * @return Size of the stack
4060 */
4061#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4062
4063/**
4064 * @brief Get a pointer to the physical stack buffer
4065 *
4066 * Convenience macro to get at the real underlying stack buffer used by
4067 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4068 * This is really only intended for diagnostic tools which want to examine
4069 * stack memory contents.
4070 *
4071 * @param sym Declared stack symbol name
4072 * @return The buffer itself, a char *
4073 */
Andrew Boie507852a2017-07-25 18:47:07 -07004074static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4075{
4076 return (char *)sym;
4077}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004078
4079#endif /* _ARCH_DECLARE_STACK */
4080
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004081#ifdef __cplusplus
4082}
4083#endif
4084
Andrew Boiee004dec2016-11-07 09:01:19 -08004085#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4086/*
4087 * Define new and delete operators.
4088 * At this moment, the operators do nothing since objects are supposed
4089 * to be statically allocated.
4090 */
4091inline void operator delete(void *ptr)
4092{
4093 (void)ptr;
4094}
4095
4096inline void operator delete[](void *ptr)
4097{
4098 (void)ptr;
4099}
4100
4101inline void *operator new(size_t size)
4102{
4103 (void)size;
4104 return NULL;
4105}
4106
4107inline void *operator new[](size_t size)
4108{
4109 (void)size;
4110 return NULL;
4111}
4112
4113/* Placement versions of operator new and delete */
4114inline void operator delete(void *ptr1, void *ptr2)
4115{
4116 (void)ptr1;
4117 (void)ptr2;
4118}
4119
4120inline void operator delete[](void *ptr1, void *ptr2)
4121{
4122 (void)ptr1;
4123 (void)ptr2;
4124}
4125
4126inline void *operator new(size_t size, void *ptr)
4127{
4128 (void)size;
4129 return ptr;
4130}
4131
4132inline void *operator new[](size_t size, void *ptr)
4133{
4134 (void)size;
4135 return ptr;
4136}
4137
4138#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4139
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004140#endif /* !_ASMLANGUAGE */
4141
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004142#endif /* _kernel__h_ */