blob: d224e1aa4bf3dbd5f2fe80f55975f758121037eb [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
Andrew Boie3f091b52017-08-30 14:34:14 -0700513#ifdef CONFIG_USERSPACE
514/**
515 * @brief Drop a thread's privileges permanently to user mode
516 *
517 * @param entry Function to start executing from
518 * @param p1 1st entry point parameter
519 * @param p2 2nd entry point parameter
520 * @param p3 3rd entry point parameter
521 */
522extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
523 void *p1, void *p2,
524 void *p3);
525#endif
526
Andrew Boied26cf2d2017-03-30 13:07:02 -0700527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500528 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400529 *
Allan Stephensc98da842016-11-11 15:45:03 -0500530 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500531 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500533 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400534 *
535 * @return N/A
536 */
Kumar Galacc334c72017-04-21 10:55:34 -0500537extern void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400538
539/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500540 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400541 *
542 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500543 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400544 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400545 * @return N/A
546 */
Kumar Galacc334c72017-04-21 10:55:34 -0500547extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400548
549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500550 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500552 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400553 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500554 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400555 *
556 * @return N/A
557 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400558extern void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400559
560/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500561 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500563 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500565 * If @a thread is not currently sleeping, the routine has no effect.
566 *
567 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400568 *
569 * @return N/A
570 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400571extern void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400572
573/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500574 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500576 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400577 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400578extern k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400579
580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500581 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500583 * This routine prevents @a thread from executing if it has not yet started
584 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500586 * @param thread ID of thread to cancel.
587 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700588 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500589 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591extern int k_thread_cancel(k_tid_t thread);
592
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400593/**
Allan Stephensc98da842016-11-11 15:45:03 -0500594 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500596 * This routine permanently stops execution of @a thread. The thread is taken
597 * off all kernel queues it is part of (i.e. the ready queue, the timeout
598 * queue, or a kernel object wait queue). However, any kernel resources the
599 * thread might currently own (such as mutexes or memory blocks) are not
600 * released. It is the responsibility of the caller of this routine to ensure
601 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500603 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604 *
605 * @return N/A
606 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400607extern void k_thread_abort(k_tid_t thread);
608
Andrew Boie7d627c52017-08-30 11:01:56 -0700609
610/**
611 * @brief Start an inactive thread
612 *
613 * If a thread was created with K_FOREVER in the delay parameter, it will
614 * not be added to the scheduling queue until this function is called
615 * on it.
616 *
617 * @param thread thread to start
618 */
619extern void k_thread_start(k_tid_t thread);
620
Allan Stephensc98da842016-11-11 15:45:03 -0500621/**
622 * @cond INTERNAL_HIDDEN
623 */
624
Benjamin Walshd211a522016-12-06 11:44:01 -0500625/* timeout has timed out and is not on _timeout_q anymore */
626#define _EXPIRED (-2)
627
628/* timeout is not in use */
629#define _INACTIVE (-1)
630
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400631struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700632 struct k_thread *init_thread;
Andrew Boie507852a2017-07-25 18:47:07 -0700633 k_thread_stack_t init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400634 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700635 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500636 void *init_p1;
637 void *init_p2;
638 void *init_p3;
639 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500640 u32_t init_options;
641 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500642 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500643 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400644};
645
Andrew Boied26cf2d2017-03-30 13:07:02 -0700646#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400647 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500648 prio, options, delay, abort, groups) \
649 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700650 .init_thread = (thread), \
651 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500652 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700653 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400654 .init_p1 = (void *)p1, \
655 .init_p2 = (void *)p2, \
656 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500657 .init_prio = (prio), \
658 .init_options = (options), \
659 .init_delay = (delay), \
660 .init_abort = (abort), \
661 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400662 }
663
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400664/**
Allan Stephensc98da842016-11-11 15:45:03 -0500665 * INTERNAL_HIDDEN @endcond
666 */
667
668/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500669 * @brief Statically define and initialize a thread.
670 *
671 * The thread may be scheduled for immediate execution or a delayed start.
672 *
673 * Thread options are architecture-specific, and can include K_ESSENTIAL,
674 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
675 * them using "|" (the logical OR operator).
676 *
677 * The ID of the thread can be accessed using:
678 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500679 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500680 *
681 * @param name Name of the thread.
682 * @param stack_size Stack size in bytes.
683 * @param entry Thread entry function.
684 * @param p1 1st entry point parameter.
685 * @param p2 2nd entry point parameter.
686 * @param p3 3rd entry point parameter.
687 * @param prio Thread priority.
688 * @param options Thread options.
689 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400690 *
691 * @internal It has been observed that the x86 compiler by default aligns
692 * these _static_thread_data structures to 32-byte boundaries, thereby
693 * wasting space. To work around this, force a 4-byte alignment.
694 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500695#define K_THREAD_DEFINE(name, stack_size, \
696 entry, p1, p2, p3, \
697 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700698 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700699 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500700 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500701 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700702 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
703 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500704 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700705 NULL, 0); \
706 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400707
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500711 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500713 * @param thread ID of thread whose priority is needed.
714 *
715 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400716 */
Allan Stephens399d0ad2016-10-07 13:41:34 -0500717extern int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400718
719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500720 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500722 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400723 *
724 * Rescheduling can occur immediately depending on the priority @a thread is
725 * set to:
726 *
727 * - If its priority is raised above the priority of the caller of this
728 * function, and the caller is preemptible, @a thread will be scheduled in.
729 *
730 * - If the caller operates on itself, it lowers its priority below that of
731 * other threads in the system, and the caller is preemptible, the thread of
732 * highest priority will be scheduled in.
733 *
734 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
735 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
736 * highest priority.
737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500738 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400739 * @param prio New priority.
740 *
741 * @warning Changing the priority of a thread currently involved in mutex
742 * priority inheritance may result in undefined behavior.
743 *
744 * @return N/A
745 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400746extern void k_thread_priority_set(k_tid_t thread, int prio);
747
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500749 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500751 * This routine prevents the kernel scheduler from making @a thread the
752 * current thread. All other internal operations on @a thread are still
753 * performed; for example, any timeout it is waiting on keeps ticking,
754 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500756 * If @a thread is already suspended, the routine has no effect.
757 *
758 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 *
760 * @return N/A
761 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400762extern void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400763
764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500765 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500767 * This routine allows the kernel scheduler to make @a thread the current
768 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * If @a thread is not currently suspended, the routine has no effect.
771 *
772 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400773 *
774 * @return N/A
775 */
Benjamin Walsh71d52282016-09-29 10:49:48 -0400776extern void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400777
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500779 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * This routine specifies how the scheduler will perform time slicing of
782 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * To enable time slicing, @a slice must be non-zero. The scheduler
785 * ensures that no thread runs for more than the specified time limit
786 * before other threads of that priority are given a chance to execute.
787 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700788 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 * execute. Once the scheduler selects a thread for execution, there is no
792 * minimum guaranteed time the thread will execute before threads of greater or
793 * equal priority are scheduled.
794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 * for execution, this routine has no effect; the thread is immediately
797 * rescheduled after the slice period expires.
798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * To disable timeslicing, set both @a slice and @a prio to zero.
800 *
801 * @param slice Maximum time slice length (in milliseconds).
802 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
804 * @return N/A
805 */
Kumar Galacc334c72017-04-21 10:55:34 -0500806extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400807
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808/**
Allan Stephensc98da842016-11-11 15:45:03 -0500809 * @} end defgroup thread_apis
810 */
811
812/**
813 * @addtogroup isr_apis
814 * @{
815 */
816
817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephensc98da842016-11-11 15:45:03 -0500820 * This routine allows the caller to customize its actions, depending on
821 * whether it is a thread or an ISR.
822 *
823 * @note Can be called by ISRs.
824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * @return 0 if invoked by a thread.
826 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500828extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400829
Benjamin Walsh445830d2016-11-10 15:54:27 -0500830/**
831 * @brief Determine if code is running in a preemptible thread.
832 *
Allan Stephensc98da842016-11-11 15:45:03 -0500833 * This routine allows the caller to customize its actions, depending on
834 * whether it can be preempted by another thread. The routine returns a 'true'
835 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500836 *
Allan Stephensc98da842016-11-11 15:45:03 -0500837 * - The code is running in a thread, not at ISR.
838 * - The thread's priority is in the preemptible range.
839 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500840 *
Allan Stephensc98da842016-11-11 15:45:03 -0500841 * @note Can be called by ISRs.
842 *
843 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500844 * @return Non-zero if invoked by a preemptible thread.
845 */
846extern int k_is_preempt_thread(void);
847
Allan Stephensc98da842016-11-11 15:45:03 -0500848/**
849 * @} end addtogroup isr_apis
850 */
851
852/**
853 * @addtogroup thread_apis
854 * @{
855 */
856
857/**
858 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500859 *
Allan Stephensc98da842016-11-11 15:45:03 -0500860 * This routine prevents the current thread from being preempted by another
861 * thread by instructing the scheduler to treat it as a cooperative thread.
862 * If the thread subsequently performs an operation that makes it unready,
863 * it will be context switched out in the normal manner. When the thread
864 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500865 *
Allan Stephensc98da842016-11-11 15:45:03 -0500866 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500867 *
Allan Stephensc98da842016-11-11 15:45:03 -0500868 * @note k_sched_lock() and k_sched_unlock() should normally be used
869 * when the operation being performed can be safely interrupted by ISRs.
870 * However, if the amount of processing involved is very small, better
871 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500872 *
873 * @return N/A
874 */
875extern void k_sched_lock(void);
876
Allan Stephensc98da842016-11-11 15:45:03 -0500877/**
878 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500879 *
Allan Stephensc98da842016-11-11 15:45:03 -0500880 * This routine reverses the effect of a previous call to k_sched_lock().
881 * A thread must call the routine once for each time it called k_sched_lock()
882 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500883 *
884 * @return N/A
885 */
886extern void k_sched_unlock(void);
887
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * Custom data is not used by the kernel itself, and is freely available
894 * for a thread to use as it sees fit. It can be used as a framework
895 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 *
899 * @return N/A
900 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400901extern void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400902
903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500904 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500906 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500908 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400909 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400910extern void *k_thread_custom_data_get(void);
911
912/**
Allan Stephensc98da842016-11-11 15:45:03 -0500913 * @} end addtogroup thread_apis
914 */
915
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400916#include <sys_clock.h>
917
Allan Stephensc2f15a42016-11-17 12:24:22 -0500918/**
919 * @addtogroup clock_apis
920 * @{
921 */
922
923/**
924 * @brief Generate null timeout delay.
925 *
926 * This macro generates a timeout delay that that instructs a kernel API
927 * not to wait if the requested operation cannot be performed immediately.
928 *
929 * @return Timeout delay value.
930 */
931#define K_NO_WAIT 0
932
933/**
934 * @brief Generate timeout delay from milliseconds.
935 *
936 * This macro generates a timeout delay that that instructs a kernel API
937 * to wait up to @a ms milliseconds to perform the requested operation.
938 *
939 * @param ms Duration in milliseconds.
940 *
941 * @return Timeout delay value.
942 */
Johan Hedberg14471692016-11-13 10:52:15 +0200943#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500944
945/**
946 * @brief Generate timeout delay from seconds.
947 *
948 * This macro generates a timeout delay that that instructs a kernel API
949 * to wait up to @a s seconds to perform the requested operation.
950 *
951 * @param s Duration in seconds.
952 *
953 * @return Timeout delay value.
954 */
Johan Hedberg14471692016-11-13 10:52:15 +0200955#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500956
957/**
958 * @brief Generate timeout delay from minutes.
959 *
960 * This macro generates a timeout delay that that instructs a kernel API
961 * to wait up to @a m minutes to perform the requested operation.
962 *
963 * @param m Duration in minutes.
964 *
965 * @return Timeout delay value.
966 */
Johan Hedberg14471692016-11-13 10:52:15 +0200967#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -0500968
969/**
970 * @brief Generate timeout delay from hours.
971 *
972 * This macro generates a timeout delay that that instructs a kernel API
973 * to wait up to @a h hours to perform the requested operation.
974 *
975 * @param h Duration in hours.
976 *
977 * @return Timeout delay value.
978 */
Johan Hedberg14471692016-11-13 10:52:15 +0200979#define K_HOURS(h) K_MINUTES((h) * 60)
980
Allan Stephensc98da842016-11-11 15:45:03 -0500981/**
Allan Stephensc2f15a42016-11-17 12:24:22 -0500982 * @brief Generate infinite timeout delay.
983 *
984 * This macro generates a timeout delay that that instructs a kernel API
985 * to wait as long as necessary to perform the requested operation.
986 *
987 * @return Timeout delay value.
988 */
989#define K_FOREVER (-1)
990
991/**
992 * @} end addtogroup clock_apis
993 */
994
995/**
Allan Stephensc98da842016-11-11 15:45:03 -0500996 * @cond INTERNAL_HIDDEN
997 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -0400998
Benjamin Walsh62092182016-12-20 14:39:08 -0500999/* kernel clocks */
1000
1001#if (sys_clock_ticks_per_sec == 1000) || \
1002 (sys_clock_ticks_per_sec == 500) || \
1003 (sys_clock_ticks_per_sec == 250) || \
1004 (sys_clock_ticks_per_sec == 125) || \
1005 (sys_clock_ticks_per_sec == 100) || \
1006 (sys_clock_ticks_per_sec == 50) || \
1007 (sys_clock_ticks_per_sec == 25) || \
1008 (sys_clock_ticks_per_sec == 20) || \
1009 (sys_clock_ticks_per_sec == 10) || \
1010 (sys_clock_ticks_per_sec == 1)
1011
1012 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1013#else
1014 /* yields horrible 64-bit math on many architectures: try to avoid */
1015 #define _NON_OPTIMIZED_TICKS_PER_SEC
1016#endif
1017
1018#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001019extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001020#else
Kumar Galacc334c72017-04-21 10:55:34 -05001021static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001022{
Kumar Galacc334c72017-04-21 10:55:34 -05001023 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001024}
1025#endif
1026
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001027/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001028#ifdef CONFIG_TICKLESS_KERNEL
1029#define _TICK_ALIGN 0
1030#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001031#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001032#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001033
Kumar Galacc334c72017-04-21 10:55:34 -05001034static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001035{
Benjamin Walsh62092182016-12-20 14:39:08 -05001036#ifdef CONFIG_SYS_CLOCK_EXISTS
1037
1038#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001039 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001040#else
Kumar Galacc334c72017-04-21 10:55:34 -05001041 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001042#endif
1043
1044#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001045 __ASSERT(ticks == 0, "");
1046 return 0;
1047#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001048}
1049
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001050struct k_timer {
1051 /*
1052 * _timeout structure must be first here if we want to use
1053 * dynamic timer allocation. timeout.node is used in the double-linked
1054 * list of free timers
1055 */
1056 struct _timeout timeout;
1057
Allan Stephens45bfa372016-10-12 12:39:42 -05001058 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001059 _wait_q_t wait_q;
1060
1061 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001062 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063
1064 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001065 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066
1067 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001068 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001069
Allan Stephens45bfa372016-10-12 12:39:42 -05001070 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001071 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001072
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001073 /* user-specific data, also used to support legacy features */
1074 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001075
Anas Nashif2f203c22016-12-18 06:57:45 -05001076 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077};
1078
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001079#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001080 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001081 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001082 .timeout.wait_q = NULL, \
1083 .timeout.thread = NULL, \
1084 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001085 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001086 .expiry_fn = expiry, \
1087 .stop_fn = stop, \
1088 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001089 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001090 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001091 }
1092
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001093#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1094
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095/**
Allan Stephensc98da842016-11-11 15:45:03 -05001096 * INTERNAL_HIDDEN @endcond
1097 */
1098
1099/**
1100 * @defgroup timer_apis Timer APIs
1101 * @ingroup kernel_apis
1102 * @{
1103 */
1104
1105/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001106 * @typedef k_timer_expiry_t
1107 * @brief Timer expiry function type.
1108 *
1109 * A timer's expiry function is executed by the system clock interrupt handler
1110 * each time the timer expires. The expiry function is optional, and is only
1111 * invoked if the timer has been initialized with one.
1112 *
1113 * @param timer Address of timer.
1114 *
1115 * @return N/A
1116 */
1117typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1118
1119/**
1120 * @typedef k_timer_stop_t
1121 * @brief Timer stop function type.
1122 *
1123 * A timer's stop function is executed if the timer is stopped prematurely.
1124 * The function runs in the context of the thread that stops the timer.
1125 * The stop function is optional, and is only invoked if the timer has been
1126 * initialized with one.
1127 *
1128 * @param timer Address of timer.
1129 *
1130 * @return N/A
1131 */
1132typedef void (*k_timer_stop_t)(struct k_timer *timer);
1133
1134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001139 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140 *
1141 * @param name Name of the timer variable.
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.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001144 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001145#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001146 struct k_timer name \
1147 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001148 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001149
Allan Stephens45bfa372016-10-12 12:39:42 -05001150/**
1151 * @brief Initialize a timer.
1152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001153 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001154 *
1155 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * @param expiry_fn Function to invoke each time the timer expires.
1157 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001158 *
1159 * @return N/A
1160 */
1161extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001162 k_timer_expiry_t expiry_fn,
1163 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001164
Allan Stephens45bfa372016-10-12 12:39:42 -05001165/**
1166 * @brief Start a timer.
1167 *
1168 * This routine starts a timer, and resets its status to zero. The timer
1169 * begins counting down using the specified duration and period values.
1170 *
1171 * Attempting to start a timer that is already running is permitted.
1172 * The timer's status is reset to zero and the timer begins counting down
1173 * using the new duration and period values.
1174 *
1175 * @param timer Address of timer.
1176 * @param duration Initial timer duration (in milliseconds).
1177 * @param period Timer period (in milliseconds).
1178 *
1179 * @return N/A
1180 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001181extern void k_timer_start(struct k_timer *timer,
Kumar Galacc334c72017-04-21 10:55:34 -05001182 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001183
1184/**
1185 * @brief Stop a timer.
1186 *
1187 * This routine stops a running timer prematurely. The timer's stop function,
1188 * if one exists, is invoked by the caller.
1189 *
1190 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001191 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001192 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001193 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1194 * if @a k_timer_stop is to be called from ISRs.
1195 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001196 * @param timer Address of timer.
1197 *
1198 * @return N/A
1199 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200extern void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001201
1202/**
1203 * @brief Read timer status.
1204 *
1205 * This routine reads the timer's status, which indicates the number of times
1206 * it has expired since its status was last read.
1207 *
1208 * Calling this routine resets the timer's status to zero.
1209 *
1210 * @param timer Address of timer.
1211 *
1212 * @return Timer status.
1213 */
Kumar Galacc334c72017-04-21 10:55:34 -05001214extern u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001215
1216/**
1217 * @brief Synchronize thread to timer expiration.
1218 *
1219 * This routine blocks the calling thread until the timer's status is non-zero
1220 * (indicating that it has expired at least once since it was last examined)
1221 * or the timer is stopped. If the timer status is already non-zero,
1222 * or the timer is already stopped, the caller continues without waiting.
1223 *
1224 * Calling this routine resets the timer's status to zero.
1225 *
1226 * This routine must not be used by interrupt handlers, since they are not
1227 * allowed to block.
1228 *
1229 * @param timer Address of timer.
1230 *
1231 * @return Timer status.
1232 */
Kumar Galacc334c72017-04-21 10:55:34 -05001233extern u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001234
1235/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001237 *
1238 * This routine computes the (approximate) time remaining before a running
1239 * timer next expires. If the timer is not running, it returns zero.
1240 *
1241 * @param timer Address of timer.
1242 *
1243 * @return Remaining time (in milliseconds).
1244 */
Kumar Galacc334c72017-04-21 10:55:34 -05001245static inline s32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001246{
1247 return _timeout_remaining_get(&timer->timeout);
1248}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001249
Allan Stephensc98da842016-11-11 15:45:03 -05001250/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001251 * @brief Associate user-specific data with a timer.
1252 *
1253 * This routine records the @a user_data with the @a timer, to be retrieved
1254 * later.
1255 *
1256 * It can be used e.g. in a timer handler shared across multiple subsystems to
1257 * retrieve data specific to the subsystem this timer is associated with.
1258 *
1259 * @param timer Address of timer.
1260 * @param user_data User data to associate with the timer.
1261 *
1262 * @return N/A
1263 */
1264static inline void k_timer_user_data_set(struct k_timer *timer,
1265 void *user_data)
1266{
1267 timer->user_data = user_data;
1268}
1269
1270/**
1271 * @brief Retrieve the user-specific data from a timer.
1272 *
1273 * @param timer Address of timer.
1274 *
1275 * @return The user data.
1276 */
1277static inline void *k_timer_user_data_get(struct k_timer *timer)
1278{
1279 return timer->user_data;
1280}
1281
1282/**
Allan Stephensc98da842016-11-11 15:45:03 -05001283 * @} end defgroup timer_apis
1284 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001285
Allan Stephensc98da842016-11-11 15:45:03 -05001286/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001287 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001288 * @{
1289 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001290
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001291/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * This routine returns the elapsed time since the system booted,
1295 * in milliseconds.
1296 *
1297 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001298 */
Kumar Galacc334c72017-04-21 10:55:34 -05001299extern s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001300
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001301#ifdef CONFIG_TICKLESS_KERNEL
1302/**
1303 * @brief Enable clock always on in tickless kernel
1304 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001305 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001306 * there are no timer events programmed in tickless kernel
1307 * scheduling. This is necessary if the clock is used to track
1308 * passage of time.
1309 *
1310 * @retval prev_status Previous status of always on flag
1311 */
1312static inline int k_enable_sys_clock_always_on(void)
1313{
1314 int prev_status = _sys_clock_always_on;
1315
1316 _sys_clock_always_on = 1;
1317 _enable_sys_clock();
1318
1319 return prev_status;
1320}
1321
1322/**
1323 * @brief Disable clock always on in tickless kernel
1324 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001325 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001326 * there are no timer events programmed in tickless kernel
1327 * scheduling. To save power, this routine should be called
1328 * immediately when clock is not used to track time.
1329 */
1330static inline void k_disable_sys_clock_always_on(void)
1331{
1332 _sys_clock_always_on = 0;
1333}
1334#else
1335#define k_enable_sys_clock_always_on() do { } while ((0))
1336#define k_disable_sys_clock_always_on() do { } while ((0))
1337#endif
1338
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001339/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001340 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001342 * This routine returns the lower 32-bits of the elapsed time since the system
1343 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001345 * This routine can be more efficient than k_uptime_get(), as it reduces the
1346 * need for interrupt locking and 64-bit math. However, the 32-bit result
1347 * cannot hold a system uptime time larger than approximately 50 days, so the
1348 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001350 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001351 */
Kumar Galacc334c72017-04-21 10:55:34 -05001352extern u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001353
1354/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001355 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001357 * This routine computes the elapsed time between the current system uptime
1358 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001360 * @param reftime Pointer to a reference time, which is updated to the current
1361 * uptime upon return.
1362 *
1363 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001364 */
Kumar Galacc334c72017-04-21 10:55:34 -05001365extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001366
1367/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001368 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001370 * This routine computes the elapsed time between the current system uptime
1371 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001373 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1374 * need for interrupt locking and 64-bit math. However, the 32-bit result
1375 * cannot hold an elapsed time larger than approximately 50 days, so the
1376 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * @param reftime Pointer to a reference time, which is updated to the current
1379 * uptime upon return.
1380 *
1381 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001382 */
Kumar Galacc334c72017-04-21 10:55:34 -05001383extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001384
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001385/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * This routine returns the current time, as measured by the system's hardware
1389 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001392 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001393#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001396 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397 */
1398
Allan Stephensc98da842016-11-11 15:45:03 -05001399/**
1400 * @cond INTERNAL_HIDDEN
1401 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001403struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001404 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001405 union {
1406 _wait_q_t wait_q;
1407
1408 _POLL_EVENT;
1409 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001410
1411 _OBJECT_TRACING_NEXT_PTR(k_queue);
1412};
1413
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001414#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001415 { \
1416 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1417 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001418 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001419 _OBJECT_TRACING_INIT \
1420 }
1421
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001422#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1423
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001424/**
1425 * INTERNAL_HIDDEN @endcond
1426 */
1427
1428/**
1429 * @defgroup queue_apis Queue APIs
1430 * @ingroup kernel_apis
1431 * @{
1432 */
1433
1434/**
1435 * @brief Initialize a queue.
1436 *
1437 * This routine initializes a queue object, prior to its first use.
1438 *
1439 * @param queue Address of the queue.
1440 *
1441 * @return N/A
1442 */
1443extern void k_queue_init(struct k_queue *queue);
1444
1445/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001446 * @brief Cancel waiting on a queue.
1447 *
1448 * This routine causes first thread pending on @a queue, if any, to
1449 * return from k_queue_get() call with NULL value (as if timeout expired).
1450 *
1451 * @note Can be called by ISRs.
1452 *
1453 * @param queue Address of the queue.
1454 *
1455 * @return N/A
1456 */
1457extern void k_queue_cancel_wait(struct k_queue *queue);
1458
1459/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001460 * @brief Append an element to the end of a queue.
1461 *
1462 * This routine appends a data item to @a queue. A queue data item must be
1463 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1464 * reserved for the kernel's use.
1465 *
1466 * @note Can be called by ISRs.
1467 *
1468 * @param queue Address of the queue.
1469 * @param data Address of the data item.
1470 *
1471 * @return N/A
1472 */
1473extern void k_queue_append(struct k_queue *queue, void *data);
1474
1475/**
1476 * @brief Prepend an element to a queue.
1477 *
1478 * This routine prepends a data item to @a queue. A queue data item must be
1479 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1480 * reserved for the kernel's use.
1481 *
1482 * @note Can be called by ISRs.
1483 *
1484 * @param queue Address of the queue.
1485 * @param data Address of the data item.
1486 *
1487 * @return N/A
1488 */
1489extern void k_queue_prepend(struct k_queue *queue, void *data);
1490
1491/**
1492 * @brief Inserts an element to a queue.
1493 *
1494 * This routine inserts a data item to @a queue after previous item. A queue
1495 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1496 * item are reserved for the kernel's use.
1497 *
1498 * @note Can be called by ISRs.
1499 *
1500 * @param queue Address of the queue.
1501 * @param prev Address of the previous data item.
1502 * @param data Address of the data item.
1503 *
1504 * @return N/A
1505 */
1506extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1507
1508/**
1509 * @brief Atomically append a list of elements to a queue.
1510 *
1511 * This routine adds a list of data items to @a queue in one operation.
1512 * The data items must be in a singly-linked list, with the first 32 bits
1513 * in each data item pointing to the next data item; the list must be
1514 * NULL-terminated.
1515 *
1516 * @note Can be called by ISRs.
1517 *
1518 * @param queue Address of the queue.
1519 * @param head Pointer to first node in singly-linked list.
1520 * @param tail Pointer to last node in singly-linked list.
1521 *
1522 * @return N/A
1523 */
1524extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1525
1526/**
1527 * @brief Atomically add a list of elements to a queue.
1528 *
1529 * This routine adds a list of data items to @a queue in one operation.
1530 * The data items must be in a singly-linked list implemented using a
1531 * sys_slist_t object. Upon completion, the original list is empty.
1532 *
1533 * @note Can be called by ISRs.
1534 *
1535 * @param queue Address of the queue.
1536 * @param list Pointer to sys_slist_t object.
1537 *
1538 * @return N/A
1539 */
1540extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1541
1542/**
1543 * @brief Get an element from a queue.
1544 *
1545 * This routine removes first data item from @a queue. The first 32 bits of the
1546 * data item are reserved for the kernel's use.
1547 *
1548 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1549 *
1550 * @param queue Address of the queue.
1551 * @param timeout Waiting period to obtain a data item (in milliseconds),
1552 * or one of the special values K_NO_WAIT and K_FOREVER.
1553 *
1554 * @return Address of the data item if successful; NULL if returned
1555 * without waiting, or waiting period timed out.
1556 */
Kumar Galacc334c72017-04-21 10:55:34 -05001557extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001558
1559/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001560 * @brief Remove an element from a queue.
1561 *
1562 * This routine removes data item from @a queue. The first 32 bits of the
1563 * data item are reserved for the kernel's use. Removing elements from k_queue
1564 * rely on sys_slist_find_and_remove which is not a constant time operation.
1565 *
1566 * @note Can be called by ISRs
1567 *
1568 * @param queue Address of the queue.
1569 * @param data Address of the data item.
1570 *
1571 * @return true if data item was removed
1572 */
1573static inline bool k_queue_remove(struct k_queue *queue, void *data)
1574{
1575 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1576}
1577
1578/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001579 * @brief Query a queue to see if it has data available.
1580 *
1581 * Note that the data might be already gone by the time this function returns
1582 * if other threads are also trying to read from the queue.
1583 *
1584 * @note Can be called by ISRs.
1585 *
1586 * @param queue Address of the queue.
1587 *
1588 * @return Non-zero if the queue is empty.
1589 * @return 0 if data is available.
1590 */
1591static inline int k_queue_is_empty(struct k_queue *queue)
1592{
1593 return (int)sys_slist_is_empty(&queue->data_q);
1594}
1595
1596/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001597 * @brief Peek element at the head of queue.
1598 *
1599 * Return element from the head of queue without removing it.
1600 *
1601 * @param queue Address of the queue.
1602 *
1603 * @return Head element, or NULL if queue is empty.
1604 */
1605static inline void *k_queue_peek_head(struct k_queue *queue)
1606{
1607 return sys_slist_peek_head(&queue->data_q);
1608}
1609
1610/**
1611 * @brief Peek element at the tail of queue.
1612 *
1613 * Return element from the tail of queue without removing it.
1614 *
1615 * @param queue Address of the queue.
1616 *
1617 * @return Tail element, or NULL if queue is empty.
1618 */
1619static inline void *k_queue_peek_tail(struct k_queue *queue)
1620{
1621 return sys_slist_peek_tail(&queue->data_q);
1622}
1623
1624/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001625 * @brief Statically define and initialize a queue.
1626 *
1627 * The queue can be accessed outside the module where it is defined using:
1628 *
1629 * @code extern struct k_queue <name>; @endcode
1630 *
1631 * @param name Name of the queue.
1632 */
1633#define K_QUEUE_DEFINE(name) \
1634 struct k_queue name \
1635 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001636 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001637
1638/**
1639 * @} end defgroup queue_apis
1640 */
1641
1642/**
1643 * @cond INTERNAL_HIDDEN
1644 */
1645
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001646struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001647 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648};
1649
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001650#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001651 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001652 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001653 }
1654
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001655#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1656
Allan Stephensc98da842016-11-11 15:45:03 -05001657/**
1658 * INTERNAL_HIDDEN @endcond
1659 */
1660
1661/**
1662 * @defgroup fifo_apis Fifo APIs
1663 * @ingroup kernel_apis
1664 * @{
1665 */
1666
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001667/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001670 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001673 *
1674 * @return N/A
1675 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001676#define k_fifo_init(fifo) \
1677 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001678
1679/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001680 * @brief Cancel waiting on a fifo.
1681 *
1682 * This routine causes first thread pending on @a fifo, if any, to
1683 * return from k_fifo_get() call with NULL value (as if timeout
1684 * expired).
1685 *
1686 * @note Can be called by ISRs.
1687 *
1688 * @param fifo Address of the fifo.
1689 *
1690 * @return N/A
1691 */
1692#define k_fifo_cancel_wait(fifo) \
1693 k_queue_cancel_wait((struct k_queue *) fifo)
1694
1695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001698 * This routine adds a data item to @a fifo. A fifo data item must be
1699 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1700 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702 * @note Can be called by ISRs.
1703 *
1704 * @param fifo Address of the fifo.
1705 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001706 *
1707 * @return N/A
1708 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001709#define k_fifo_put(fifo, data) \
1710 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001711
1712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * This routine adds a list of data items to @a fifo in one operation.
1716 * The data items must be in a singly-linked list, with the first 32 bits
1717 * each data item pointing to the next data item; the list must be
1718 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * @param fifo Address of the fifo.
1723 * @param head Pointer to first node in singly-linked list.
1724 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001725 *
1726 * @return N/A
1727 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001728#define k_fifo_put_list(fifo, head, tail) \
1729 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730
1731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * This routine adds a list of data items to @a fifo in one operation.
1735 * The data items must be in a singly-linked list implemented using a
1736 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 * and must be re-initialized via sys_slist_init().
1738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * @note Can be called by ISRs.
1740 *
1741 * @param fifo Address of the fifo.
1742 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 *
1744 * @return N/A
1745 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001746#define k_fifo_put_slist(fifo, list) \
1747 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748
1749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001750 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * This routine removes a data item from @a fifo in a "first in, first out"
1753 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1756 *
1757 * @param fifo Address of the fifo.
1758 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 * or one of the special values K_NO_WAIT and K_FOREVER.
1760 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001761 * @return Address of the data item if successful; NULL if returned
1762 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001764#define k_fifo_get(fifo, timeout) \
1765 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001767/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001768 * @brief Query a fifo to see if it has data available.
1769 *
1770 * Note that the data might be already gone by the time this function returns
1771 * if other threads is also trying to read from the fifo.
1772 *
1773 * @note Can be called by ISRs.
1774 *
1775 * @param fifo Address of the fifo.
1776 *
1777 * @return Non-zero if the fifo is empty.
1778 * @return 0 if data is available.
1779 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001780#define k_fifo_is_empty(fifo) \
1781 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001782
1783/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001784 * @brief Peek element at the head of fifo.
1785 *
1786 * Return element from the head of fifo without removing it. A usecase
1787 * for this is if elements of the fifo are themselves containers. Then
1788 * on each iteration of processing, a head container will be peeked,
1789 * and some data processed out of it, and only if the container is empty,
1790 * it will be completely remove from the fifo.
1791 *
1792 * @param fifo Address of the fifo.
1793 *
1794 * @return Head element, or NULL if the fifo is empty.
1795 */
1796#define k_fifo_peek_head(fifo) \
1797 k_queue_peek_head((struct k_queue *) fifo)
1798
1799/**
1800 * @brief Peek element at the tail of fifo.
1801 *
1802 * Return element from the tail of fifo (without removing it). A usecase
1803 * for this is if elements of the fifo are themselves containers. Then
1804 * it may be useful to add more data to the last container in fifo.
1805 *
1806 * @param fifo Address of the fifo.
1807 *
1808 * @return Tail element, or NULL if fifo is empty.
1809 */
1810#define k_fifo_peek_tail(fifo) \
1811 k_queue_peek_tail((struct k_queue *) fifo)
1812
1813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001814 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001818 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001821 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001822#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001823 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001824 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001825 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001826
Allan Stephensc98da842016-11-11 15:45:03 -05001827/**
1828 * @} end defgroup fifo_apis
1829 */
1830
1831/**
1832 * @cond INTERNAL_HIDDEN
1833 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001834
1835struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001836 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837};
1838
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001839#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001840 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001841 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001842 }
1843
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001844#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1845
Allan Stephensc98da842016-11-11 15:45:03 -05001846/**
1847 * INTERNAL_HIDDEN @endcond
1848 */
1849
1850/**
1851 * @defgroup lifo_apis Lifo APIs
1852 * @ingroup kernel_apis
1853 * @{
1854 */
1855
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001857 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001859 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001861 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001862 *
1863 * @return N/A
1864 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001865#define k_lifo_init(lifo) \
1866 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867
1868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001869 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001871 * This routine adds a data item to @a lifo. A lifo data item must be
1872 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1873 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * @note Can be called by ISRs.
1876 *
1877 * @param lifo Address of the lifo.
1878 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 *
1880 * @return N/A
1881 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001882#define k_lifo_put(lifo, data) \
1883 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001884
1885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001886 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001888 * This routine removes a data item from @a lifo in a "last in, first out"
1889 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1892 *
1893 * @param lifo Address of the lifo.
1894 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 * or one of the special values K_NO_WAIT and K_FOREVER.
1896 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001897 * @return Address of the data item if successful; NULL if returned
1898 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001899 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001900#define k_lifo_get(lifo, timeout) \
1901 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001904 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001906 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001907 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001908 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001910 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001911 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001912#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001913 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001914 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001915 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001916
Allan Stephensc98da842016-11-11 15:45:03 -05001917/**
1918 * @} end defgroup lifo_apis
1919 */
1920
1921/**
1922 * @cond INTERNAL_HIDDEN
1923 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924
1925struct k_stack {
1926 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05001927 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001928
Anas Nashif2f203c22016-12-18 06:57:45 -05001929 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930};
1931
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001932#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05001933 { \
1934 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1935 .base = stack_buffer, \
1936 .next = stack_buffer, \
1937 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001938 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05001939 }
1940
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001941#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
1942
Allan Stephensc98da842016-11-11 15:45:03 -05001943/**
1944 * INTERNAL_HIDDEN @endcond
1945 */
1946
1947/**
1948 * @defgroup stack_apis Stack APIs
1949 * @ingroup kernel_apis
1950 * @{
1951 */
1952
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001953/**
1954 * @brief Initialize a stack.
1955 *
1956 * This routine initializes a stack object, prior to its first use.
1957 *
1958 * @param stack Address of the stack.
1959 * @param buffer Address of array used to hold stacked values.
1960 * @param num_entries Maximum number of values that can be stacked.
1961 *
1962 * @return N/A
1963 */
Allan Stephens018cd9a2016-10-07 15:13:24 -05001964extern void k_stack_init(struct k_stack *stack,
Kumar Galacc334c72017-04-21 10:55:34 -05001965 u32_t *buffer, int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001966
1967/**
1968 * @brief Push an element onto a stack.
1969 *
1970 * This routine adds a 32-bit value @a data to @a stack.
1971 *
1972 * @note Can be called by ISRs.
1973 *
1974 * @param stack Address of the stack.
1975 * @param data Value to push onto the stack.
1976 *
1977 * @return N/A
1978 */
Kumar Galacc334c72017-04-21 10:55:34 -05001979extern void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980
1981/**
1982 * @brief Pop an element from a stack.
1983 *
1984 * This routine removes a 32-bit value from @a stack in a "last in, first out"
1985 * manner and stores the value in @a data.
1986 *
1987 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1988 *
1989 * @param stack Address of the stack.
1990 * @param data Address of area to hold the value popped from the stack.
1991 * @param timeout Waiting period to obtain a value (in milliseconds),
1992 * or one of the special values K_NO_WAIT and K_FOREVER.
1993 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001994 * @retval 0 Element popped from stack.
1995 * @retval -EBUSY Returned without waiting.
1996 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001997 */
Kumar Galacc334c72017-04-21 10:55:34 -05001998extern int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002005 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @param name Name of the stack.
2008 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002010#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002011 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002012 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002013 struct k_stack name \
2014 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002015 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002016 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002017
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018/**
Allan Stephensc98da842016-11-11 15:45:03 -05002019 * @} end defgroup stack_apis
2020 */
2021
Allan Stephens6bba9b02016-11-16 14:56:54 -05002022struct k_work;
2023
Allan Stephensc98da842016-11-11 15:45:03 -05002024/**
2025 * @defgroup workqueue_apis Workqueue Thread APIs
2026 * @ingroup kernel_apis
2027 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028 */
2029
Allan Stephens6bba9b02016-11-16 14:56:54 -05002030/**
2031 * @typedef k_work_handler_t
2032 * @brief Work item handler function type.
2033 *
2034 * A work item's handler function is executed by a workqueue's thread
2035 * when the work item is processed by the workqueue.
2036 *
2037 * @param work Address of the work item.
2038 *
2039 * @return N/A
2040 */
2041typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002042
2043/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002044 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002045 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002046
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002048 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002049 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002050};
2051
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002052enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002053 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002054};
2055
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002057 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058 k_work_handler_t handler;
2059 atomic_t flags[1];
2060};
2061
Allan Stephens6bba9b02016-11-16 14:56:54 -05002062struct k_delayed_work {
2063 struct k_work work;
2064 struct _timeout timeout;
2065 struct k_work_q *work_q;
2066};
2067
2068extern struct k_work_q k_sys_work_q;
2069
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002070/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002071 * INTERNAL_HIDDEN @endcond
2072 */
2073
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002074#define _K_WORK_INITIALIZER(work_handler) \
2075 { \
2076 ._reserved = NULL, \
2077 .handler = work_handler, \
2078 .flags = { 0 } \
2079 }
2080
2081#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2082
Allan Stephens6bba9b02016-11-16 14:56:54 -05002083/**
2084 * @brief Initialize a statically-defined work item.
2085 *
2086 * This macro can be used to initialize a statically-defined workqueue work
2087 * item, prior to its first use. For example,
2088 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002089 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002090 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002091 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002092 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002094#define K_WORK_DEFINE(work, work_handler) \
2095 struct k_work work \
2096 __in_section(_k_work, static, work) = \
2097 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002098
2099/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002100 * @brief Initialize a work item.
2101 *
2102 * This routine initializes a workqueue work item, prior to its first use.
2103 *
2104 * @param work Address of work item.
2105 * @param handler Function to invoke each time work item is processed.
2106 *
2107 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002108 */
2109static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2110{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002111 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002112 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002113 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114}
2115
2116/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002117 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002118 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002119 * This routine submits work item @a work to be processed by workqueue
2120 * @a work_q. If the work item is already pending in the workqueue's queue
2121 * as a result of an earlier submission, this routine has no effect on the
2122 * work item. If the work item has already been processed, or is currently
2123 * being processed, its work is considered complete and the work item can be
2124 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002125 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002126 * @warning
2127 * A submitted work item must not be modified until it has been processed
2128 * by the workqueue.
2129 *
2130 * @note Can be called by ISRs.
2131 *
2132 * @param work_q Address of workqueue.
2133 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002134 *
2135 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002136 */
2137static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2138 struct k_work *work)
2139{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002140 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002141 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002142 }
2143}
2144
2145/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002146 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002147 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148 * This routine indicates if work item @a work is pending in a workqueue's
2149 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002151 * @note Can be called by ISRs.
2152 *
2153 * @param work Address of work item.
2154 *
2155 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002156 */
2157static inline int k_work_pending(struct k_work *work)
2158{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002159 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002160}
2161
2162/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002163 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002165 * This routine starts workqueue @a work_q. The workqueue spawns its work
2166 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002167 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002168 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002169 * @param stack Pointer to work queue thread's stack space, as defined by
2170 * K_THREAD_STACK_DEFINE()
2171 * @param stack_size Size of the work queue thread's stack (in bytes), which
2172 * should either be the same constant passed to
2173 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002174 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175 *
2176 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002177 */
Andrew Boie507852a2017-07-25 18:47:07 -07002178extern void k_work_q_start(struct k_work_q *work_q,
2179 k_thread_stack_t stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002180 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002183 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002184 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002185 * This routine initializes a workqueue delayed work item, prior to
2186 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002187 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002188 * @param work Address of delayed work item.
2189 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002190 *
2191 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002193extern void k_delayed_work_init(struct k_delayed_work *work,
2194 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195
2196/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002197 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002199 * This routine schedules work item @a work to be processed by workqueue
2200 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002201 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002202 * Only when the countdown completes is the work item actually submitted to
2203 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002204 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002205 * Submitting a previously submitted delayed work item that is still
2206 * counting down cancels the existing submission and restarts the countdown
2207 * using the new delay. If the work item is currently pending on the
2208 * workqueue's queue because the countdown has completed it is too late to
2209 * resubmit the item, and resubmission fails without impacting the work item.
2210 * If the work item has already been processed, or is currently being processed,
2211 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002213 * @warning
2214 * A delayed work item must not be modified until it has been processed
2215 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002217 * @note Can be called by ISRs.
2218 *
2219 * @param work_q Address of workqueue.
2220 * @param work Address of delayed work item.
2221 * @param delay Delay before submitting the work item (in milliseconds).
2222 *
2223 * @retval 0 Work item countdown started.
2224 * @retval -EINPROGRESS Work item is already pending.
2225 * @retval -EINVAL Work item is being processed or has completed its work.
2226 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002227 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002228extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2229 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002230 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231
2232/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002233 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002235 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002236 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002237 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002239 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002241 * @param work Address of delayed work item.
2242 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002243 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002244 * @retval -EINPROGRESS Work item is already pending.
2245 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002247extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250 * @brief Submit a work item to the system workqueue.
2251 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002252 * This routine submits work item @a work to be processed by the system
2253 * workqueue. If the work item is already pending in the workqueue's queue
2254 * as a result of an earlier submission, this routine has no effect on the
2255 * work item. If the work item has already been processed, or is currently
2256 * being processed, its work is considered complete and the work item can be
2257 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002259 * @warning
2260 * Work items submitted to the system workqueue should avoid using handlers
2261 * that block or yield since this may prevent the system workqueue from
2262 * processing other work items in a timely manner.
2263 *
2264 * @note Can be called by ISRs.
2265 *
2266 * @param work Address of work item.
2267 *
2268 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002269 */
2270static inline void k_work_submit(struct k_work *work)
2271{
2272 k_work_submit_to_queue(&k_sys_work_q, work);
2273}
2274
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002276 * @brief Submit a delayed work item to the system workqueue.
2277 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * This routine schedules work item @a work to be processed by the system
2279 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002280 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002281 * Only when the countdown completes is the work item actually submitted to
2282 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002283 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002284 * Submitting a previously submitted delayed work item that is still
2285 * counting down cancels the existing submission and restarts the countdown
2286 * using the new delay. If the work item is currently pending on the
2287 * workqueue's queue because the countdown has completed it is too late to
2288 * resubmit the item, and resubmission fails without impacting the work item.
2289 * If the work item has already been processed, or is currently being processed,
2290 * its work is considered complete and the work item can be resubmitted.
2291 *
2292 * @warning
2293 * Work items submitted to the system workqueue should avoid using handlers
2294 * that block or yield since this may prevent the system workqueue from
2295 * processing other work items in a timely manner.
2296 *
2297 * @note Can be called by ISRs.
2298 *
2299 * @param work Address of delayed work item.
2300 * @param delay Delay before submitting the work item (in milliseconds).
2301 *
2302 * @retval 0 Work item countdown started.
2303 * @retval -EINPROGRESS Work item is already pending.
2304 * @retval -EINVAL Work item is being processed or has completed its work.
2305 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 */
2307static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002308 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002310 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311}
2312
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002313/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002314 * @brief Get time remaining before a delayed work gets scheduled.
2315 *
2316 * This routine computes the (approximate) time remaining before a
2317 * delayed work gets executed. If the delayed work is not waiting to be
2318 * schedules, it returns zero.
2319 *
2320 * @param work Delayed work item.
2321 *
2322 * @return Remaining time (in milliseconds).
2323 */
Kumar Galacc334c72017-04-21 10:55:34 -05002324static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002325{
2326 return _timeout_remaining_get(&work->timeout);
2327}
2328
2329/**
Allan Stephensc98da842016-11-11 15:45:03 -05002330 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 */
2332
Allan Stephensc98da842016-11-11 15:45:03 -05002333/**
2334 * @cond INTERNAL_HIDDEN
2335 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336
2337struct k_mutex {
2338 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002339 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002340 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342
Anas Nashif2f203c22016-12-18 06:57:45 -05002343 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344};
2345
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002346#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347 { \
2348 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2349 .owner = NULL, \
2350 .lock_count = 0, \
2351 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002352 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353 }
2354
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002355#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2356
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357/**
Allan Stephensc98da842016-11-11 15:45:03 -05002358 * INTERNAL_HIDDEN @endcond
2359 */
2360
2361/**
2362 * @defgroup mutex_apis Mutex APIs
2363 * @ingroup kernel_apis
2364 * @{
2365 */
2366
2367/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002370 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002371 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002372 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002375 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002377 struct k_mutex name \
2378 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002379 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002386 * Upon completion, the mutex is available and does not have an owner.
2387 *
2388 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002389 *
2390 * @return N/A
2391 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392extern void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393
2394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * This routine locks @a mutex. If the mutex is locked by another thread,
2398 * the calling thread waits until the mutex becomes available or until
2399 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * A thread is permitted to lock a mutex it has already locked. The operation
2402 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 * @param mutex Address of the mutex.
2405 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 * or one of the special values K_NO_WAIT and K_FOREVER.
2407 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002408 * @retval 0 Mutex locked.
2409 * @retval -EBUSY Returned without waiting.
2410 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 */
Kumar Galacc334c72017-04-21 10:55:34 -05002412extern int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413
2414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002417 * This routine unlocks @a mutex. The mutex must already be locked by the
2418 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
2420 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422 * thread.
2423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002424 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 *
2426 * @return N/A
2427 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428extern void k_mutex_unlock(struct k_mutex *mutex);
2429
Allan Stephensc98da842016-11-11 15:45:03 -05002430/**
2431 * @} end defgroup mutex_apis
2432 */
2433
2434/**
2435 * @cond INTERNAL_HIDDEN
2436 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437
2438struct k_sem {
2439 _wait_q_t wait_q;
2440 unsigned int count;
2441 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002442 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443
Anas Nashif2f203c22016-12-18 06:57:45 -05002444 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445};
2446
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002447#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002448 { \
2449 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2450 .count = initial_count, \
2451 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002452 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002453 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002454 }
2455
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002456#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2457
Allan Stephensc98da842016-11-11 15:45:03 -05002458/**
2459 * INTERNAL_HIDDEN @endcond
2460 */
2461
2462/**
2463 * @defgroup semaphore_apis Semaphore APIs
2464 * @ingroup kernel_apis
2465 * @{
2466 */
2467
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @param sem Address of the semaphore.
2474 * @param initial_count Initial semaphore count.
2475 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002476 *
2477 * @return N/A
2478 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479extern void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2480 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002481
2482/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002485 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2488 *
2489 * @param sem Address of the semaphore.
2490 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002491 * or one of the special values K_NO_WAIT and K_FOREVER.
2492 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002493 * @note When porting code from the nanokernel legacy API to the new API, be
2494 * careful with the return value of this function. The return value is the
2495 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2496 * non-zero means failure, while the nano_sem_take family returns 1 for success
2497 * and 0 for failure.
2498 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002499 * @retval 0 Semaphore taken.
2500 * @retval -EBUSY Returned without waiting.
2501 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002502 */
Kumar Galacc334c72017-04-21 10:55:34 -05002503extern int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002504
2505/**
2506 * @brief Give a semaphore.
2507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * This routine gives @a sem, unless the semaphore is already at its maximum
2509 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002511 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002514 *
2515 * @return N/A
2516 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517extern void k_sem_give(struct k_sem *sem);
2518
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002519/**
2520 * @brief Reset a semaphore's count to zero.
2521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * This routine sets the count of @a sem to zero.
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 *
2526 * @return N/A
2527 */
Benjamin Walsh70c68b92016-09-21 10:37:34 -04002528static inline void k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529{
2530 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531}
2532
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002533/**
2534 * @brief Get a semaphore's count.
2535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002541 */
Tomasz Bursztyka276086d2016-09-21 16:03:21 +02002542static inline unsigned int k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543{
2544 return sem->count;
2545}
2546
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002547/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002548 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002551 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002552 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * @param name Name of the semaphore.
2555 * @param initial_count Initial semaphore count.
2556 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002557 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002559 struct k_sem name \
2560 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002561 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562
Allan Stephensc98da842016-11-11 15:45:03 -05002563/**
2564 * @} end defgroup semaphore_apis
2565 */
2566
2567/**
2568 * @defgroup alert_apis Alert APIs
2569 * @ingroup kernel_apis
2570 * @{
2571 */
2572
Allan Stephens5eceb852016-11-16 10:16:30 -05002573/**
2574 * @typedef k_alert_handler_t
2575 * @brief Alert handler function type.
2576 *
2577 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002578 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002579 * and is only invoked if the alert has been initialized with one.
2580 *
2581 * @param alert Address of the alert.
2582 *
2583 * @return 0 if alert has been consumed; non-zero if alert should pend.
2584 */
2585typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002586
2587/**
2588 * @} end defgroup alert_apis
2589 */
2590
2591/**
2592 * @cond INTERNAL_HIDDEN
2593 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002595#define K_ALERT_DEFAULT NULL
2596#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002598struct k_alert {
2599 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600 atomic_t send_count;
2601 struct k_work work_item;
2602 struct k_sem sem;
2603
Anas Nashif2f203c22016-12-18 06:57:45 -05002604 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002605};
2606
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002607extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002608
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002609#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002611 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002613 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2614 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002615 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 }
2617
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002618#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2619
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620/**
Allan Stephensc98da842016-11-11 15:45:03 -05002621 * INTERNAL_HIDDEN @endcond
2622 */
2623
2624/**
2625 * @addtogroup alert_apis
2626 * @{
2627 */
2628
2629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002631 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002632 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002633 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002634 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * @param name Name of the alert.
2637 * @param alert_handler Action to take when alert is sent. Specify either
2638 * the address of a function to be invoked by the system workqueue
2639 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2640 * K_ALERT_DEFAULT (which causes the alert to pend).
2641 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002643#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002644 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002645 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002646 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002647 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002648
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 * @param alert Address of the alert.
2655 * @param handler Action to take when alert is sent. Specify either the address
2656 * of a function to be invoked by the system workqueue thread,
2657 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2658 * K_ALERT_DEFAULT (which causes the alert to pend).
2659 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660 *
2661 * @return N/A
2662 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002663extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2664 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665
2666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2672 *
2673 * @param alert Address of the alert.
2674 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 * or one of the special values K_NO_WAIT and K_FOREVER.
2676 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002677 * @retval 0 Alert received.
2678 * @retval -EBUSY Returned without waiting.
2679 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002680 */
Kumar Galacc334c72017-04-21 10:55:34 -05002681extern int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002682
2683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002684 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * This routine signals @a alert. The action specified for @a alert will
2687 * be taken, which may trigger the execution of an alert handler function
2688 * and/or cause the alert to pend (assuming the alert has not reached its
2689 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002691 * @note Can be called by ISRs.
2692 *
2693 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002694 *
2695 * @return N/A
2696 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002697extern void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698
2699/**
Allan Stephensc98da842016-11-11 15:45:03 -05002700 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701 */
2702
Allan Stephensc98da842016-11-11 15:45:03 -05002703/**
2704 * @cond INTERNAL_HIDDEN
2705 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706
2707struct k_msgq {
2708 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002709 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002710 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711 char *buffer_start;
2712 char *buffer_end;
2713 char *read_ptr;
2714 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002715 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716
Anas Nashif2f203c22016-12-18 06:57:45 -05002717 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718};
2719
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002720#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721 { \
2722 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002723 .max_msgs = q_max_msgs, \
2724 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002726 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727 .read_ptr = q_buffer, \
2728 .write_ptr = q_buffer, \
2729 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002730 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 }
2732
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002733#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2734
Peter Mitsis1da807e2016-10-06 11:36:59 -04002735/**
Allan Stephensc98da842016-11-11 15:45:03 -05002736 * INTERNAL_HIDDEN @endcond
2737 */
2738
2739/**
2740 * @defgroup msgq_apis Message Queue APIs
2741 * @ingroup kernel_apis
2742 * @{
2743 */
2744
2745/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002746 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002748 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2749 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002750 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2751 * message is similarly aligned to this boundary, @a q_msg_size must also be
2752 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 * The message queue can be accessed outside the module where it is defined
2755 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002756 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002757 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * @param q_name Name of the message queue.
2760 * @param q_msg_size Message size (in bytes).
2761 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002762 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002763 */
2764#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2765 static char __noinit __aligned(q_align) \
2766 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002767 struct k_msgq q_name \
2768 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002769 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002770 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771
Peter Mitsisd7a37502016-10-13 11:37:40 -04002772/**
2773 * @brief Initialize a message queue.
2774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * This routine initializes a message queue object, prior to its first use.
2776 *
Allan Stephensda827222016-11-09 14:23:58 -06002777 * The message queue's ring buffer must contain space for @a max_msgs messages,
2778 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2779 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2780 * that each message is similarly aligned to this boundary, @a q_msg_size
2781 * must also be a multiple of N.
2782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002783 * @param q Address of the message queue.
2784 * @param buffer Pointer to ring buffer that holds queued messages.
2785 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002786 * @param max_msgs Maximum number of messages that can be queued.
2787 *
2788 * @return N/A
2789 */
Peter Mitsis1da807e2016-10-06 11:36:59 -04002790extern void k_msgq_init(struct k_msgq *q, char *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05002791 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002792
2793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002797 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002798 * @note Can be called by ISRs.
2799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002800 * @param q Address of the message queue.
2801 * @param data Pointer to the message.
2802 * @param timeout Waiting period to add the message (in milliseconds),
2803 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002804 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002805 * @retval 0 Message sent.
2806 * @retval -ENOMSG Returned without waiting or queue purged.
2807 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002808 */
Kumar Galacc334c72017-04-21 10:55:34 -05002809extern int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002810
2811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002814 * This routine receives a message from message queue @a q in a "first in,
2815 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002816 *
Allan Stephensc98da842016-11-11 15:45:03 -05002817 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * @param q Address of the message queue.
2820 * @param data Address of area to hold the received message.
2821 * @param timeout Waiting period to receive the message (in milliseconds),
2822 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002823 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002824 * @retval 0 Message received.
2825 * @retval -ENOMSG Returned without waiting.
2826 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002827 */
Kumar Galacc334c72017-04-21 10:55:34 -05002828extern int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002829
2830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002831 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * This routine discards all unreceived messages in a message queue's ring
2834 * buffer. Any threads that are blocked waiting to send a message to the
2835 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002837 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002838 *
2839 * @return N/A
2840 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841extern void k_msgq_purge(struct k_msgq *q);
2842
Peter Mitsis67be2492016-10-07 11:44:34 -04002843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002846 * This routine returns the number of unused entries in a message queue's
2847 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * @param q Address of the message queue.
2850 *
2851 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002852 */
Kumar Galacc334c72017-04-21 10:55:34 -05002853static inline u32_t k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002854{
2855 return q->max_msgs - q->used_msgs;
2856}
2857
Peter Mitsisd7a37502016-10-13 11:37:40 -04002858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * @param q Address of the message queue.
2864 *
2865 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002866 */
Kumar Galacc334c72017-04-21 10:55:34 -05002867static inline u32_t k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868{
2869 return q->used_msgs;
2870}
2871
Allan Stephensc98da842016-11-11 15:45:03 -05002872/**
2873 * @} end defgroup msgq_apis
2874 */
2875
2876/**
2877 * @defgroup mem_pool_apis Memory Pool APIs
2878 * @ingroup kernel_apis
2879 * @{
2880 */
2881
Andy Ross73cb9582017-05-09 10:42:39 -07002882/* Note on sizing: the use of a 20 bit field for block means that,
2883 * assuming a reasonable minimum block size of 16 bytes, we're limited
2884 * to 16M of memory managed by a single pool. Long term it would be
2885 * good to move to a variable bit size based on configuration.
2886 */
2887struct k_mem_block_id {
2888 u32_t pool : 8;
2889 u32_t level : 4;
2890 u32_t block : 20;
2891};
2892
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002893struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07002895 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896};
2897
Allan Stephensc98da842016-11-11 15:45:03 -05002898/**
2899 * @} end defgroup mem_pool_apis
2900 */
2901
2902/**
2903 * @defgroup mailbox_apis Mailbox APIs
2904 * @ingroup kernel_apis
2905 * @{
2906 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002907
2908struct k_mbox_msg {
2909 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05002910 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002911 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04002912 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05002914 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915 /** sender's message data buffer */
2916 void *tx_data;
2917 /** internal use only - needed for legacy API support */
2918 void *_rx_data;
2919 /** message data block descriptor */
2920 struct k_mem_block tx_block;
2921 /** source thread id */
2922 k_tid_t rx_source_thread;
2923 /** target thread id */
2924 k_tid_t tx_target_thread;
2925 /** internal use only - thread waiting on send (may be a dummy) */
2926 k_tid_t _syncing_thread;
2927#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
2928 /** internal use only - semaphore used during asynchronous send */
2929 struct k_sem *_async_sem;
2930#endif
2931};
2932
Allan Stephensc98da842016-11-11 15:45:03 -05002933/**
2934 * @cond INTERNAL_HIDDEN
2935 */
2936
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937struct k_mbox {
2938 _wait_q_t tx_msg_queue;
2939 _wait_q_t rx_msg_queue;
2940
Anas Nashif2f203c22016-12-18 06:57:45 -05002941 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942};
2943
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002944#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945 { \
2946 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
2947 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002948 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 }
2950
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002951#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
2952
Peter Mitsis12092702016-10-14 12:57:23 -04002953/**
Allan Stephensc98da842016-11-11 15:45:03 -05002954 * INTERNAL_HIDDEN @endcond
2955 */
2956
2957/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002961 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002962 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002964 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002965 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002967 struct k_mbox name \
2968 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002969 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970
Peter Mitsis12092702016-10-14 12:57:23 -04002971/**
2972 * @brief Initialize a mailbox.
2973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * This routine initializes a mailbox object, prior to its first use.
2975 *
2976 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04002977 *
2978 * @return N/A
2979 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980extern void k_mbox_init(struct k_mbox *mbox);
2981
Peter Mitsis12092702016-10-14 12:57:23 -04002982/**
2983 * @brief Send a mailbox message in a synchronous manner.
2984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * This routine sends a message to @a mbox and waits for a receiver to both
2986 * receive and process it. The message data may be in a buffer, in a memory
2987 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * @param mbox Address of the mailbox.
2990 * @param tx_msg Address of the transmit message descriptor.
2991 * @param timeout Waiting period for the message to be received (in
2992 * milliseconds), or one of the special values K_NO_WAIT
2993 * and K_FOREVER. Once the message has been received,
2994 * this routine waits as long as necessary for the message
2995 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04002996 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002997 * @retval 0 Message sent.
2998 * @retval -ENOMSG Returned without waiting.
2999 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003000 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003001extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003002 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003003
Peter Mitsis12092702016-10-14 12:57:23 -04003004/**
3005 * @brief Send a mailbox message in an asynchronous manner.
3006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * This routine sends a message to @a mbox without waiting for a receiver
3008 * to process it. The message data may be in a buffer, in a memory pool block,
3009 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3010 * will be given when the message has been both received and completely
3011 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @param mbox Address of the mailbox.
3014 * @param tx_msg Address of the transmit message descriptor.
3015 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003016 *
3017 * @return N/A
3018 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003019extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020 struct k_sem *sem);
3021
Peter Mitsis12092702016-10-14 12:57:23 -04003022/**
3023 * @brief Receive a mailbox message.
3024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * This routine receives a message from @a mbox, then optionally retrieves
3026 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @param mbox Address of the mailbox.
3029 * @param rx_msg Address of the receive message descriptor.
3030 * @param buffer Address of the buffer to receive data, or NULL to defer data
3031 * retrieval and message disposal until later.
3032 * @param timeout Waiting period for a message to be received (in
3033 * milliseconds), or one of the special values K_NO_WAIT
3034 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003035 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003036 * @retval 0 Message received.
3037 * @retval -ENOMSG Returned without waiting.
3038 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003039 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003040extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003041 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003042
3043/**
3044 * @brief Retrieve mailbox message data into a buffer.
3045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * This routine completes the processing of a received message by retrieving
3047 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003048 *
3049 * Alternatively, this routine can be used to dispose of a received message
3050 * without retrieving its data.
3051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * @param rx_msg Address of the receive message descriptor.
3053 * @param buffer Address of the buffer to receive data, or NULL to discard
3054 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003055 *
3056 * @return N/A
3057 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003058extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003059
3060/**
3061 * @brief Retrieve mailbox message data into a memory pool block.
3062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * This routine completes the processing of a received message by retrieving
3064 * its data into a memory pool block, then disposing of the message.
3065 * The memory pool block that results from successful retrieval must be
3066 * returned to the pool once the data has been processed, even in cases
3067 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003068 *
3069 * Alternatively, this routine can be used to dispose of a received message
3070 * without retrieving its data. In this case there is no need to return a
3071 * memory pool block to the pool.
3072 *
3073 * This routine allocates a new memory pool block for the data only if the
3074 * data is not already in one. If a new block cannot be allocated, the routine
3075 * returns a failure code and the received message is left unchanged. This
3076 * permits the caller to reattempt data retrieval at a later time or to dispose
3077 * of the received message without retrieving its data.
3078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * @param rx_msg Address of a receive message descriptor.
3080 * @param pool Address of memory pool, or NULL to discard data.
3081 * @param block Address of the area to hold memory pool block info.
3082 * @param timeout Waiting period to wait for a memory pool block (in
3083 * milliseconds), or one of the special values K_NO_WAIT
3084 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003085 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003086 * @retval 0 Data retrieved.
3087 * @retval -ENOMEM Returned without waiting.
3088 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003089 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003090extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003091 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003092 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093
Allan Stephensc98da842016-11-11 15:45:03 -05003094/**
3095 * @} end defgroup mailbox_apis
3096 */
3097
3098/**
3099 * @cond INTERNAL_HIDDEN
3100 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101
3102struct k_pipe {
3103 unsigned char *buffer; /* Pipe buffer: may be NULL */
3104 size_t size; /* Buffer size */
3105 size_t bytes_used; /* # bytes used in buffer */
3106 size_t read_index; /* Where in buffer to read from */
3107 size_t write_index; /* Where in buffer to write */
3108
3109 struct {
3110 _wait_q_t readers; /* Reader wait queue */
3111 _wait_q_t writers; /* Writer wait queue */
3112 } wait_q;
3113
Anas Nashif2f203c22016-12-18 06:57:45 -05003114 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115};
3116
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003117#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118 { \
3119 .buffer = pipe_buffer, \
3120 .size = pipe_buffer_size, \
3121 .bytes_used = 0, \
3122 .read_index = 0, \
3123 .write_index = 0, \
3124 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3125 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003126 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127 }
3128
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003129#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3130
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131/**
Allan Stephensc98da842016-11-11 15:45:03 -05003132 * INTERNAL_HIDDEN @endcond
3133 */
3134
3135/**
3136 * @defgroup pipe_apis Pipe APIs
3137 * @ingroup kernel_apis
3138 * @{
3139 */
3140
3141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003145 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003146 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003148 * @param name Name of the pipe.
3149 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3150 * or zero if no ring buffer is used.
3151 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003152 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003153#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3154 static unsigned char __noinit __aligned(pipe_align) \
3155 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003156 struct k_pipe name \
3157 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003158 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003163 * This routine initializes a pipe object, prior to its first use.
3164 *
3165 * @param pipe Address of the pipe.
3166 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3167 * is used.
3168 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3169 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170 *
3171 * @return N/A
3172 */
3173extern void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3174 size_t size);
3175
3176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @param pipe Address of the pipe.
3182 * @param data Address of data to write.
3183 * @param bytes_to_write Size of data (in bytes).
3184 * @param bytes_written Address of area to hold the number of bytes written.
3185 * @param min_xfer Minimum number of bytes to write.
3186 * @param timeout Waiting period to wait for the data to be written (in
3187 * milliseconds), or one of the special values K_NO_WAIT
3188 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003189 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003190 * @retval 0 At least @a min_xfer bytes of data were written.
3191 * @retval -EIO Returned without waiting; zero data bytes were written.
3192 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003193 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003195extern int k_pipe_put(struct k_pipe *pipe, void *data,
3196 size_t bytes_to_write, size_t *bytes_written,
Kumar Galacc334c72017-04-21 10:55:34 -05003197 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003198
3199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * @param pipe Address of the pipe.
3205 * @param data Address to place the data read from pipe.
3206 * @param bytes_to_read Maximum number of data bytes to read.
3207 * @param bytes_read Address of area to hold the number of bytes read.
3208 * @param min_xfer Minimum number of data bytes to read.
3209 * @param timeout Waiting period to wait for the data to be read (in
3210 * milliseconds), or one of the special values K_NO_WAIT
3211 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003213 * @retval 0 At least @a min_xfer bytes of data were read.
3214 * @retval -EIO Returned without waiting; zero data bytes were read.
3215 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003218extern int k_pipe_get(struct k_pipe *pipe, void *data,
3219 size_t bytes_to_read, size_t *bytes_read,
Kumar Galacc334c72017-04-21 10:55:34 -05003220 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221
3222/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * This routine writes the data contained in a memory block to @a pipe.
3226 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230 * @param block Memory block containing data to send
3231 * @param size Number of data bytes in memory block to send
3232 * @param sem Semaphore to signal upon completion (else NULL)
3233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003234 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003235 */
3236extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3237 size_t size, struct k_sem *sem);
3238
3239/**
Allan Stephensc98da842016-11-11 15:45:03 -05003240 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 */
3242
Allan Stephensc98da842016-11-11 15:45:03 -05003243/**
3244 * @cond INTERNAL_HIDDEN
3245 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003247struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003249 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003250 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 char *buffer;
3252 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003253 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254
Anas Nashif2f203c22016-12-18 06:57:45 -05003255 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256};
3257
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003258#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003259 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260 { \
3261 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003262 .num_blocks = slab_num_blocks, \
3263 .block_size = slab_block_size, \
3264 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265 .free_list = NULL, \
3266 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003267 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003268 }
3269
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003270#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3271
3272
Peter Mitsis578f9112016-10-07 13:50:31 -04003273/**
Allan Stephensc98da842016-11-11 15:45:03 -05003274 * INTERNAL_HIDDEN @endcond
3275 */
3276
3277/**
3278 * @defgroup mem_slab_apis Memory Slab APIs
3279 * @ingroup kernel_apis
3280 * @{
3281 */
3282
3283/**
Allan Stephensda827222016-11-09 14:23:58 -06003284 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003285 *
Allan Stephensda827222016-11-09 14:23:58 -06003286 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003288 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3289 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003290 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003291 *
Allan Stephensda827222016-11-09 14:23:58 -06003292 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003294 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003295 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @param name Name of the memory slab.
3298 * @param slab_block_size Size of each memory block (in bytes).
3299 * @param slab_num_blocks Number memory blocks.
3300 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003301 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003302#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3303 char __noinit __aligned(slab_align) \
3304 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3305 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003306 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003307 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003308 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003310/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003311 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003314 *
Allan Stephensda827222016-11-09 14:23:58 -06003315 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3316 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3317 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3318 * To ensure that each memory block is similarly aligned to this boundary,
3319 * @a slab_block_size must also be a multiple of N.
3320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003321 * @param slab Address of the memory slab.
3322 * @param buffer Pointer to buffer used for the memory blocks.
3323 * @param block_size Size of each memory block (in bytes).
3324 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003325 *
3326 * @return N/A
3327 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003328extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003329 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003330
3331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @param slab Address of the memory slab.
3337 * @param mem Pointer to block address area.
3338 * @param timeout Maximum time to wait for operation to complete
3339 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3340 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003341 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003342 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003344 * @retval -ENOMEM Returned without waiting.
3345 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003346 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003347extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003348 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003349
3350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * This routine releases a previously allocated memory block back to its
3354 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * @param slab Address of the memory slab.
3357 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003358 *
3359 * @return N/A
3360 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003361extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * This routine gets the number of memory blocks that are currently
3367 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003372 */
Kumar Galacc334c72017-04-21 10:55:34 -05003373static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003375 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376}
3377
Peter Mitsisc001aa82016-10-13 13:53:37 -04003378/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * This routine gets the number of memory blocks that are currently
3382 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003387 */
Kumar Galacc334c72017-04-21 10:55:34 -05003388static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003389{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003390 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003391}
3392
Allan Stephensc98da842016-11-11 15:45:03 -05003393/**
3394 * @} end defgroup mem_slab_apis
3395 */
3396
3397/**
3398 * @cond INTERNAL_HIDDEN
3399 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003400
Andy Ross73cb9582017-05-09 10:42:39 -07003401struct k_mem_pool_lvl {
3402 union {
3403 u32_t *bits_p;
3404 u32_t bits;
3405 };
3406 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003407};
3408
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003409struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003410 void *buf;
3411 size_t max_sz;
3412 u16_t n_max;
3413 u8_t n_levels;
3414 u8_t max_inline_level;
3415 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003416 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417};
3418
Andy Ross73cb9582017-05-09 10:42:39 -07003419#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420
Andy Ross73cb9582017-05-09 10:42:39 -07003421#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3422
3423#define _MPOOL_LVLS(maxsz, minsz) \
3424 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3425 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3426 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3427 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3428 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3429 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3430 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3431 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3432 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3433 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3434 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3435 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3436 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3437 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3438 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3439 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3440
3441/* Rounds the needed bits up to integer multiples of u32_t */
3442#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3443 ((((n_max) << (2*(l))) + 31) / 32)
3444
3445/* One word gets stored free unioned with the pointer, otherwise the
3446 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003447 */
Andy Ross73cb9582017-05-09 10:42:39 -07003448#define _MPOOL_LBIT_WORDS(n_max, l) \
3449 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3450 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003451
Andy Ross73cb9582017-05-09 10:42:39 -07003452/* How many bytes for the bitfields of a single level? */
3453#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3454 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3455 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003456
Andy Ross73cb9582017-05-09 10:42:39 -07003457/* Size of the bitmap array that follows the buffer in allocated memory */
3458#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3459 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3460 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3461 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3462 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3463 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3464 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3465 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3466 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3467 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3468 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3469 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3470 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3471 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3472 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3473 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3474 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003475
3476/**
Allan Stephensc98da842016-11-11 15:45:03 -05003477 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003478 */
3479
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003480/**
Allan Stephensc98da842016-11-11 15:45:03 -05003481 * @addtogroup mem_pool_apis
3482 * @{
3483 */
3484
3485/**
3486 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003488 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3489 * long. The memory pool allows blocks to be repeatedly partitioned into
3490 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003491 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003492 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003493 * If the pool is to be accessed outside the module where it is defined, it
3494 * can be declared via
3495 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003496 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003498 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003499 * @param minsz Size of the smallest blocks in the pool (in bytes).
3500 * @param maxsz Size of the largest blocks in the pool (in bytes).
3501 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003503 */
Andy Ross73cb9582017-05-09 10:42:39 -07003504#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3505 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3506 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3507 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3508 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3509 .buf = _mpool_buf_##name, \
3510 .max_sz = maxsz, \
3511 .n_max = nmax, \
3512 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3513 .levels = _mpool_lvls_##name, \
3514 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003515
Peter Mitsis937042c2016-10-13 13:18:26 -04003516/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003517 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * @param pool Address of the memory pool.
3522 * @param block Pointer to block descriptor for the allocated memory.
3523 * @param size Amount of memory to allocate (in bytes).
3524 * @param timeout Maximum time to wait for operation to complete
3525 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3526 * or K_FOREVER to wait as long as necessary.
3527 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003528 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003530 * @retval -ENOMEM Returned without waiting.
3531 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003532 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003533extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003534 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003535
3536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * This routine releases a previously allocated memory block back to its
3540 * memory pool.
3541 *
3542 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003543 *
3544 * @return N/A
3545 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003547
3548/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003549 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003550 *
Andy Ross73cb9582017-05-09 10:42:39 -07003551 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 *
Andy Ross73cb9582017-05-09 10:42:39 -07003553 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003554 *
3555 * @return N/A
3556 */
Andy Ross73cb9582017-05-09 10:42:39 -07003557static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003558
3559/**
Allan Stephensc98da842016-11-11 15:45:03 -05003560 * @} end addtogroup mem_pool_apis
3561 */
3562
3563/**
3564 * @defgroup heap_apis Heap Memory Pool APIs
3565 * @ingroup kernel_apis
3566 * @{
3567 */
3568
3569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003573 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003575 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003577 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003578 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003579extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003580
3581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003582 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003583 *
3584 * This routine provides traditional free() semantics. The memory being
3585 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003586 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003587 * If @a ptr is NULL, no operation is performed.
3588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003589 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003590 *
3591 * @return N/A
3592 */
3593extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003594
Allan Stephensc98da842016-11-11 15:45:03 -05003595/**
3596 * @} end defgroup heap_apis
3597 */
3598
Benjamin Walshacc68c12017-01-29 18:57:45 -05003599/* polling API - PRIVATE */
3600
Benjamin Walshb0179862017-02-02 16:39:57 -05003601#ifdef CONFIG_POLL
3602#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3603#else
3604#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3605#endif
3606
Benjamin Walshacc68c12017-01-29 18:57:45 -05003607/* private - implementation data created as needed, per-type */
3608struct _poller {
3609 struct k_thread *thread;
3610};
3611
3612/* private - types bit positions */
3613enum _poll_types_bits {
3614 /* can be used to ignore an event */
3615 _POLL_TYPE_IGNORE,
3616
3617 /* to be signaled by k_poll_signal() */
3618 _POLL_TYPE_SIGNAL,
3619
3620 /* semaphore availability */
3621 _POLL_TYPE_SEM_AVAILABLE,
3622
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003623 /* queue/fifo/lifo data availability */
3624 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003625
3626 _POLL_NUM_TYPES
3627};
3628
3629#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3630
3631/* private - states bit positions */
3632enum _poll_states_bits {
3633 /* default state when creating event */
3634 _POLL_STATE_NOT_READY,
3635
Benjamin Walshacc68c12017-01-29 18:57:45 -05003636 /* signaled by k_poll_signal() */
3637 _POLL_STATE_SIGNALED,
3638
3639 /* semaphore is available */
3640 _POLL_STATE_SEM_AVAILABLE,
3641
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003642 /* data is available to read on queue/fifo/lifo */
3643 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003644
3645 _POLL_NUM_STATES
3646};
3647
3648#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3649
3650#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003651 (32 - (0 \
3652 + 8 /* tag */ \
3653 + _POLL_NUM_TYPES \
3654 + _POLL_NUM_STATES \
3655 + 1 /* modes */ \
3656 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003657
3658#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3659#error overflow of 32-bit word in struct k_poll_event
3660#endif
3661
3662/* end of polling API - PRIVATE */
3663
3664
3665/**
3666 * @defgroup poll_apis Async polling APIs
3667 * @ingroup kernel_apis
3668 * @{
3669 */
3670
3671/* Public polling API */
3672
3673/* public - values for k_poll_event.type bitfield */
3674#define K_POLL_TYPE_IGNORE 0
3675#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3676#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003677#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3678#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003679
3680/* public - polling modes */
3681enum k_poll_modes {
3682 /* polling thread does not take ownership of objects when available */
3683 K_POLL_MODE_NOTIFY_ONLY = 0,
3684
3685 K_POLL_NUM_MODES
3686};
3687
3688/* public - values for k_poll_event.state bitfield */
3689#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003690#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3691#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003692#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3693#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003694
3695/* public - poll signal object */
3696struct k_poll_signal {
3697 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003698 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003699
3700 /*
3701 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3702 * user resets it to 0.
3703 */
3704 unsigned int signaled;
3705
3706 /* custom result value passed to k_poll_signal() if needed */
3707 int result;
3708};
3709
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003710#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003711 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003712 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003713 .signaled = 0, \
3714 .result = 0, \
3715 }
3716
3717struct k_poll_event {
3718 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003719 sys_dnode_t _node;
3720
3721 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003722 struct _poller *poller;
3723
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003724 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003725 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003726
Benjamin Walshacc68c12017-01-29 18:57:45 -05003727 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003728 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003729
3730 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003731 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003732
3733 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003734 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003735
3736 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003737 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003738
3739 /* per-type data */
3740 union {
3741 void *obj;
3742 struct k_poll_signal *signal;
3743 struct k_sem *sem;
3744 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003745 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003746 };
3747};
3748
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003749#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003750 { \
3751 .poller = NULL, \
3752 .type = event_type, \
3753 .state = K_POLL_STATE_NOT_READY, \
3754 .mode = event_mode, \
3755 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003756 { .obj = event_obj }, \
3757 }
3758
3759#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3760 event_tag) \
3761 { \
3762 .type = event_type, \
3763 .tag = event_tag, \
3764 .state = K_POLL_STATE_NOT_READY, \
3765 .mode = event_mode, \
3766 .unused = 0, \
3767 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003768 }
3769
3770/**
3771 * @brief Initialize one struct k_poll_event instance
3772 *
3773 * After this routine is called on a poll event, the event it ready to be
3774 * placed in an event array to be passed to k_poll().
3775 *
3776 * @param event The event to initialize.
3777 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3778 * values. Only values that apply to the same object being polled
3779 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3780 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003781 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003782 * @param obj Kernel object or poll signal.
3783 *
3784 * @return N/A
3785 */
3786
Kumar Galacc334c72017-04-21 10:55:34 -05003787extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003788 int mode, void *obj);
3789
3790/**
3791 * @brief Wait for one or many of multiple poll events to occur
3792 *
3793 * This routine allows a thread to wait concurrently for one or many of
3794 * multiple poll events to have occurred. Such events can be a kernel object
3795 * being available, like a semaphore, or a poll signal event.
3796 *
3797 * When an event notifies that a kernel object is available, the kernel object
3798 * is not "given" to the thread calling k_poll(): it merely signals the fact
3799 * that the object was available when the k_poll() call was in effect. Also,
3800 * all threads trying to acquire an object the regular way, i.e. by pending on
3801 * the object, have precedence over the thread polling on the object. This
3802 * means that the polling thread will never get the poll event on an object
3803 * until the object becomes available and its pend queue is empty. For this
3804 * reason, the k_poll() call is more effective when the objects being polled
3805 * only have one thread, the polling thread, trying to acquire them.
3806 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003807 * When k_poll() returns 0, the caller should loop on all the events that were
3808 * passed to k_poll() and check the state field for the values that were
3809 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003810 *
3811 * Before being reused for another call to k_poll(), the user has to reset the
3812 * state field to K_POLL_STATE_NOT_READY.
3813 *
3814 * @param events An array of pointers to events to be polled for.
3815 * @param num_events The number of events in the array.
3816 * @param timeout Waiting period for an event to be ready (in milliseconds),
3817 * or one of the special values K_NO_WAIT and K_FOREVER.
3818 *
3819 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820 * @retval -EAGAIN Waiting period timed out.
3821 */
3822
3823extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003824 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003825
3826/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003827 * @brief Initialize a poll signal object.
3828 *
3829 * Ready a poll signal object to be signaled via k_poll_signal().
3830 *
3831 * @param signal A poll signal.
3832 *
3833 * @return N/A
3834 */
3835
3836extern void k_poll_signal_init(struct k_poll_signal *signal);
3837
3838/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003839 * @brief Signal a poll signal object.
3840 *
3841 * This routine makes ready a poll signal, which is basically a poll event of
3842 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3843 * made ready to run. A @a result value can be specified.
3844 *
3845 * The poll signal contains a 'signaled' field that, when set by
3846 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3847 * be reset by the user before being passed again to k_poll() or k_poll() will
3848 * consider it being signaled, and will return immediately.
3849 *
3850 * @param signal A poll signal.
3851 * @param result The value to store in the result field of the signal.
3852 *
3853 * @retval 0 The signal was delivered successfully.
3854 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3855 */
3856
3857extern int k_poll_signal(struct k_poll_signal *signal, int result);
3858
3859/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003860extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003861
3862/**
3863 * @} end defgroup poll_apis
3864 */
3865
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003866/**
3867 * @brief Make the CPU idle.
3868 *
3869 * This function makes the CPU idle until an event wakes it up.
3870 *
3871 * In a regular system, the idle thread should be the only thread responsible
3872 * for making the CPU idle and triggering any type of power management.
3873 * However, in some more constrained systems, such as a single-threaded system,
3874 * the only thread would be responsible for this if needed.
3875 *
3876 * @return N/A
3877 */
3878extern void k_cpu_idle(void);
3879
3880/**
3881 * @brief Make the CPU idle in an atomic fashion.
3882 *
3883 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3884 * must be done atomically before making the CPU idle.
3885 *
3886 * @param key Interrupt locking key obtained from irq_lock().
3887 *
3888 * @return N/A
3889 */
3890extern void k_cpu_atomic_idle(unsigned int key);
3891
Kumar Galacc334c72017-04-21 10:55:34 -05003892extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003893
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003894#include <arch/cpu.h>
3895
Andrew Boie1f32d092017-08-30 14:00:26 -07003896#ifdef CONFIG_USERSPACE
3897/* Architecture-specific inline functions that may be indirectly called by
3898 * application code due to their appearance in macros or other inline functions.
3899 *
3900 * Each arch should implement these in <arch/cpu.h>
3901 */
3902
3903/* Indicate whether we are currently running in user mode
3904 *
3905 * @return nonzero if the CPU is currently running with user permissions
3906 */
3907static inline int _arch_is_user_context(void);
3908
Andrew Boie3f091b52017-08-30 14:34:14 -07003909/**
3910 * Indicate whether the CPU is currently in user mode
3911 *
3912 * @return nonzero if the CPU is currently running with user permissions
3913 */
3914static inline int _is_user_context(void)
3915{
3916 return _arch_is_user_context();
3917}
3918
Andrew Boie1f32d092017-08-30 14:00:26 -07003919/* Interfaces for invoking system calls */
3920static inline u32_t _arch_syscall_invoke5(u32_t arg1, u32_t arg2, u32_t arg3,
3921 u32_t arg4, u32_t arg5,
3922 u32_t call_id);
3923
3924static inline u32_t _arch_syscall_invoke4(u32_t arg1, u32_t arg2, u32_t arg3,
3925 u32_t arg4, u32_t call_id);
3926
3927static inline u32_t _arch_syscall_invoke3(u32_t arg1, u32_t arg2, u32_t arg3,
3928 u32_t call_id);
3929
3930static inline u32_t _arch_syscall_invoke2(u32_t arg1, u32_t arg2,
3931 u32_t call_id);
3932
3933static inline u32_t _arch_syscall_invoke1(u32_t arg1, u32_t call_id);
3934
3935static inline u32_t _arch_syscall_invoke0(u32_t call_id);
3936#endif
3937
Andrew Boiecdb94d62017-04-18 15:22:05 -07003938#ifdef _ARCH_EXCEPT
3939/* This archtecture has direct support for triggering a CPU exception */
3940#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
3941#else
3942
3943#include <misc/printk.h>
3944
3945/* NOTE: This is the implementation for arches that do not implement
3946 * _ARCH_EXCEPT() to generate a real CPU exception.
3947 *
3948 * We won't have a real exception frame to determine the PC value when
3949 * the oops occurred, so print file and line number before we jump into
3950 * the fatal error handler.
3951 */
3952#define _k_except_reason(reason) do { \
3953 printk("@ %s:%d:\n", __FILE__, __LINE__); \
3954 _NanoFatalErrorHandler(reason, &_default_esf); \
3955 CODE_UNREACHABLE; \
3956 } while (0)
3957
3958#endif /* _ARCH__EXCEPT */
3959
3960/**
3961 * @brief Fatally terminate a thread
3962 *
3963 * This should be called when a thread has encountered an unrecoverable
3964 * runtime condition and needs to terminate. What this ultimately
3965 * means is determined by the _fatal_error_handler() implementation, which
3966 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
3967 *
3968 * If this is called from ISR context, the default system fatal error handler
3969 * will treat it as an unrecoverable system error, just like k_panic().
3970 */
3971#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
3972
3973/**
3974 * @brief Fatally terminate the system
3975 *
3976 * This should be called when the Zephyr kernel has encountered an
3977 * unrecoverable runtime condition and needs to terminate. What this ultimately
3978 * means is determined by the _fatal_error_handler() implementation, which
3979 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
3980 */
3981#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
3982
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003983/*
3984 * private APIs that are utilized by one or more public APIs
3985 */
3986
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003987#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003988extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05003989#else
3990#define _init_static_threads() do { } while ((0))
3991#endif
3992
3993extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05003994extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003995
Andrew Boiedc5d9352017-06-02 12:56:47 -07003996/* arch/cpu.h may declare an architecture or platform-specific macro
3997 * for properly declaring stacks, compatible with MMU/MPU constraints if
3998 * enabled
3999 */
4000#ifdef _ARCH_THREAD_STACK_DEFINE
4001#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4002#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4003 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4004#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4005#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004006static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4007{
4008 return _ARCH_THREAD_STACK_BUFFER(sym);
4009}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004010#else
4011/**
4012 * @brief Declare a toplevel thread stack memory region
4013 *
4014 * This declares a region of memory suitable for use as a thread's stack.
4015 *
4016 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4017 * 'noinit' section so that it isn't zeroed at boot
4018 *
Andrew Boie507852a2017-07-25 18:47:07 -07004019 * The declared symbol will always be a k_thread_stack_t which can be passed to
4020 * k_thread_create, but should otherwise not be manipulated. If the buffer
4021 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004022 *
4023 * It is legal to precede this definition with the 'static' keyword.
4024 *
4025 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4026 * parameter of k_thread_create(), it may not be the same as the
4027 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4028 *
4029 * @param sym Thread stack symbol name
4030 * @param size Size of the stack memory region
4031 */
4032#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004033 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004034
4035/**
4036 * @brief Declare a toplevel array of thread stack memory regions
4037 *
4038 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4039 * definition for additional details and constraints.
4040 *
4041 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4042 * 'noinit' section so that it isn't zeroed at boot
4043 *
4044 * @param sym Thread stack symbol name
4045 * @param nmemb Number of stacks to declare
4046 * @param size Size of the stack memory region
4047 */
4048
4049#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004050 struct _k_thread_stack_element __noinit \
4051 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004052
4053/**
4054 * @brief Declare an embedded stack memory region
4055 *
4056 * Used for stacks embedded within other data structures. Use is highly
4057 * discouraged but in some cases necessary. For memory protection scenarios,
4058 * it is very important that any RAM preceding this member not be writable
4059 * by threads else a stack overflow will lead to silent corruption. In other
4060 * words, the containing data structure should live in RAM owned by the kernel.
4061 *
4062 * @param sym Thread stack symbol name
4063 * @param size Size of the stack memory region
4064 */
4065#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004066 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004067
4068/**
4069 * @brief Return the size in bytes of a stack memory region
4070 *
4071 * Convenience macro for passing the desired stack size to k_thread_create()
4072 * since the underlying implementation may actually create something larger
4073 * (for instance a guard area).
4074 *
4075 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004076 * passed to K_THREAD_STACK_DEFINE.
4077 *
4078 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4079 * it is not guaranteed to return the original value since each array
4080 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004081 *
4082 * @param sym Stack memory symbol
4083 * @return Size of the stack
4084 */
4085#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4086
4087/**
4088 * @brief Get a pointer to the physical stack buffer
4089 *
4090 * Convenience macro to get at the real underlying stack buffer used by
4091 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4092 * This is really only intended for diagnostic tools which want to examine
4093 * stack memory contents.
4094 *
4095 * @param sym Declared stack symbol name
4096 * @return The buffer itself, a char *
4097 */
Andrew Boie507852a2017-07-25 18:47:07 -07004098static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t sym)
4099{
4100 return (char *)sym;
4101}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004102
4103#endif /* _ARCH_DECLARE_STACK */
4104
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105#ifdef __cplusplus
4106}
4107#endif
4108
Andrew Boiee004dec2016-11-07 09:01:19 -08004109#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4110/*
4111 * Define new and delete operators.
4112 * At this moment, the operators do nothing since objects are supposed
4113 * to be statically allocated.
4114 */
4115inline void operator delete(void *ptr)
4116{
4117 (void)ptr;
4118}
4119
4120inline void operator delete[](void *ptr)
4121{
4122 (void)ptr;
4123}
4124
4125inline void *operator new(size_t size)
4126{
4127 (void)size;
4128 return NULL;
4129}
4130
4131inline void *operator new[](size_t size)
4132{
4133 (void)size;
4134 return NULL;
4135}
4136
4137/* Placement versions of operator new and delete */
4138inline void operator delete(void *ptr1, void *ptr2)
4139{
4140 (void)ptr1;
4141 (void)ptr2;
4142}
4143
4144inline void operator delete[](void *ptr1, void *ptr2)
4145{
4146 (void)ptr1;
4147 (void)ptr2;
4148}
4149
4150inline void *operator new(size_t size, void *ptr)
4151{
4152 (void)size;
4153 return ptr;
4154}
4155
4156inline void *operator new[](size_t size, void *ptr)
4157{
4158 (void)size;
4159 return ptr;
4160}
4161
4162#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4163
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004164#endif /* !_ASMLANGUAGE */
4165
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004166#endif /* _kernel__h_ */