blob: 6983fa17cadaa9f9c3c6e94b2f2768ec05b2ea09 [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
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070019#include <stdbool.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020
21#ifdef __cplusplus
22extern "C" {
23#endif
24
Anas Nashifbbb157d2017-01-15 08:46:31 -050025/**
26 * @brief Kernel APIs
27 * @defgroup kernel_apis Kernel APIs
28 * @{
29 * @}
30 */
31
Anas Nashif61f4b242016-11-18 10:53:59 -050032#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040033#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
34#else
35#define K_DEBUG(fmt, ...)
36#endif
37
Benjamin Walsh2f280412017-01-14 19:23:46 -050038#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
39#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
40#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
41#elif defined(CONFIG_COOP_ENABLED)
42#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
43#define _NUM_PREEMPT_PRIO (0)
44#elif defined(CONFIG_PREEMPT_ENABLED)
45#define _NUM_COOP_PRIO (0)
46#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
47#else
48#error "invalid configuration"
49#endif
50
51#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_PRIO_PREEMPT(x) (x)
53
Benjamin Walsh456c6da2016-09-02 18:55:39 -040054#define K_ANY NULL
55#define K_END NULL
56
Benjamin Walshedb35702017-01-14 18:47:22 -050057#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040058#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050059#elif defined(CONFIG_COOP_ENABLED)
60#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
61#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040062#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050063#else
64#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#endif
66
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050067#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
69#else
70#define K_LOWEST_THREAD_PRIO -1
71#endif
72
Benjamin Walshfab8d922016-11-08 15:36:36 -050073#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
74
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
76#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
77
Andy Ross225c74b2018-06-27 11:20:50 -070078#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070079
80typedef struct {
81 struct _priq_rb waitq;
82} _wait_q_t;
83
Flavio Ceolin02ed85b2018-09-20 15:43:57 -070084extern bool _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070085
86#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
87
88#else
89
Andy Rossccf3bf72018-05-10 11:10:34 -070090typedef struct {
91 sys_dlist_t waitq;
92} _wait_q_t;
93
94#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095
Andy Ross1acd8c22018-05-03 14:51:49 -070096#endif
97
Anas Nashif2f203c22016-12-18 06:57:45 -050098#ifdef CONFIG_OBJECT_TRACING
99#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
100#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500102#define _OBJECT_TRACING_INIT
Mark Ruvald Pedersen9960bd92018-09-11 12:31:36 +0200103#define _OBJECT_TRACING_NEXT_PTR(type) u8_t __dummy_next[0]
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104#endif
105
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300107#define _POLL_EVENT_OBJ_INIT(obj) \
108 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
109#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500110#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300111#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500112#define _POLL_EVENT
113#endif
114
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500115struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_mutex;
117struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400118struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_msgq;
120struct k_mbox;
121struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200122struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_fifo;
124struct k_lifo;
125struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400126struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127struct k_mem_pool;
128struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500129struct k_poll_event;
130struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800131struct k_mem_domain;
132struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400133
Andrew Boie5bd891d2017-09-27 12:59:28 -0700134/* This enumeration needs to be kept in sync with the lists of kernel objects
135 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
136 * function in kernel/userspace.c
137 */
Andrew Boie945af952017-08-22 13:15:23 -0700138enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700139 K_OBJ_ANY,
140
Leandro Pereirac2003672018-04-04 13:50:32 -0700141 /** @cond
142 * Doxygen should ignore this build-time generated include file
143 * when genrating API documentation. Enumeration values are
144 * generated during build by gen_kobject_list.py. It includes
145 * basic kernel objects (e.g. pipes and mutexes) and driver types.
146 */
147#include <kobj-types-enum.h>
148 /** @endcond
149 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700150
Andrew Boie945af952017-08-22 13:15:23 -0700151 K_OBJ_LAST
152};
153
154#ifdef CONFIG_USERSPACE
155/* Table generated by gperf, these objects are retrieved via
156 * _k_object_find() */
157struct _k_object {
158 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700159 u8_t perms[CONFIG_MAX_THREAD_BYTES];
160 u8_t type;
161 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700162 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700163} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700164
Andrew Boie877f82e2017-10-17 11:20:22 -0700165struct _k_object_assignment {
166 struct k_thread *thread;
167 void * const *objects;
168};
169
170/**
171 * @brief Grant a static thread access to a list of kernel objects
172 *
173 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
174 * a set of kernel objects. These objects do not need to be in an initialized
175 * state. The permissions will be granted when the threads are initialized
176 * in the early boot sequence.
177 *
178 * All arguments beyond the first must be pointers to kernel objects.
179 *
180 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
181 */
182#define K_THREAD_ACCESS_GRANT(name_, ...) \
183 static void * const _CONCAT(_object_list_, name_)[] = \
184 { __VA_ARGS__, NULL }; \
185 static __used __in_section_unique(object_access) \
186 const struct _k_object_assignment \
187 _CONCAT(_object_access_, name_) = \
188 { (&_k_thread_obj_ ## name_), \
189 (_CONCAT(_object_list_, name_)) }
190
Andrew Boie945af952017-08-22 13:15:23 -0700191#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700192#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700193#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700194
195/**
196 * Lookup a kernel object and init its metadata if it exists
197 *
198 * Calling this on an object will make it usable from userspace.
199 * Intended to be called as the last statement in kernel object init
200 * functions.
201 *
202 * @param object Address of the kernel object
203 */
204void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700205#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700206
207#define K_THREAD_ACCESS_GRANT(thread, ...)
208
Anas Nashif954d5502018-02-25 08:37:28 -0600209/**
210 * @internal
211 */
Andrew Boie743e4682017-10-04 12:25:50 -0700212static inline void _k_object_init(void *obj)
213{
214 ARG_UNUSED(obj);
215}
216
Anas Nashif954d5502018-02-25 08:37:28 -0600217/**
218 * @internal
219 */
Andrew Boie743e4682017-10-04 12:25:50 -0700220static inline void _impl_k_object_access_grant(void *object,
221 struct k_thread *thread)
222{
223 ARG_UNUSED(object);
224 ARG_UNUSED(thread);
225}
226
Anas Nashif954d5502018-02-25 08:37:28 -0600227/**
228 * @internal
229 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700230static inline void k_object_access_revoke(void *object,
231 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700232{
233 ARG_UNUSED(object);
234 ARG_UNUSED(thread);
235}
236
Andrew Boiee9cfc542018-04-13 13:15:28 -0700237/**
238 * @internal
239 */
240static inline void _impl_k_object_release(void *object)
241{
242 ARG_UNUSED(object);
243}
244
Andrew Boie41bab6e2017-10-14 14:42:23 -0700245static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700246{
247 ARG_UNUSED(object);
248}
249#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700250
251/**
252 * grant a thread access to a kernel object
253 *
254 * The thread will be granted access to the object if the caller is from
255 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700256 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700257 *
258 * @param object Address of kernel object
259 * @param thread Thread to grant access to the object
260 */
Andrew Boie743e4682017-10-04 12:25:50 -0700261__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700262
Andrew Boiea89bf012017-10-09 14:47:55 -0700263/**
264 * grant a thread access to a kernel object
265 *
266 * The thread will lose access to the object if the caller is from
267 * supervisor mode, or the caller is from user mode AND has permissions
268 * on both the object and the thread whose access is being revoked.
269 *
270 * @param object Address of kernel object
271 * @param thread Thread to remove access to the object
272 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700273void k_object_access_revoke(void *object, struct k_thread *thread);
274
275
276__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700277
278/**
279 * grant all present and future threads access to an object
280 *
281 * If the caller is from supervisor mode, or the caller is from user mode and
282 * have sufficient permissions on the object, then that object will have
283 * permissions granted to it for *all* current and future threads running in
284 * the system, effectively becoming a public kernel object.
285 *
286 * Use of this API should be avoided on systems that are running untrusted code
287 * as it is possible for such code to derive the addresses of kernel objects
288 * and perform unwanted operations on them.
289 *
Andrew Boie04caa672017-10-13 13:57:07 -0700290 * It is not possible to revoke permissions on public objects; once public,
291 * any thread may use it.
292 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700293 * @param object Address of kernel object
294 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700295void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700296
Andrew Boie31bdfc02017-11-08 16:38:03 -0800297/**
298 * Allocate a kernel object of a designated type
299 *
300 * This will instantiate at runtime a kernel object of the specified type,
301 * returning a pointer to it. The object will be returned in an uninitialized
302 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700303 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800304 *
305 * Currently, allocation of thread stacks is not supported.
306 *
307 * @param otype Requested kernel object type
308 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
309 * available
310 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700311__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800312
Andrew Boie97bf0012018-04-24 17:01:37 -0700313#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800314/**
315 * Free a kernel object previously allocated with k_object_alloc()
316 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700317 * This will return memory for a kernel object back to resource pool it was
318 * allocated from. Care must be exercised that the object will not be used
319 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800320 *
321 * @param obj Pointer to the kernel object memory address.
322 */
323void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700324#else
325static inline void *_impl_k_object_alloc(enum k_objects otype)
326{
Kumar Gala85699f72018-05-17 09:26:03 -0500327 ARG_UNUSED(otype);
328
Andrew Boie97bf0012018-04-24 17:01:37 -0700329 return NULL;
330}
331
332static inline void k_obj_free(void *obj)
333{
334 ARG_UNUSED(obj);
335}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800336#endif /* CONFIG_DYNAMIC_OBJECTS */
337
Andrew Boiebca15da2017-10-15 14:17:48 -0700338/* Using typedef deliberately here, this is quite intended to be an opaque
339 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
340 *
341 * The purpose of this data type is to clearly distinguish between the
342 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
343 * buffer which composes the stack data actually used by the underlying
344 * thread; they cannot be used interchangably as some arches precede the
345 * stack buffer region with guard areas that trigger a MPU or MMU fault
346 * if written to.
347 *
348 * APIs that want to work with the buffer inside should continue to use
349 * char *.
350 *
351 * Stacks should always be created with K_THREAD_STACK_DEFINE().
352 */
353struct __packed _k_thread_stack_element {
354 char data;
355};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700356typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700357
Andrew Boie73abd322017-04-04 13:19:13 -0700358/* timeouts */
359
360struct _timeout;
361typedef void (*_timeout_func_t)(struct _timeout *t);
362
363struct _timeout {
364 sys_dnode_t node;
365 struct k_thread *thread;
366 sys_dlist_t *wait_q;
367 s32_t delta_ticks_from_prev;
368 _timeout_func_t func;
369};
370
371extern s32_t _timeout_remaining_get(struct _timeout *timeout);
372
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700373/**
374 * @typedef k_thread_entry_t
375 * @brief Thread entry point function type.
376 *
377 * A thread's entry point function is invoked when the thread starts executing.
378 * Up to 3 argument values can be passed to the function.
379 *
380 * The thread terminates execution permanently if the entry point function
381 * returns. The thread is responsible for releasing any shared resources
382 * it may own (such as mutexes and dynamically allocated memory), prior to
383 * returning.
384 *
385 * @param p1 First argument.
386 * @param p2 Second argument.
387 * @param p3 Third argument.
388 *
389 * @return N/A
390 */
391typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700392
393#ifdef CONFIG_THREAD_MONITOR
394struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700395 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700396 void *parameter1;
397 void *parameter2;
398 void *parameter3;
399};
400#endif
401
402/* can be used for creating 'dummy' threads, e.g. for pending on objects */
403struct _thread_base {
404
405 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700406 union {
407 sys_dlist_t qnode_dlist;
408 struct rbnode qnode_rb;
409 };
410
Andy Ross225c74b2018-06-27 11:20:50 -0700411#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -0700412 /* wait queue on which the thread is pended (needed only for
413 * trees, not dumb lists)
414 */
415 _wait_q_t *pended_on;
416#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700417
418 /* user facing 'thread options'; values defined in include/kernel.h */
419 u8_t user_options;
420
421 /* thread state */
422 u8_t thread_state;
423
424 /*
425 * scheduler lock count and thread priority
426 *
427 * These two fields control the preemptibility of a thread.
428 *
429 * When the scheduler is locked, sched_locked is decremented, which
430 * means that the scheduler is locked for values from 0xff to 0x01. A
431 * thread is coop if its prio is negative, thus 0x80 to 0xff when
432 * looked at the value as unsigned.
433 *
434 * By putting them end-to-end, this means that a thread is
435 * non-preemptible if the bundled value is greater than or equal to
436 * 0x0080.
437 */
438 union {
439 struct {
440#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
441 u8_t sched_locked;
442 s8_t prio;
443#else /* LITTLE and PDP */
444 s8_t prio;
445 u8_t sched_locked;
446#endif
447 };
448 u16_t preempt;
449 };
450
Andy Ross4a2e50f2018-05-15 11:06:25 -0700451#ifdef CONFIG_SCHED_DEADLINE
452 int prio_deadline;
453#endif
454
Andy Ross1acd8c22018-05-03 14:51:49 -0700455 u32_t order_key;
456
Andy Ross2724fd12018-01-29 14:55:20 -0800457#ifdef CONFIG_SMP
458 /* True for the per-CPU idle threads */
459 u8_t is_idle;
460
Andy Ross2724fd12018-01-29 14:55:20 -0800461 /* CPU index on which thread was last run */
462 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700463
464 /* Recursive count of irq_lock() calls */
465 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800466#endif
467
Andrew Boie73abd322017-04-04 13:19:13 -0700468 /* data returned by APIs */
469 void *swap_data;
470
471#ifdef CONFIG_SYS_CLOCK_EXISTS
472 /* this thread's entry in a timeout queue */
473 struct _timeout timeout;
474#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700475};
476
477typedef struct _thread_base _thread_base_t;
478
479#if defined(CONFIG_THREAD_STACK_INFO)
480/* Contains the stack information of a thread */
481struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700482 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
483 * object. Represents thread-writable stack area without any extras.
484 */
Andrew Boie73abd322017-04-04 13:19:13 -0700485 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700486
487 /* Stack Size - Thread writable stack buffer size. Represents
488 * the size of the actual area, starting from the start member,
489 * that should be writable by the thread
490 */
Andrew Boie73abd322017-04-04 13:19:13 -0700491 u32_t size;
492};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700493
494typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700495#endif /* CONFIG_THREAD_STACK_INFO */
496
Chunlin Hane9c97022017-07-07 20:29:30 +0800497#if defined(CONFIG_USERSPACE)
498struct _mem_domain_info {
499 /* memory domain queue node */
500 sys_dnode_t mem_domain_q_node;
501 /* memory domain of the thread */
502 struct k_mem_domain *mem_domain;
503};
504
505#endif /* CONFIG_USERSPACE */
506
Daniel Leungfc182432018-08-16 15:42:28 -0700507#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
508struct _thread_userspace_local_data {
509 int errno_var;
510};
511#endif
512
Anas Nashifce78d162018-05-24 12:43:11 -0500513/**
514 * @ingroup thread_apis
515 * Thread Structure
516 */
Andrew Boie73abd322017-04-04 13:19:13 -0700517struct k_thread {
518
519 struct _thread_base base;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700522 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500523 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700524 struct _callee_saved callee_saved;
525
Anas Nashifce78d162018-05-24 12:43:11 -0500526 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700527 void *init_data;
528
Anas Nashifce78d162018-05-24 12:43:11 -0500529 /**
530 * abort function
531 * @req K-THREAD-002
532 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700533 void (*fn_abort)(void);
534
535#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500536 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700537 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700538
Anas Nashifce78d162018-05-24 12:43:11 -0500539 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700540 struct k_thread *next_thread;
541#endif
542
Anas Nashif57554052018-03-03 02:31:05 -0600543#if defined(CONFIG_THREAD_NAME)
544 /* Thread name */
545 const char *name;
546#endif
547
Andrew Boie73abd322017-04-04 13:19:13 -0700548#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500549 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700550 void *custom_data;
551#endif
552
Daniel Leungfc182432018-08-16 15:42:28 -0700553#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
554 struct _thread_userspace_local_data *userspace_local_data;
555#endif
556
Andrew Boie73abd322017-04-04 13:19:13 -0700557#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700558#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500559 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700560 int errno_var;
561#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700562#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700563
564#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700566 struct _thread_stack_info stack_info;
567#endif /* CONFIG_THREAD_STACK_INFO */
568
Chunlin Hane9c97022017-07-07 20:29:30 +0800569#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800571 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500572 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700573 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800574#endif /* CONFIG_USERSPACE */
575
Andy Ross042d8ec2017-12-09 08:37:20 -0800576#if defined(CONFIG_USE_SWITCH)
577 /* When using __switch() a few previously arch-specific items
578 * become part of the core OS
579 */
580
Anas Nashifce78d162018-05-24 12:43:11 -0500581 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800582 int swap_retval;
583
Anas Nashifce78d162018-05-24 12:43:11 -0500584 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800585 void *switch_handle;
586#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500587 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700588 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800589
Anas Nashifce78d162018-05-24 12:43:11 -0500590 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700591 struct _thread_arch arch;
592};
593
594typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400595typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700596#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400597
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598enum execution_context_types {
599 K_ISR = 0,
600 K_COOP_THREAD,
601 K_PREEMPT_THREAD,
602};
603
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400604/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100605 * @defgroup profiling_apis Profiling APIs
606 * @ingroup kernel_apis
607 * @{
608 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530609typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
610 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100611
612/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530613 * @brief Iterate over all the threads in the system.
614 *
615 * This routine iterates over all the threads in the system and
616 * calls the user_cb function for each thread.
617 *
618 * @param user_cb Pointer to the user callback function.
619 * @param user_data Pointer to user data.
620 *
621 * @note CONFIG_THREAD_MONITOR must be set for this function
622 * to be effective. Also this API uses irq_lock to protect the
623 * _kernel.threads list which means creation of new threads and
624 * terminations of existing threads are blocked until this
625 * API returns.
626 *
627 * @return N/A
628 */
629extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
630
Anas Nashif166f5192018-02-25 08:02:36 -0600631/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100632
633/**
Allan Stephensc98da842016-11-11 15:45:03 -0500634 * @defgroup thread_apis Thread APIs
635 * @ingroup kernel_apis
636 * @{
637 */
638
Benjamin Walshed240f22017-01-22 13:05:08 -0500639#endif /* !_ASMLANGUAGE */
640
641
642/*
643 * Thread user options. May be needed by assembly code. Common part uses low
644 * bits, arch-specific use high bits.
645 */
646
Anas Nashifa541e932018-05-24 11:19:16 -0500647/**
648 * @brief system thread that must not abort
649 * @req K-THREAD-000
650 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700651#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500652
653#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500654/**
655 * @brief thread uses floating point registers
656 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700657#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500658#endif
659
Anas Nashifa541e932018-05-24 11:19:16 -0500660/**
661 * @brief user mode thread
662 *
663 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700664 * has additional restrictions
665 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700666#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700667
Anas Nashifa541e932018-05-24 11:19:16 -0500668/**
669 * @brief Inherit Permissions
670 *
671 * @details
672 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700673 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
674 * is not enabled.
675 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700676#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700677
Benjamin Walshed240f22017-01-22 13:05:08 -0500678#ifdef CONFIG_X86
679/* x86 Bitmask definitions for threads user options */
680
681#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
682/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700683#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500684#endif
685#endif
686
687/* end - thread options */
688
689#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400690/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700691 * @brief Create a thread.
692 *
693 * This routine initializes a thread, then schedules it for execution.
694 *
695 * The new thread may be scheduled for immediate execution or a delayed start.
696 * If the newly spawned thread does not have a delayed start the kernel
697 * scheduler may preempt the current thread to allow the new thread to
698 * execute.
699 *
700 * Thread options are architecture-specific, and can include K_ESSENTIAL,
701 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
702 * them using "|" (the logical OR operator).
703 *
704 * Historically, users often would use the beginning of the stack memory region
705 * to store the struct k_thread data, although corruption will occur if the
706 * stack overflows this region and stack protection features may not detect this
707 * situation.
708 *
709 * @param new_thread Pointer to uninitialized struct k_thread
710 * @param stack Pointer to the stack space.
711 * @param stack_size Stack size in bytes.
712 * @param entry Thread entry function.
713 * @param p1 1st entry point parameter.
714 * @param p2 2nd entry point parameter.
715 * @param p3 3rd entry point parameter.
716 * @param prio Thread priority.
717 * @param options Thread options.
718 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
719 *
720 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400721 *
722 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700723 */
Andrew Boie662c3452017-10-02 10:51:18 -0700724__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700725 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700726 size_t stack_size,
727 k_thread_entry_t entry,
728 void *p1, void *p2, void *p3,
729 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700730
Andrew Boie3f091b52017-08-30 14:34:14 -0700731/**
732 * @brief Drop a thread's privileges permanently to user mode
733 *
734 * @param entry Function to start executing from
735 * @param p1 1st entry point parameter
736 * @param p2 2nd entry point parameter
737 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400738 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700739 */
740extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
741 void *p1, void *p2,
742 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700743
Andrew Boied26cf2d2017-03-30 13:07:02 -0700744/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700745 * @brief Grant a thread access to a NULL-terminated set of kernel objects
746 *
747 * This is a convenience function. For the provided thread, grant access to
748 * the remaining arguments, which must be pointers to kernel objects.
749 * The final argument must be a NULL.
750 *
751 * The thread object must be initialized (i.e. running). The objects don't
752 * need to be.
753 *
754 * @param thread Thread to grant access to objects
755 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400756 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700757 */
758extern void __attribute__((sentinel))
759 k_thread_access_grant(struct k_thread *thread, ...);
760
761/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700762 * @brief Assign a resource memory pool to a thread
763 *
764 * By default, threads have no resource pool assigned unless their parent
765 * thread has a resource pool, in which case it is inherited. Multiple
766 * threads may be assigned to the same memory pool.
767 *
768 * Changing a thread's resource pool will not migrate allocations from the
769 * previous pool.
770 *
771 * @param thread Target thread to assign a memory pool for resource requests,
772 * or NULL if the thread should no longer have a memory pool.
773 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400774 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700775 */
776static inline void k_thread_resource_pool_assign(struct k_thread *thread,
777 struct k_mem_pool *pool)
778{
779 thread->resource_pool = pool;
780}
781
782#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
783/**
784 * @brief Assign the system heap as a thread's resource pool
785 *
786 * Similar to k_thread_resource_pool_assign(), but the thread will use
787 * the kernel heap to draw memory.
788 *
789 * Use with caution, as a malicious thread could perform DoS attacks on the
790 * kernel heap.
791 *
792 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400793 *
794 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700795 */
796void k_thread_system_pool_assign(struct k_thread *thread);
797#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
798
799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Allan Stephensc98da842016-11-11 15:45:03 -0500802 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500803 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
807 * @return N/A
808 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700809__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810
811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 *
814 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500815 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 * @return N/A
818 */
Kumar Galacc334c72017-04-21 10:55:34 -0500819extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820
821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
828 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400829 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 */
Andrew Boie468190a2017-09-29 14:00:48 -0700831__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832
833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * If @a thread is not currently sleeping, the routine has no effect.
839 *
840 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
842 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400843 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 */
Andrew Boie468190a2017-09-29 14:00:48 -0700845__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846
847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400851 *
852 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700854__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855
856/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * This routine prevents @a thread from executing if it has not yet started
860 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @param thread ID of thread to cancel.
863 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700864 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500865 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700866 *
867 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700869__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400870
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871/**
Allan Stephensc98da842016-11-11 15:45:03 -0500872 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * This routine permanently stops execution of @a thread. The thread is taken
875 * off all kernel queues it is part of (i.e. the ready queue, the timeout
876 * queue, or a kernel object wait queue). However, any kernel resources the
877 * thread might currently own (such as mutexes or memory blocks) are not
878 * released. It is the responsibility of the caller of this routine to ensure
879 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
883 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400884 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 */
Andrew Boie468190a2017-09-29 14:00:48 -0700886__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400887
Andrew Boie7d627c52017-08-30 11:01:56 -0700888
889/**
890 * @brief Start an inactive thread
891 *
892 * If a thread was created with K_FOREVER in the delay parameter, it will
893 * not be added to the scheduling queue until this function is called
894 * on it.
895 *
896 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400897 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700898 */
Andrew Boie468190a2017-09-29 14:00:48 -0700899__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700900
Allan Stephensc98da842016-11-11 15:45:03 -0500901/**
902 * @cond INTERNAL_HIDDEN
903 */
904
Benjamin Walshd211a522016-12-06 11:44:01 -0500905/* timeout has timed out and is not on _timeout_q anymore */
906#define _EXPIRED (-2)
907
908/* timeout is not in use */
909#define _INACTIVE (-1)
910
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400911struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700912 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700913 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400914 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700915 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500916 void *init_p1;
917 void *init_p2;
918 void *init_p3;
919 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500920 u32_t init_options;
921 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500922 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600923 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400924};
925
Andrew Boied26cf2d2017-03-30 13:07:02 -0700926#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400927 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600928 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500929 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700930 .init_thread = (thread), \
931 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500932 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700933 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400934 .init_p1 = (void *)p1, \
935 .init_p2 = (void *)p2, \
936 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500937 .init_prio = (prio), \
938 .init_options = (options), \
939 .init_delay = (delay), \
940 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600941 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400942 }
943
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400944/**
Allan Stephensc98da842016-11-11 15:45:03 -0500945 * INTERNAL_HIDDEN @endcond
946 */
947
948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 * @brief Statically define and initialize a thread.
950 *
951 * The thread may be scheduled for immediate execution or a delayed start.
952 *
953 * Thread options are architecture-specific, and can include K_ESSENTIAL,
954 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
955 * them using "|" (the logical OR operator).
956 *
957 * The ID of the thread can be accessed using:
958 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500959 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500960 *
961 * @param name Name of the thread.
962 * @param stack_size Stack size in bytes.
963 * @param entry Thread entry function.
964 * @param p1 1st entry point parameter.
965 * @param p2 2nd entry point parameter.
966 * @param p3 3rd entry point parameter.
967 * @param prio Thread priority.
968 * @param options Thread options.
969 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400970 *
Anas Nashif47420d02018-05-24 14:20:56 -0400971 * @req K-THREAD-010
972 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400973 * @internal It has been observed that the x86 compiler by default aligns
974 * these _static_thread_data structures to 32-byte boundaries, thereby
975 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400976 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400977 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500978#define K_THREAD_DEFINE(name, stack_size, \
979 entry, p1, p2, p3, \
980 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700981 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700982 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500983 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500984 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700985 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
986 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500987 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600988 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700989 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400990
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * @param thread ID of thread whose priority is needed.
997 *
998 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400999 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001001__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002
1003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 *
1008 * Rescheduling can occur immediately depending on the priority @a thread is
1009 * set to:
1010 *
1011 * - If its priority is raised above the priority of the caller of this
1012 * function, and the caller is preemptible, @a thread will be scheduled in.
1013 *
1014 * - If the caller operates on itself, it lowers its priority below that of
1015 * other threads in the system, and the caller is preemptible, the thread of
1016 * highest priority will be scheduled in.
1017 *
1018 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1019 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1020 * highest priority.
1021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001023 * @param prio New priority.
1024 *
1025 * @warning Changing the priority of a thread currently involved in mutex
1026 * priority inheritance may result in undefined behavior.
1027 *
1028 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001029 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030 */
Andrew Boie468190a2017-09-29 14:00:48 -07001031__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001032
Andy Ross4a2e50f2018-05-15 11:06:25 -07001033
1034#ifdef CONFIG_SCHED_DEADLINE
1035/**
1036 * @brief Set deadline expiration time for scheduler
1037 *
1038 * This sets the "deadline" expiration as a time delta from the
1039 * current time, in the same units used by k_cycle_get_32(). The
1040 * scheduler (when deadline scheduling is enabled) will choose the
1041 * next expiring thread when selecting between threads at the same
1042 * static priority. Threads at different priorities will be scheduled
1043 * according to their static priority.
1044 *
1045 * @note Deadlines that are negative (i.e. in the past) are still seen
1046 * as higher priority than others, even if the thread has "finished"
1047 * its work. If you don't want it scheduled anymore, you have to
1048 * reset the deadline into the future, block/pend the thread, or
1049 * modify its priority with k_thread_priority_set().
1050 *
1051 * @note Despite the API naming, the scheduler makes no guarantees the
1052 * the thread WILL be scheduled within that deadline, nor does it take
1053 * extra metadata (like e.g. the "runtime" and "period" parameters in
1054 * Linux sched_setattr()) that allows the kernel to validate the
1055 * scheduling for achievability. Such features could be implemented
1056 * above this call, which is simply input to the priority selection
1057 * logic.
1058 *
1059 * @param thread A thread on which to set the deadline
1060 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001061 *
1062 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001063 */
1064__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1065#endif
1066
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001068 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * This routine prevents the kernel scheduler from making @a thread the
1071 * current thread. All other internal operations on @a thread are still
1072 * performed; for example, any timeout it is waiting on keeps ticking,
1073 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * If @a thread is already suspended, the routine has no effect.
1076 *
1077 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078 *
1079 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001080 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 */
Andrew Boie468190a2017-09-29 14:00:48 -07001082__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001083
1084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001085 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001087 * This routine allows the kernel scheduler to make @a thread the current
1088 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * If @a thread is not currently suspended, the routine has no effect.
1091 *
1092 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
1094 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001095 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 */
Andrew Boie468190a2017-09-29 14:00:48 -07001097__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001100 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * This routine specifies how the scheduler will perform time slicing of
1103 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001105 * To enable time slicing, @a slice must be non-zero. The scheduler
1106 * ensures that no thread runs for more than the specified time limit
1107 * before other threads of that priority are given a chance to execute.
1108 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001109 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001111 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112 * execute. Once the scheduler selects a thread for execution, there is no
1113 * minimum guaranteed time the thread will execute before threads of greater or
1114 * equal priority are scheduled.
1115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 * for execution, this routine has no effect; the thread is immediately
1118 * rescheduled after the slice period expires.
1119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * To disable timeslicing, set both @a slice and @a prio to zero.
1121 *
1122 * @param slice Maximum time slice length (in milliseconds).
1123 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
1125 * @return N/A
1126 */
Kumar Galacc334c72017-04-21 10:55:34 -05001127extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001128
Anas Nashif166f5192018-02-25 08:02:36 -06001129/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001130
1131/**
1132 * @addtogroup isr_apis
1133 * @{
1134 */
1135
1136/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephensc98da842016-11-11 15:45:03 -05001139 * This routine allows the caller to customize its actions, depending on
1140 * whether it is a thread or an ISR.
1141 *
1142 * @note Can be called by ISRs.
1143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * @return 0 if invoked by a thread.
1145 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001147extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001148
Benjamin Walsh445830d2016-11-10 15:54:27 -05001149/**
1150 * @brief Determine if code is running in a preemptible thread.
1151 *
Allan Stephensc98da842016-11-11 15:45:03 -05001152 * This routine allows the caller to customize its actions, depending on
1153 * whether it can be preempted by another thread. The routine returns a 'true'
1154 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001155 *
Allan Stephensc98da842016-11-11 15:45:03 -05001156 * - The code is running in a thread, not at ISR.
1157 * - The thread's priority is in the preemptible range.
1158 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001159 *
Allan Stephensc98da842016-11-11 15:45:03 -05001160 * @note Can be called by ISRs.
1161 *
1162 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001163 * @return Non-zero if invoked by a preemptible thread.
1164 */
Andrew Boie468190a2017-09-29 14:00:48 -07001165__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001166
Allan Stephensc98da842016-11-11 15:45:03 -05001167/**
Anas Nashif166f5192018-02-25 08:02:36 -06001168 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001169 */
1170
1171/**
1172 * @addtogroup thread_apis
1173 * @{
1174 */
1175
1176/**
1177 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001178 *
Allan Stephensc98da842016-11-11 15:45:03 -05001179 * This routine prevents the current thread from being preempted by another
1180 * thread by instructing the scheduler to treat it as a cooperative thread.
1181 * If the thread subsequently performs an operation that makes it unready,
1182 * it will be context switched out in the normal manner. When the thread
1183 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001184 *
Allan Stephensc98da842016-11-11 15:45:03 -05001185 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001186 *
Allan Stephensc98da842016-11-11 15:45:03 -05001187 * @note k_sched_lock() and k_sched_unlock() should normally be used
1188 * when the operation being performed can be safely interrupted by ISRs.
1189 * However, if the amount of processing involved is very small, better
1190 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001191 *
1192 * @return N/A
1193 */
1194extern void k_sched_lock(void);
1195
Allan Stephensc98da842016-11-11 15:45:03 -05001196/**
1197 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001198 *
Allan Stephensc98da842016-11-11 15:45:03 -05001199 * This routine reverses the effect of a previous call to k_sched_lock().
1200 * A thread must call the routine once for each time it called k_sched_lock()
1201 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001202 *
1203 * @return N/A
1204 */
1205extern void k_sched_unlock(void);
1206
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001208 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001210 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001212 * Custom data is not used by the kernel itself, and is freely available
1213 * for a thread to use as it sees fit. It can be used as a framework
1214 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001216 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001217 *
1218 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001219 *
1220 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001221 */
Andrew Boie468190a2017-09-29 14:00:48 -07001222__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223
1224/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001227 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001230 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001231 */
Andrew Boie468190a2017-09-29 14:00:48 -07001232__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001233
1234/**
Anas Nashif57554052018-03-03 02:31:05 -06001235 * @brief Set current thread name
1236 *
1237 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1238 * tracing and debugging.
1239 *
1240 */
1241__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1242
1243/**
1244 * @brief Get thread name
1245 *
1246 * Get the name of a thread
1247 *
1248 * @param thread_id Thread ID
1249 *
1250 */
1251__syscall const char *k_thread_name_get(k_tid_t thread_id);
1252
1253/**
Anas Nashif166f5192018-02-25 08:02:36 -06001254 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001255 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001256#include <sys_clock.h>
1257
Allan Stephensc2f15a42016-11-17 12:24:22 -05001258/**
1259 * @addtogroup clock_apis
1260 * @{
1261 */
1262
1263/**
1264 * @brief Generate null timeout delay.
1265 *
1266 * This macro generates a timeout delay that that instructs a kernel API
1267 * not to wait if the requested operation cannot be performed immediately.
1268 *
1269 * @return Timeout delay value.
1270 */
1271#define K_NO_WAIT 0
1272
1273/**
1274 * @brief Generate timeout delay from milliseconds.
1275 *
1276 * This macro generates a timeout delay that that instructs a kernel API
1277 * to wait up to @a ms milliseconds to perform the requested operation.
1278 *
1279 * @param ms Duration in milliseconds.
1280 *
1281 * @return Timeout delay value.
1282 */
Johan Hedberg14471692016-11-13 10:52:15 +02001283#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001284
1285/**
1286 * @brief Generate timeout delay from seconds.
1287 *
1288 * This macro generates a timeout delay that that instructs a kernel API
1289 * to wait up to @a s seconds to perform the requested operation.
1290 *
1291 * @param s Duration in seconds.
1292 *
1293 * @return Timeout delay value.
1294 */
Johan Hedberg14471692016-11-13 10:52:15 +02001295#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001296
1297/**
1298 * @brief Generate timeout delay from minutes.
1299 *
1300 * This macro generates a timeout delay that that instructs a kernel API
1301 * to wait up to @a m minutes to perform the requested operation.
1302 *
1303 * @param m Duration in minutes.
1304 *
1305 * @return Timeout delay value.
1306 */
Johan Hedberg14471692016-11-13 10:52:15 +02001307#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001308
1309/**
1310 * @brief Generate timeout delay from hours.
1311 *
1312 * This macro generates a timeout delay that that instructs a kernel API
1313 * to wait up to @a h hours to perform the requested operation.
1314 *
1315 * @param h Duration in hours.
1316 *
1317 * @return Timeout delay value.
1318 */
Johan Hedberg14471692016-11-13 10:52:15 +02001319#define K_HOURS(h) K_MINUTES((h) * 60)
1320
Allan Stephensc98da842016-11-11 15:45:03 -05001321/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001322 * @brief Generate infinite timeout delay.
1323 *
1324 * This macro generates a timeout delay that that instructs a kernel API
1325 * to wait as long as necessary to perform the requested operation.
1326 *
1327 * @return Timeout delay value.
1328 */
1329#define K_FOREVER (-1)
1330
1331/**
Anas Nashif166f5192018-02-25 08:02:36 -06001332 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001333 */
1334
1335/**
Allan Stephensc98da842016-11-11 15:45:03 -05001336 * @cond INTERNAL_HIDDEN
1337 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001338
Benjamin Walsh62092182016-12-20 14:39:08 -05001339/* kernel clocks */
1340
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001341#ifdef CONFIG_SYS_CLOCK_EXISTS
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001342
1343/*
1344 * If timer frequency is known at compile time, a simple (32-bit)
1345 * tick <-> ms conversion could be used for some combinations of
1346 * hardware timer frequency and tick rate. Otherwise precise
1347 * (64-bit) calculations are used.
1348 */
1349
1350#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1351#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001352 #define _NEED_PRECISE_TICK_MS_CONVERSION
1353#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001354 #define _NON_OPTIMIZED_TICKS_PER_SEC
1355#endif
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001356#endif
Benjamin Walsh62092182016-12-20 14:39:08 -05001357
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001358#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
1359 defined(_NON_OPTIMIZED_TICKS_PER_SEC)
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001360 #define _NEED_PRECISE_TICK_MS_CONVERSION
1361#endif
1362#endif
1363
Kumar Galacc334c72017-04-21 10:55:34 -05001364static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001365{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001366#ifdef CONFIG_SYS_CLOCK_EXISTS
1367
1368#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1369 /* use 64-bit math to keep precision */
Piotr Zięcik3c7f9902018-07-23 14:09:10 +02001370 return (s32_t)ceiling_fraction(
1371 (s64_t)ms * sys_clock_hw_cycles_per_sec,
Vinayak Kariappa Chettimadac7d27342018-08-31 08:58:59 +02001372 ((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) /
1373 sys_clock_ticks_per_sec);
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001374#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001375 /* simple division keeps precision */
1376 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1377
1378 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1379#endif
1380
1381#else
1382 __ASSERT(ms == 0, "ms not zero");
1383 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001384#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001385}
Benjamin Walsh62092182016-12-20 14:39:08 -05001386
Kumar Galacc334c72017-04-21 10:55:34 -05001387static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001388{
Benjamin Walsh62092182016-12-20 14:39:08 -05001389#ifdef CONFIG_SYS_CLOCK_EXISTS
1390
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001391#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1392 /* use 64-bit math to keep precision */
Vinayak Kariappa Chettimadac7d27342018-08-31 08:58:59 +02001393 return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001394#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001395 /* simple multiplication keeps precision */
1396 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1397
1398 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001399#endif
1400
1401#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001402 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001403 return 0;
1404#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001405}
1406
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001407/* added tick needed to account for tick in progress */
1408#ifdef CONFIG_TICKLESS_KERNEL
1409#define _TICK_ALIGN 0
1410#else
1411#define _TICK_ALIGN 1
1412#endif
1413
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414struct k_timer {
1415 /*
1416 * _timeout structure must be first here if we want to use
1417 * dynamic timer allocation. timeout.node is used in the double-linked
1418 * list of free timers
1419 */
1420 struct _timeout timeout;
1421
Allan Stephens45bfa372016-10-12 12:39:42 -05001422 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423 _wait_q_t wait_q;
1424
1425 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001426 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001427
1428 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001429 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430
1431 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001432 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433
Allan Stephens45bfa372016-10-12 12:39:42 -05001434 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001435 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001436
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001437 /* user-specific data, also used to support legacy features */
1438 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001439
Anas Nashif2f203c22016-12-18 06:57:45 -05001440 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441};
1442
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001443#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001445 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001446 .timeout.wait_q = NULL, \
1447 .timeout.thread = NULL, \
1448 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001449 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001450 .expiry_fn = expiry, \
1451 .stop_fn = stop, \
1452 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001453 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001454 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455 }
1456
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001457#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1458
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459/**
Allan Stephensc98da842016-11-11 15:45:03 -05001460 * INTERNAL_HIDDEN @endcond
1461 */
1462
1463/**
1464 * @defgroup timer_apis Timer APIs
1465 * @ingroup kernel_apis
1466 * @{
1467 */
1468
1469/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001470 * @typedef k_timer_expiry_t
1471 * @brief Timer expiry function type.
1472 *
1473 * A timer's expiry function is executed by the system clock interrupt handler
1474 * each time the timer expires. The expiry function is optional, and is only
1475 * invoked if the timer has been initialized with one.
1476 *
1477 * @param timer Address of timer.
1478 *
1479 * @return N/A
1480 */
1481typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1482
1483/**
1484 * @typedef k_timer_stop_t
1485 * @brief Timer stop function type.
1486 *
1487 * A timer's stop function is executed if the timer is stopped prematurely.
1488 * The function runs in the context of the thread that stops the timer.
1489 * The stop function is optional, and is only invoked if the timer has been
1490 * initialized with one.
1491 *
1492 * @param timer Address of timer.
1493 *
1494 * @return N/A
1495 */
1496typedef void (*k_timer_stop_t)(struct k_timer *timer);
1497
1498/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001499 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001501 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001503 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001504 *
1505 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001506 * @param expiry_fn Function to invoke each time the timer expires.
1507 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001508 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001509#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001510 struct k_timer name \
1511 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001512 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001513
Allan Stephens45bfa372016-10-12 12:39:42 -05001514/**
1515 * @brief Initialize a timer.
1516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001517 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001518 *
1519 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001520 * @param expiry_fn Function to invoke each time the timer expires.
1521 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001522 *
1523 * @return N/A
1524 */
1525extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001526 k_timer_expiry_t expiry_fn,
1527 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001528
Allan Stephens45bfa372016-10-12 12:39:42 -05001529/**
1530 * @brief Start a timer.
1531 *
1532 * This routine starts a timer, and resets its status to zero. The timer
1533 * begins counting down using the specified duration and period values.
1534 *
1535 * Attempting to start a timer that is already running is permitted.
1536 * The timer's status is reset to zero and the timer begins counting down
1537 * using the new duration and period values.
1538 *
1539 * @param timer Address of timer.
1540 * @param duration Initial timer duration (in milliseconds).
1541 * @param period Timer period (in milliseconds).
1542 *
1543 * @return N/A
1544 */
Andrew Boiea354d492017-09-29 16:22:28 -07001545__syscall void k_timer_start(struct k_timer *timer,
1546 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001547
1548/**
1549 * @brief Stop a timer.
1550 *
1551 * This routine stops a running timer prematurely. The timer's stop function,
1552 * if one exists, is invoked by the caller.
1553 *
1554 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001555 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001556 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001557 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1558 * if @a k_timer_stop is to be called from ISRs.
1559 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001560 * @param timer Address of timer.
1561 *
1562 * @return N/A
1563 */
Andrew Boiea354d492017-09-29 16:22:28 -07001564__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001565
1566/**
1567 * @brief Read timer status.
1568 *
1569 * This routine reads the timer's status, which indicates the number of times
1570 * it has expired since its status was last read.
1571 *
1572 * Calling this routine resets the timer's status to zero.
1573 *
1574 * @param timer Address of timer.
1575 *
1576 * @return Timer status.
1577 */
Andrew Boiea354d492017-09-29 16:22:28 -07001578__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001579
1580/**
1581 * @brief Synchronize thread to timer expiration.
1582 *
1583 * This routine blocks the calling thread until the timer's status is non-zero
1584 * (indicating that it has expired at least once since it was last examined)
1585 * or the timer is stopped. If the timer status is already non-zero,
1586 * or the timer is already stopped, the caller continues without waiting.
1587 *
1588 * Calling this routine resets the timer's status to zero.
1589 *
1590 * This routine must not be used by interrupt handlers, since they are not
1591 * allowed to block.
1592 *
1593 * @param timer Address of timer.
1594 *
1595 * @return Timer status.
1596 */
Andrew Boiea354d492017-09-29 16:22:28 -07001597__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001598
1599/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001600 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001601 *
1602 * This routine computes the (approximate) time remaining before a running
1603 * timer next expires. If the timer is not running, it returns zero.
1604 *
1605 * @param timer Address of timer.
1606 *
1607 * @return Remaining time (in milliseconds).
1608 */
Andrew Boiea354d492017-09-29 16:22:28 -07001609__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1610
1611static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001612{
1613 return _timeout_remaining_get(&timer->timeout);
1614}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001615
Allan Stephensc98da842016-11-11 15:45:03 -05001616/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001617 * @brief Associate user-specific data with a timer.
1618 *
1619 * This routine records the @a user_data with the @a timer, to be retrieved
1620 * later.
1621 *
1622 * It can be used e.g. in a timer handler shared across multiple subsystems to
1623 * retrieve data specific to the subsystem this timer is associated with.
1624 *
1625 * @param timer Address of timer.
1626 * @param user_data User data to associate with the timer.
1627 *
1628 * @return N/A
1629 */
Andrew Boiea354d492017-09-29 16:22:28 -07001630__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1631
Anas Nashif954d5502018-02-25 08:37:28 -06001632/**
1633 * @internal
1634 */
Andrew Boiea354d492017-09-29 16:22:28 -07001635static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1636 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001637{
1638 timer->user_data = user_data;
1639}
1640
1641/**
1642 * @brief Retrieve the user-specific data from a timer.
1643 *
1644 * @param timer Address of timer.
1645 *
1646 * @return The user data.
1647 */
Andrew Boiea354d492017-09-29 16:22:28 -07001648__syscall void *k_timer_user_data_get(struct k_timer *timer);
1649
1650static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001651{
1652 return timer->user_data;
1653}
1654
Anas Nashif166f5192018-02-25 08:02:36 -06001655/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001656
Allan Stephensc98da842016-11-11 15:45:03 -05001657/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001658 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001659 * @{
1660 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001661
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001663 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001665 * This routine returns the elapsed time since the system booted,
1666 * in milliseconds.
1667 *
1668 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001669 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001670__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001671
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001672/**
1673 * @brief Enable clock always on in tickless kernel
1674 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001675 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001676 * there are no timer events programmed in tickless kernel
1677 * scheduling. This is necessary if the clock is used to track
1678 * passage of time.
1679 *
1680 * @retval prev_status Previous status of always on flag
1681 */
1682static inline int k_enable_sys_clock_always_on(void)
1683{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001684#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001685 int prev_status = _sys_clock_always_on;
1686
1687 _sys_clock_always_on = 1;
1688 _enable_sys_clock();
1689
1690 return prev_status;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301691#else
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001692 return -ENOTSUP;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301693#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001694}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001695
1696/**
1697 * @brief Disable clock always on in tickless kernel
1698 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001699 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001700 * there are no timer events programmed in tickless kernel
1701 * scheduling. To save power, this routine should be called
1702 * immediately when clock is not used to track time.
1703 */
1704static inline void k_disable_sys_clock_always_on(void)
1705{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001706#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001707 _sys_clock_always_on = 0;
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001708#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001709}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001710
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * This routine returns the lower 32-bits of the elapsed time since the system
1715 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * This routine can be more efficient than k_uptime_get(), as it reduces the
1718 * need for interrupt locking and 64-bit math. However, the 32-bit result
1719 * cannot hold a system uptime time larger than approximately 50 days, so the
1720 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001723 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001724__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001725
1726/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001727 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * This routine computes the elapsed time between the current system uptime
1730 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @param reftime Pointer to a reference time, which is updated to the current
1733 * uptime upon return.
1734 *
1735 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001736 */
Kumar Galacc334c72017-04-21 10:55:34 -05001737extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001738
1739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * This routine computes the elapsed time between the current system uptime
1743 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001745 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1746 * need for interrupt locking and 64-bit math. However, the 32-bit result
1747 * cannot hold an elapsed time larger than approximately 50 days, so the
1748 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001750 * @param reftime Pointer to a reference time, which is updated to the current
1751 * uptime upon return.
1752 *
1753 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001754 */
Kumar Galacc334c72017-04-21 10:55:34 -05001755extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001756
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001758 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001760 * This routine returns the current time, as measured by the system's hardware
1761 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001763 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001765#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001767/**
Anas Nashif166f5192018-02-25 08:02:36 -06001768 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001769 */
1770
Allan Stephensc98da842016-11-11 15:45:03 -05001771/**
1772 * @cond INTERNAL_HIDDEN
1773 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001774
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001775struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001776 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001777 union {
1778 _wait_q_t wait_q;
1779
1780 _POLL_EVENT;
1781 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001782
1783 _OBJECT_TRACING_NEXT_PTR(k_queue);
1784};
1785
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001786#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001787 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001788 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001789 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001790 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001791 _OBJECT_TRACING_INIT \
1792 }
1793
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001794#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1795
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001796extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1797
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001798/**
1799 * INTERNAL_HIDDEN @endcond
1800 */
1801
1802/**
1803 * @defgroup queue_apis Queue APIs
1804 * @ingroup kernel_apis
1805 * @{
1806 */
1807
1808/**
1809 * @brief Initialize a queue.
1810 *
1811 * This routine initializes a queue object, prior to its first use.
1812 *
1813 * @param queue Address of the queue.
1814 *
1815 * @return N/A
1816 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001817__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001818
1819/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001820 * @brief Cancel waiting on a queue.
1821 *
1822 * This routine causes first thread pending on @a queue, if any, to
1823 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001824 * If the queue is being waited on by k_poll(), it will return with
1825 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1826 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001827 *
1828 * @note Can be called by ISRs.
1829 *
1830 * @param queue Address of the queue.
1831 *
1832 * @return N/A
1833 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001834__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001835
1836/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001837 * @brief Append an element to the end of a queue.
1838 *
1839 * This routine appends a data item to @a queue. A queue data item must be
1840 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1841 * reserved for the kernel's use.
1842 *
1843 * @note Can be called by ISRs.
1844 *
1845 * @param queue Address of the queue.
1846 * @param data Address of the data item.
1847 *
1848 * @return N/A
1849 */
1850extern void k_queue_append(struct k_queue *queue, void *data);
1851
1852/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001853 * @brief Append an element to a queue.
1854 *
1855 * This routine appends a data item to @a queue. There is an implicit
1856 * memory allocation from the calling thread's resource pool, which is
1857 * automatically freed when the item is removed from the queue.
1858 *
1859 * @note Can be called by ISRs.
1860 *
1861 * @param queue Address of the queue.
1862 * @param data Address of the data item.
1863 *
1864 * @retval 0 on success
1865 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1866 */
1867__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1868
1869/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001870 * @brief Prepend an element to a queue.
1871 *
1872 * This routine prepends a data item to @a queue. A queue data item must be
1873 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1874 * reserved for the kernel's use.
1875 *
1876 * @note Can be called by ISRs.
1877 *
1878 * @param queue Address of the queue.
1879 * @param data Address of the data item.
1880 *
1881 * @return N/A
1882 */
1883extern void k_queue_prepend(struct k_queue *queue, void *data);
1884
1885/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001886 * @brief Prepend an element to a queue.
1887 *
1888 * This routine prepends a data item to @a queue. There is an implicit
1889 * memory allocation from the calling thread's resource pool, which is
1890 * automatically freed when the item is removed from the queue.
1891 *
1892 * @note Can be called by ISRs.
1893 *
1894 * @param queue Address of the queue.
1895 * @param data Address of the data item.
1896 *
1897 * @retval 0 on success
1898 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1899 */
1900__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1901
1902/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001903 * @brief Inserts an element to a queue.
1904 *
1905 * This routine inserts a data item to @a queue after previous item. A queue
1906 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1907 * item are reserved for the kernel's use.
1908 *
1909 * @note Can be called by ISRs.
1910 *
1911 * @param queue Address of the queue.
1912 * @param prev Address of the previous data item.
1913 * @param data Address of the data item.
1914 *
1915 * @return N/A
1916 */
1917extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1918
1919/**
1920 * @brief Atomically append a list of elements to a queue.
1921 *
1922 * This routine adds a list of data items to @a queue in one operation.
1923 * The data items must be in a singly-linked list, with the first 32 bits
1924 * in each data item pointing to the next data item; the list must be
1925 * NULL-terminated.
1926 *
1927 * @note Can be called by ISRs.
1928 *
1929 * @param queue Address of the queue.
1930 * @param head Pointer to first node in singly-linked list.
1931 * @param tail Pointer to last node in singly-linked list.
1932 *
1933 * @return N/A
1934 */
1935extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1936
1937/**
1938 * @brief Atomically add a list of elements to a queue.
1939 *
1940 * This routine adds a list of data items to @a queue in one operation.
1941 * The data items must be in a singly-linked list implemented using a
1942 * sys_slist_t object. Upon completion, the original list is empty.
1943 *
1944 * @note Can be called by ISRs.
1945 *
1946 * @param queue Address of the queue.
1947 * @param list Pointer to sys_slist_t object.
1948 *
1949 * @return N/A
1950 */
1951extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1952
1953/**
1954 * @brief Get an element from a queue.
1955 *
1956 * This routine removes first data item from @a queue. The first 32 bits of the
1957 * data item are reserved for the kernel's use.
1958 *
1959 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1960 *
1961 * @param queue Address of the queue.
1962 * @param timeout Waiting period to obtain a data item (in milliseconds),
1963 * or one of the special values K_NO_WAIT and K_FOREVER.
1964 *
1965 * @return Address of the data item if successful; NULL if returned
1966 * without waiting, or waiting period timed out.
1967 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001968__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001969
1970/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001971 * @brief Remove an element from a queue.
1972 *
1973 * This routine removes data item from @a queue. The first 32 bits of the
1974 * data item are reserved for the kernel's use. Removing elements from k_queue
1975 * rely on sys_slist_find_and_remove which is not a constant time operation.
1976 *
1977 * @note Can be called by ISRs
1978 *
1979 * @param queue Address of the queue.
1980 * @param data Address of the data item.
1981 *
1982 * @return true if data item was removed
1983 */
1984static inline bool k_queue_remove(struct k_queue *queue, void *data)
1985{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001986 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001987}
1988
1989/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001990 * @brief Append an element to a queue only if it's not present already.
1991 *
1992 * This routine appends data item to @a queue. The first 32 bits of the
1993 * data item are reserved for the kernel's use. Appending elements to k_queue
1994 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1995 *
1996 * @note Can be called by ISRs
1997 *
1998 * @param queue Address of the queue.
1999 * @param data Address of the data item.
2000 *
2001 * @return true if data item was added, false if not
2002 */
2003static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2004{
2005 sys_sfnode_t *test;
2006
2007 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2008 if (test == (sys_sfnode_t *) data) {
2009 return false;
2010 }
2011 }
2012
2013 k_queue_append(queue, data);
2014 return true;
2015}
2016
2017/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002018 * @brief Query a queue to see if it has data available.
2019 *
2020 * Note that the data might be already gone by the time this function returns
2021 * if other threads are also trying to read from the queue.
2022 *
2023 * @note Can be called by ISRs.
2024 *
2025 * @param queue Address of the queue.
2026 *
2027 * @return Non-zero if the queue is empty.
2028 * @return 0 if data is available.
2029 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002030__syscall int k_queue_is_empty(struct k_queue *queue);
2031
2032static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002033{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002034 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002035}
2036
2037/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002038 * @brief Peek element at the head of queue.
2039 *
2040 * Return element from the head of queue without removing it.
2041 *
2042 * @param queue Address of the queue.
2043 *
2044 * @return Head element, or NULL if queue is empty.
2045 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002046__syscall void *k_queue_peek_head(struct k_queue *queue);
2047
2048static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002049{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002050 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002051}
2052
2053/**
2054 * @brief Peek element at the tail of queue.
2055 *
2056 * Return element from the tail of queue without removing it.
2057 *
2058 * @param queue Address of the queue.
2059 *
2060 * @return Tail element, or NULL if queue is empty.
2061 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002062__syscall void *k_queue_peek_tail(struct k_queue *queue);
2063
2064static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002065{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002066 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002067}
2068
2069/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002070 * @brief Statically define and initialize a queue.
2071 *
2072 * The queue can be accessed outside the module where it is defined using:
2073 *
2074 * @code extern struct k_queue <name>; @endcode
2075 *
2076 * @param name Name of the queue.
2077 */
2078#define K_QUEUE_DEFINE(name) \
2079 struct k_queue name \
2080 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002081 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002082
Anas Nashif166f5192018-02-25 08:02:36 -06002083/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002084
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002085struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002086 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087};
2088
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002089/**
2090 * @cond INTERNAL_HIDDEN
2091 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002092#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002093 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002094 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002095 }
2096
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002097#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2098
Allan Stephensc98da842016-11-11 15:45:03 -05002099/**
2100 * INTERNAL_HIDDEN @endcond
2101 */
2102
2103/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002104 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002105 * @ingroup kernel_apis
2106 * @{
2107 */
2108
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002110 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002114 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115 *
2116 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002117 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002118 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002119#define k_fifo_init(fifo) \
2120 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002121
2122/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002123 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002124 *
2125 * This routine causes first thread pending on @a fifo, if any, to
2126 * return from k_fifo_get() call with NULL value (as if timeout
2127 * expired).
2128 *
2129 * @note Can be called by ISRs.
2130 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002131 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002132 *
2133 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002134 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002135 */
2136#define k_fifo_cancel_wait(fifo) \
2137 k_queue_cancel_wait((struct k_queue *) fifo)
2138
2139/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002140 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002141 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002142 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002143 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2144 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * @note Can be called by ISRs.
2147 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
2151 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002152 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002153 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002154#define k_fifo_put(fifo, data) \
2155 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156
2157/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002158 * @brief Add an element to a FIFO queue.
2159 *
2160 * This routine adds a data item to @a fifo. There is an implicit
2161 * memory allocation from the calling thread's resource pool, which is
2162 * automatically freed when the item is removed.
2163 *
2164 * @note Can be called by ISRs.
2165 *
2166 * @param fifo Address of the FIFO.
2167 * @param data Address of the data item.
2168 *
2169 * @retval 0 on success
2170 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002171 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002172 */
2173#define k_fifo_alloc_put(fifo, data) \
2174 k_queue_alloc_append((struct k_queue *) fifo, data)
2175
2176/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002177 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002179 * This routine adds a list of data items to @a fifo in one operation.
2180 * The data items must be in a singly-linked list, with the first 32 bits
2181 * each data item pointing to the next data item; the list must be
2182 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002186 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002187 * @param head Pointer to first node in singly-linked list.
2188 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002189 *
2190 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002191 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002193#define k_fifo_put_list(fifo, head, tail) \
2194 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195
2196/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002197 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * This routine adds a list of data items to @a fifo in one operation.
2200 * The data items must be in a singly-linked list implemented using a
2201 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002202 * and must be re-initialized via sys_slist_init().
2203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002204 * @note Can be called by ISRs.
2205 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002207 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002208 *
2209 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002210 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002212#define k_fifo_put_slist(fifo, list) \
2213 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214
2215/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002218 * This routine removes a data item from @a fifo in a "first in, first out"
2219 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002221 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2222 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002223 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002224 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225 * or one of the special values K_NO_WAIT and K_FOREVER.
2226 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002227 * @return Address of the data item if successful; NULL if returned
2228 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002229 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002231#define k_fifo_get(fifo, timeout) \
2232 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002236 *
2237 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002238 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002239 *
2240 * @note Can be called by ISRs.
2241 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002242 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002243 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002244 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002245 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002246 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002247 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002248#define k_fifo_is_empty(fifo) \
2249 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002250
2251/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002252 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002253 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002254 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302255 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002256 * on each iteration of processing, a head container will be peeked,
2257 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002258 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002259 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002260 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002261 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002262 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002263 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002264 */
2265#define k_fifo_peek_head(fifo) \
2266 k_queue_peek_head((struct k_queue *) fifo)
2267
2268/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002270 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002271 * Return element from the tail of FIFO queue (without removing it). A usecase
2272 * for this is if elements of the FIFO queue are themselves containers. Then
2273 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002274 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002275 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002276 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002277 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002278 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002279 */
2280#define k_fifo_peek_tail(fifo) \
2281 k_queue_peek_tail((struct k_queue *) fifo)
2282
2283/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002284 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002286 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002288 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002290 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002291 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002294 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002295 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002296 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297
Anas Nashif166f5192018-02-25 08:02:36 -06002298/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002299
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002301 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302};
2303
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002304/**
2305 * @cond INTERNAL_HIDDEN
2306 */
2307
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002308#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002309 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002310 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002311 }
2312
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002313#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2314
Allan Stephensc98da842016-11-11 15:45:03 -05002315/**
2316 * INTERNAL_HIDDEN @endcond
2317 */
2318
2319/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002320 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002321 * @ingroup kernel_apis
2322 * @{
2323 */
2324
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002326 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002328 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002330 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 *
2332 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002333 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002335#define k_lifo_init(lifo) \
2336 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337
2338/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002339 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002341 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2343 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002345 * @note Can be called by ISRs.
2346 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002347 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 *
2350 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002351 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002353#define k_lifo_put(lifo, data) \
2354 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355
2356/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002357 * @brief Add an element to a LIFO queue.
2358 *
2359 * This routine adds a data item to @a lifo. There is an implicit
2360 * memory allocation from the calling thread's resource pool, which is
2361 * automatically freed when the item is removed.
2362 *
2363 * @note Can be called by ISRs.
2364 *
2365 * @param lifo Address of the LIFO.
2366 * @param data Address of the data item.
2367 *
2368 * @retval 0 on success
2369 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002370 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002371 */
2372#define k_lifo_alloc_put(lifo, data) \
2373 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2374
2375/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002376 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002378 * This routine removes a data item from @a lifo in a "last in, first out"
2379 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002381 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2382 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002383 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002385 * or one of the special values K_NO_WAIT and K_FOREVER.
2386 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002387 * @return Address of the data item if successful; NULL if returned
2388 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002389 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002390 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002391#define k_lifo_get(lifo, timeout) \
2392 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002394/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002395 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002396 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002397 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002399 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002402 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002404#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002405 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002406 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002407 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408
Anas Nashif166f5192018-02-25 08:02:36 -06002409/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002410
2411/**
2412 * @cond INTERNAL_HIDDEN
2413 */
Andrew Boief3bee952018-05-02 17:44:39 -07002414#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415
2416struct k_stack {
2417 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002418 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002419
Anas Nashif2f203c22016-12-18 06:57:45 -05002420 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002421 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422};
2423
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002424#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002425 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002426 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002427 .base = stack_buffer, \
2428 .next = stack_buffer, \
2429 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002430 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002431 }
2432
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002433#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2434
Allan Stephensc98da842016-11-11 15:45:03 -05002435/**
2436 * INTERNAL_HIDDEN @endcond
2437 */
2438
2439/**
2440 * @defgroup stack_apis Stack APIs
2441 * @ingroup kernel_apis
2442 * @{
2443 */
2444
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445/**
2446 * @brief Initialize a stack.
2447 *
2448 * This routine initializes a stack object, prior to its first use.
2449 *
2450 * @param stack Address of the stack.
2451 * @param buffer Address of array used to hold stacked values.
2452 * @param num_entries Maximum number of values that can be stacked.
2453 *
2454 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002455 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 */
Andrew Boief3bee952018-05-02 17:44:39 -07002457void k_stack_init(struct k_stack *stack,
2458 u32_t *buffer, unsigned int num_entries);
2459
2460
2461/**
2462 * @brief Initialize a stack.
2463 *
2464 * This routine initializes a stack object, prior to its first use. Internal
2465 * buffers will be allocated from the calling thread's resource pool.
2466 * This memory will be released if k_stack_cleanup() is called, or
2467 * userspace is enabled and the stack object loses all references to it.
2468 *
2469 * @param stack Address of the stack.
2470 * @param num_entries Maximum number of values that can be stacked.
2471 *
2472 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002473 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002474 */
2475
2476__syscall int k_stack_alloc_init(struct k_stack *stack,
2477 unsigned int num_entries);
2478
2479/**
2480 * @brief Release a stack's allocated buffer
2481 *
2482 * If a stack object was given a dynamically allocated buffer via
2483 * k_stack_alloc_init(), this will free it. This function does nothing
2484 * if the buffer wasn't dynamically allocated.
2485 *
2486 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002487 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002488 */
2489void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490
2491/**
2492 * @brief Push an element onto a stack.
2493 *
2494 * This routine adds a 32-bit value @a data to @a stack.
2495 *
2496 * @note Can be called by ISRs.
2497 *
2498 * @param stack Address of the stack.
2499 * @param data Value to push onto the stack.
2500 *
2501 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002502 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 */
Andrew Boiee8734462017-09-29 16:42:07 -07002504__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002505
2506/**
2507 * @brief Pop an element from a stack.
2508 *
2509 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2510 * manner and stores the value in @a data.
2511 *
2512 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2513 *
2514 * @param stack Address of the stack.
2515 * @param data Address of area to hold the value popped from the stack.
2516 * @param timeout Waiting period to obtain a value (in milliseconds),
2517 * or one of the special values K_NO_WAIT and K_FOREVER.
2518 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002519 * @retval 0 Element popped from stack.
2520 * @retval -EBUSY Returned without waiting.
2521 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002522 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 */
Andrew Boiee8734462017-09-29 16:42:07 -07002524__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002531 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @param name Name of the stack.
2534 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002535 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002537#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002538 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002539 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002540 struct k_stack name \
2541 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002542 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002543 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544
Anas Nashif166f5192018-02-25 08:02:36 -06002545/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002546
Allan Stephens6bba9b02016-11-16 14:56:54 -05002547struct k_work;
2548
Allan Stephensc98da842016-11-11 15:45:03 -05002549/**
2550 * @defgroup workqueue_apis Workqueue Thread APIs
2551 * @ingroup kernel_apis
2552 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553 */
2554
Allan Stephens6bba9b02016-11-16 14:56:54 -05002555/**
2556 * @typedef k_work_handler_t
2557 * @brief Work item handler function type.
2558 *
2559 * A work item's handler function is executed by a workqueue's thread
2560 * when the work item is processed by the workqueue.
2561 *
2562 * @param work Address of the work item.
2563 *
2564 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002565 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002566 */
2567typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568
2569/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002570 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002571 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002572
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002574 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002575 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576};
2577
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002579 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580};
2581
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002583 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584 k_work_handler_t handler;
2585 atomic_t flags[1];
2586};
2587
Allan Stephens6bba9b02016-11-16 14:56:54 -05002588struct k_delayed_work {
2589 struct k_work work;
2590 struct _timeout timeout;
2591 struct k_work_q *work_q;
2592};
2593
2594extern struct k_work_q k_sys_work_q;
2595
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002597 * INTERNAL_HIDDEN @endcond
2598 */
2599
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002600#define _K_WORK_INITIALIZER(work_handler) \
2601 { \
2602 ._reserved = NULL, \
2603 .handler = work_handler, \
2604 .flags = { 0 } \
2605 }
2606
2607#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2608
Allan Stephens6bba9b02016-11-16 14:56:54 -05002609/**
2610 * @brief Initialize a statically-defined work item.
2611 *
2612 * This macro can be used to initialize a statically-defined workqueue work
2613 * item, prior to its first use. For example,
2614 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002615 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002616 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002617 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002618 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002619 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002621#define K_WORK_DEFINE(work, work_handler) \
2622 struct k_work work \
2623 __in_section(_k_work, static, work) = \
2624 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002625
2626/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002627 * @brief Initialize a work item.
2628 *
2629 * This routine initializes a workqueue work item, prior to its first use.
2630 *
2631 * @param work Address of work item.
2632 * @param handler Function to invoke each time work item is processed.
2633 *
2634 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002635 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636 */
2637static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2638{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002639 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002640 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641}
2642
2643/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002644 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002645 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002646 * This routine submits work item @a work to be processed by workqueue
2647 * @a work_q. If the work item is already pending in the workqueue's queue
2648 * as a result of an earlier submission, this routine has no effect on the
2649 * work item. If the work item has already been processed, or is currently
2650 * being processed, its work is considered complete and the work item can be
2651 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002652 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002653 * @warning
2654 * A submitted work item must not be modified until it has been processed
2655 * by the workqueue.
2656 *
2657 * @note Can be called by ISRs.
2658 *
2659 * @param work_q Address of workqueue.
2660 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002661 *
2662 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002663 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 */
2665static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2666 struct k_work *work)
2667{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002668 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002669 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670 }
2671}
2672
2673/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002674 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * This routine indicates if work item @a work is pending in a workqueue's
2677 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * @note Can be called by ISRs.
2680 *
2681 * @param work Address of work item.
2682 *
2683 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002684 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002685 */
2686static inline int k_work_pending(struct k_work *work)
2687{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002688 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002689}
2690
2691/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002692 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694 * This routine starts workqueue @a work_q. The workqueue spawns its work
2695 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002696 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002697 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002698 * @param stack Pointer to work queue thread's stack space, as defined by
2699 * K_THREAD_STACK_DEFINE()
2700 * @param stack_size Size of the work queue thread's stack (in bytes), which
2701 * should either be the same constant passed to
2702 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002703 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002704 *
2705 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002706 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 */
Andrew Boie507852a2017-07-25 18:47:07 -07002708extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002709 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002710 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002712/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002714 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715 * This routine initializes a workqueue delayed work item, prior to
2716 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * @param work Address of delayed work item.
2719 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002720 *
2721 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002722 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002724extern void k_delayed_work_init(struct k_delayed_work *work,
2725 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726
2727/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002728 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002730 * This routine schedules work item @a work to be processed by workqueue
2731 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002732 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002733 * Only when the countdown completes is the work item actually submitted to
2734 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002736 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002737 * counting down cancels the existing submission and restarts the
2738 * countdown using the new delay. Note that this behavior is
2739 * inherently subject to race conditions with the pre-existing
2740 * timeouts and work queue, so care must be taken to synchronize such
2741 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002743 * @warning
2744 * A delayed work item must not be modified until it has been processed
2745 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002747 * @note Can be called by ISRs.
2748 *
2749 * @param work_q Address of workqueue.
2750 * @param work Address of delayed work item.
2751 * @param delay Delay before submitting the work item (in milliseconds).
2752 *
2753 * @retval 0 Work item countdown started.
2754 * @retval -EINPROGRESS Work item is already pending.
2755 * @retval -EINVAL Work item is being processed or has completed its work.
2756 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002757 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002759extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2760 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002761 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762
2763/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002764 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002766 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002767 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002768 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002770 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002772 * @param work Address of delayed work item.
2773 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002774 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002775 * @retval -EINPROGRESS Work item is already pending.
2776 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002777 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002779extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002781/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782 * @brief Submit a work item to the system workqueue.
2783 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002784 * This routine submits work item @a work to be processed by the system
2785 * workqueue. If the work item is already pending in the workqueue's queue
2786 * as a result of an earlier submission, this routine has no effect on the
2787 * work item. If the work item has already been processed, or is currently
2788 * being processed, its work is considered complete and the work item can be
2789 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002791 * @warning
2792 * Work items submitted to the system workqueue should avoid using handlers
2793 * that block or yield since this may prevent the system workqueue from
2794 * processing other work items in a timely manner.
2795 *
2796 * @note Can be called by ISRs.
2797 *
2798 * @param work Address of work item.
2799 *
2800 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002801 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802 */
2803static inline void k_work_submit(struct k_work *work)
2804{
2805 k_work_submit_to_queue(&k_sys_work_q, work);
2806}
2807
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002808/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809 * @brief Submit a delayed work item to the system workqueue.
2810 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002811 * This routine schedules work item @a work to be processed by the system
2812 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002813 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002814 * Only when the countdown completes is the work item actually submitted to
2815 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002817 * Submitting a previously submitted delayed work item that is still
2818 * counting down cancels the existing submission and restarts the countdown
2819 * using the new delay. If the work item is currently pending on the
2820 * workqueue's queue because the countdown has completed it is too late to
2821 * resubmit the item, and resubmission fails without impacting the work item.
2822 * If the work item has already been processed, or is currently being processed,
2823 * its work is considered complete and the work item can be resubmitted.
2824 *
2825 * @warning
2826 * Work items submitted to the system workqueue should avoid using handlers
2827 * that block or yield since this may prevent the system workqueue from
2828 * processing other work items in a timely manner.
2829 *
2830 * @note Can be called by ISRs.
2831 *
2832 * @param work Address of delayed work item.
2833 * @param delay Delay before submitting the work item (in milliseconds).
2834 *
2835 * @retval 0 Work item countdown started.
2836 * @retval -EINPROGRESS Work item is already pending.
2837 * @retval -EINVAL Work item is being processed or has completed its work.
2838 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002839 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840 */
2841static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002842 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002844 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845}
2846
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002848 * @brief Get time remaining before a delayed work gets scheduled.
2849 *
2850 * This routine computes the (approximate) time remaining before a
2851 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002852 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002853 *
2854 * @param work Delayed work item.
2855 *
2856 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002857 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002858 */
Kumar Galacc334c72017-04-21 10:55:34 -05002859static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002860{
2861 return _timeout_remaining_get(&work->timeout);
2862}
2863
Anas Nashif166f5192018-02-25 08:02:36 -06002864/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002865/**
Anas Nashifce78d162018-05-24 12:43:11 -05002866 * @defgroup mutex_apis Mutex APIs
2867 * @ingroup kernel_apis
2868 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002869 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870
Anas Nashifce78d162018-05-24 12:43:11 -05002871/**
2872 * Mutex Structure
2873 * @ingroup mutex_apis
2874 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002875struct k_mutex {
2876 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002877 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002878 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002879 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002880 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002881
Anas Nashif2f203c22016-12-18 06:57:45 -05002882 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883};
2884
Anas Nashifce78d162018-05-24 12:43:11 -05002885/**
2886 * @cond INTERNAL_HIDDEN
2887 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002888#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002890 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891 .owner = NULL, \
2892 .lock_count = 0, \
2893 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002894 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002895 }
2896
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002897#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2898
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002899/**
Allan Stephensc98da842016-11-11 15:45:03 -05002900 * INTERNAL_HIDDEN @endcond
2901 */
2902
2903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002908 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002911 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002914 struct k_mutex name \
2915 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002916 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * Upon completion, the mutex is available and does not have an owner.
2924 *
2925 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002926 *
2927 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002928 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002929 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002930__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002931
2932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * This routine locks @a mutex. If the mutex is locked by another thread,
2936 * the calling thread waits until the mutex becomes available or until
2937 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * A thread is permitted to lock a mutex it has already locked. The operation
2940 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @param mutex Address of the mutex.
2943 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002944 * or one of the special values K_NO_WAIT and K_FOREVER.
2945 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002946 * @retval 0 Mutex locked.
2947 * @retval -EBUSY Returned without waiting.
2948 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002949 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002950 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002951__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002952
2953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * This routine unlocks @a mutex. The mutex must already be locked by the
2957 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002958 *
2959 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002961 * thread.
2962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002964 *
2965 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002966 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002967 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002968__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969
Allan Stephensc98da842016-11-11 15:45:03 -05002970/**
Anas Nashif166f5192018-02-25 08:02:36 -06002971 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002972 */
2973
2974/**
2975 * @cond INTERNAL_HIDDEN
2976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002977
2978struct k_sem {
2979 _wait_q_t wait_q;
2980 unsigned int count;
2981 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002982 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983
Anas Nashif2f203c22016-12-18 06:57:45 -05002984 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985};
2986
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002987#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002988 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002989 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002990 .count = initial_count, \
2991 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002992 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002993 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002994 }
2995
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002996#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2997
Allan Stephensc98da842016-11-11 15:45:03 -05002998/**
2999 * INTERNAL_HIDDEN @endcond
3000 */
3001
3002/**
3003 * @defgroup semaphore_apis Semaphore APIs
3004 * @ingroup kernel_apis
3005 * @{
3006 */
3007
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @param sem Address of the semaphore.
3014 * @param initial_count Initial semaphore count.
3015 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003016 *
3017 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003018 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003019 */
Andrew Boie99280232017-09-29 14:17:47 -07003020__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3021 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003022
3023/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3029 *
3030 * @param sem Address of the semaphore.
3031 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032 * or one of the special values K_NO_WAIT and K_FOREVER.
3033 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003034 * @note When porting code from the nanokernel legacy API to the new API, be
3035 * careful with the return value of this function. The return value is the
3036 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3037 * non-zero means failure, while the nano_sem_take family returns 1 for success
3038 * and 0 for failure.
3039 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003040 * @retval 0 Semaphore taken.
3041 * @retval -EBUSY Returned without waiting.
3042 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003043 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003044 */
Andrew Boie99280232017-09-29 14:17:47 -07003045__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003046
3047/**
3048 * @brief Give a semaphore.
3049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * This routine gives @a sem, unless the semaphore is already at its maximum
3051 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003056 *
3057 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003058 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003059 */
Andrew Boie99280232017-09-29 14:17:47 -07003060__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003062/**
3063 * @brief Reset a semaphore's count to zero.
3064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003067 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003068 *
3069 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003070 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003071 */
Andrew Boie990bf162017-10-03 12:36:49 -07003072__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003073
Anas Nashif954d5502018-02-25 08:37:28 -06003074/**
3075 * @internal
3076 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003077static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078{
3079 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080}
3081
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003082/**
3083 * @brief Get a semaphore's count.
3084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003087 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003090 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003091 */
Andrew Boie990bf162017-10-03 12:36:49 -07003092__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003093
Anas Nashif954d5502018-02-25 08:37:28 -06003094/**
3095 * @internal
3096 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003097static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098{
3099 return sem->count;
3100}
3101
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003102/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003103 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003105 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003106 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003107 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003109 * @param name Name of the semaphore.
3110 * @param initial_count Initial semaphore count.
3111 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003112 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003115 struct k_sem name \
3116 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003117 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303118 BUILD_ASSERT(((count_limit) != 0) && \
3119 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003120
Anas Nashif166f5192018-02-25 08:02:36 -06003121/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003122
3123/**
3124 * @defgroup alert_apis Alert APIs
3125 * @ingroup kernel_apis
3126 * @{
3127 */
3128
Allan Stephens5eceb852016-11-16 10:16:30 -05003129/**
3130 * @typedef k_alert_handler_t
3131 * @brief Alert handler function type.
3132 *
3133 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003134 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003135 * and is only invoked if the alert has been initialized with one.
3136 *
3137 * @param alert Address of the alert.
3138 *
3139 * @return 0 if alert has been consumed; non-zero if alert should pend.
3140 */
3141typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003142
Anas Nashif166f5192018-02-25 08:02:36 -06003143/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003144
3145/**
3146 * @cond INTERNAL_HIDDEN
3147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003149#define K_ALERT_DEFAULT NULL
3150#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003151
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003152struct k_alert {
3153 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154 atomic_t send_count;
3155 struct k_work work_item;
3156 struct k_sem sem;
3157
Anas Nashif2f203c22016-12-18 06:57:45 -05003158 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159};
3160
Anas Nashif954d5502018-02-25 08:37:28 -06003161/**
3162 * @internal
3163 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003164extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003166#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003167 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003168 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003169 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003170 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3171 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003172 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173 }
3174
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003175#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3176
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003177/**
Allan Stephensc98da842016-11-11 15:45:03 -05003178 * INTERNAL_HIDDEN @endcond
3179 */
3180
3181/**
3182 * @addtogroup alert_apis
3183 * @{
3184 */
3185
3186/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003187 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003189 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003190 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003191 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003192 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003193 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @param name Name of the alert.
3196 * @param alert_handler Action to take when alert is sent. Specify either
3197 * the address of a function to be invoked by the system workqueue
3198 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3199 * K_ALERT_DEFAULT (which causes the alert to pend).
3200 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003201 *
3202 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003203 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003204#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003205 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003206 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003207 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003208 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003210/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * @param alert Address of the alert.
3216 * @param handler Action to take when alert is sent. Specify either the address
3217 * of a function to be invoked by the system workqueue thread,
3218 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3219 * K_ALERT_DEFAULT (which causes the alert to pend).
3220 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003221 *
3222 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003223 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003224 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003225extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3226 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003227
3228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3234 *
3235 * @param alert Address of the alert.
3236 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003237 * or one of the special values K_NO_WAIT and K_FOREVER.
3238 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003239 * @retval 0 Alert received.
3240 * @retval -EBUSY Returned without waiting.
3241 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003242 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003243 */
Andrew Boie310e9872017-09-29 04:41:15 -07003244__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003245
3246/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * This routine signals @a alert. The action specified for @a alert will
3250 * be taken, which may trigger the execution of an alert handler function
3251 * and/or cause the alert to pend (assuming the alert has not reached its
3252 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @note Can be called by ISRs.
3255 *
3256 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 *
3258 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003259 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003260 */
Andrew Boie310e9872017-09-29 04:41:15 -07003261__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262
3263/**
Anas Nashif166f5192018-02-25 08:02:36 -06003264 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265 */
3266
Allan Stephensc98da842016-11-11 15:45:03 -05003267/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003268 * @defgroup msgq_apis Message Queue APIs
3269 * @ingroup kernel_apis
3270 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003271 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003273/**
3274 * @brief Message Queue Structure
3275 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276struct k_msgq {
3277 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003278 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003279 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003280 char *buffer_start;
3281 char *buffer_end;
3282 char *read_ptr;
3283 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003284 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285
Anas Nashif2f203c22016-12-18 06:57:45 -05003286 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003287 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003289/**
3290 * @cond INTERNAL_HIDDEN
3291 */
3292
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003294#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003295 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003296 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003297 .max_msgs = q_max_msgs, \
3298 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003300 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 .read_ptr = q_buffer, \
3302 .write_ptr = q_buffer, \
3303 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003304 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003306#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003307/**
3308 * INTERNAL_HIDDEN @endcond
3309 */
3310
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003311
Andrew Boie0fe789f2018-04-12 18:35:56 -07003312#define K_MSGQ_FLAG_ALLOC BIT(0)
3313
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003314/**
3315 * @brief Message Queue Attributes
3316 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303317struct k_msgq_attrs {
3318 size_t msg_size;
3319 u32_t max_msgs;
3320 u32_t used_msgs;
3321};
3322
Allan Stephensc98da842016-11-11 15:45:03 -05003323
3324/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003325 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3328 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003329 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3330 * message is similarly aligned to this boundary, @a q_msg_size must also be
3331 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003332 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003333 * The message queue can be accessed outside the module where it is defined
3334 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003335 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003336 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * @param q_name Name of the message queue.
3339 * @param q_msg_size Message size (in bytes).
3340 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003341 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003342 *
3343 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003344 */
3345#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003346 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003347 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003348 struct k_msgq q_name \
3349 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003350 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003351 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352
Peter Mitsisd7a37502016-10-13 11:37:40 -04003353/**
3354 * @brief Initialize a message queue.
3355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * This routine initializes a message queue object, prior to its first use.
3357 *
Allan Stephensda827222016-11-09 14:23:58 -06003358 * The message queue's ring buffer must contain space for @a max_msgs messages,
3359 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3360 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3361 * that each message is similarly aligned to this boundary, @a q_msg_size
3362 * must also be a multiple of N.
3363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * @param q Address of the message queue.
3365 * @param buffer Pointer to ring buffer that holds queued messages.
3366 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003367 * @param max_msgs Maximum number of messages that can be queued.
3368 *
3369 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003370 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003371 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003372void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3373 u32_t max_msgs);
3374
3375/**
3376 * @brief Initialize a message queue.
3377 *
3378 * This routine initializes a message queue object, prior to its first use,
3379 * allocating its internal ring buffer from the calling thread's resource
3380 * pool.
3381 *
3382 * Memory allocated for the ring buffer can be released by calling
3383 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3384 * all of its references.
3385 *
3386 * @param q Address of the message queue.
3387 * @param msg_size Message size (in bytes).
3388 * @param max_msgs Maximum number of messages that can be queued.
3389 *
3390 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3391 * thread's resource pool, or -EINVAL if the size parameters cause
3392 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003393 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003394 */
3395__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3396 u32_t max_msgs);
3397
3398
3399void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003400
3401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003405 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003406 * @note Can be called by ISRs.
3407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @param q Address of the message queue.
3409 * @param data Pointer to the message.
3410 * @param timeout Waiting period to add the message (in milliseconds),
3411 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003412 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003413 * @retval 0 Message sent.
3414 * @retval -ENOMSG Returned without waiting or queue purged.
3415 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003416 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003417 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003418__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003419
3420/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * This routine receives a message from message queue @a q in a "first in,
3424 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003425 *
Allan Stephensc98da842016-11-11 15:45:03 -05003426 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * @param q Address of the message queue.
3429 * @param data Address of area to hold the received message.
3430 * @param timeout Waiting period to receive the message (in milliseconds),
3431 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003432 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003433 * @retval 0 Message received.
3434 * @retval -ENOMSG Returned without waiting.
3435 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003436 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003437 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003438__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003439
3440/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003441 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003442 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003443 * This routine discards all unreceived messages in a message queue's ring
3444 * buffer. Any threads that are blocked waiting to send a message to the
3445 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003447 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003448 *
3449 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003450 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003451 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003452__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003453
Peter Mitsis67be2492016-10-07 11:44:34 -04003454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * This routine returns the number of unused entries in a message queue's
3458 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @param q Address of the message queue.
3461 *
3462 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003463 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003464 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003465__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3466
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303467/**
3468 * @brief Get basic attributes of a message queue.
3469 *
3470 * This routine fetches basic attributes of message queue into attr argument.
3471 *
3472 * @param q Address of the message queue.
3473 * @param attrs pointer to message queue attribute structure.
3474 *
3475 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003476 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303477 */
3478__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3479
3480
Andrew Boie82edb6e2017-10-02 10:53:06 -07003481static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003482{
3483 return q->max_msgs - q->used_msgs;
3484}
3485
Peter Mitsisd7a37502016-10-13 11:37:40 -04003486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * @param q Address of the message queue.
3492 *
3493 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003494 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003495 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003496__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3497
3498static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499{
3500 return q->used_msgs;
3501}
3502
Anas Nashif166f5192018-02-25 08:02:36 -06003503/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003504
3505/**
3506 * @defgroup mem_pool_apis Memory Pool APIs
3507 * @ingroup kernel_apis
3508 * @{
3509 */
3510
Andy Ross73cb9582017-05-09 10:42:39 -07003511/* Note on sizing: the use of a 20 bit field for block means that,
3512 * assuming a reasonable minimum block size of 16 bytes, we're limited
3513 * to 16M of memory managed by a single pool. Long term it would be
3514 * good to move to a variable bit size based on configuration.
3515 */
3516struct k_mem_block_id {
3517 u32_t pool : 8;
3518 u32_t level : 4;
3519 u32_t block : 20;
3520};
3521
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003522struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003523 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003524 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003525};
3526
Anas Nashif166f5192018-02-25 08:02:36 -06003527/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003528
3529/**
3530 * @defgroup mailbox_apis Mailbox APIs
3531 * @ingroup kernel_apis
3532 * @{
3533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003534
3535struct k_mbox_msg {
3536 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003537 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003539 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003541 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003542 /** sender's message data buffer */
3543 void *tx_data;
3544 /** internal use only - needed for legacy API support */
3545 void *_rx_data;
3546 /** message data block descriptor */
3547 struct k_mem_block tx_block;
3548 /** source thread id */
3549 k_tid_t rx_source_thread;
3550 /** target thread id */
3551 k_tid_t tx_target_thread;
3552 /** internal use only - thread waiting on send (may be a dummy) */
3553 k_tid_t _syncing_thread;
3554#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3555 /** internal use only - semaphore used during asynchronous send */
3556 struct k_sem *_async_sem;
3557#endif
3558};
3559
3560struct k_mbox {
3561 _wait_q_t tx_msg_queue;
3562 _wait_q_t rx_msg_queue;
3563
Anas Nashif2f203c22016-12-18 06:57:45 -05003564 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003565};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003566/**
3567 * @cond INTERNAL_HIDDEN
3568 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003570#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003571 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003572 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3573 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003574 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003575 }
3576
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003577#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3578
Peter Mitsis12092702016-10-14 12:57:23 -04003579/**
Allan Stephensc98da842016-11-11 15:45:03 -05003580 * INTERNAL_HIDDEN @endcond
3581 */
3582
3583/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003584 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003586 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003587 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003588 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003590 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003591 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003592 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003594 struct k_mbox name \
3595 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003596 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003597
Peter Mitsis12092702016-10-14 12:57:23 -04003598/**
3599 * @brief Initialize a mailbox.
3600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * This routine initializes a mailbox object, prior to its first use.
3602 *
3603 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003604 *
3605 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003606 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003607 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003608extern void k_mbox_init(struct k_mbox *mbox);
3609
Peter Mitsis12092702016-10-14 12:57:23 -04003610/**
3611 * @brief Send a mailbox message in a synchronous manner.
3612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * This routine sends a message to @a mbox and waits for a receiver to both
3614 * receive and process it. The message data may be in a buffer, in a memory
3615 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * @param mbox Address of the mailbox.
3618 * @param tx_msg Address of the transmit message descriptor.
3619 * @param timeout Waiting period for the message to be received (in
3620 * milliseconds), or one of the special values K_NO_WAIT
3621 * and K_FOREVER. Once the message has been received,
3622 * this routine waits as long as necessary for the message
3623 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003624 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003625 * @retval 0 Message sent.
3626 * @retval -ENOMSG Returned without waiting.
3627 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003628 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003629 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003630extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003631 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003632
Peter Mitsis12092702016-10-14 12:57:23 -04003633/**
3634 * @brief Send a mailbox message in an asynchronous manner.
3635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * This routine sends a message to @a mbox without waiting for a receiver
3637 * to process it. The message data may be in a buffer, in a memory pool block,
3638 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3639 * will be given when the message has been both received and completely
3640 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003642 * @param mbox Address of the mailbox.
3643 * @param tx_msg Address of the transmit message descriptor.
3644 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003645 *
3646 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003647 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003648 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003649extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003650 struct k_sem *sem);
3651
Peter Mitsis12092702016-10-14 12:57:23 -04003652/**
3653 * @brief Receive a mailbox message.
3654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * This routine receives a message from @a mbox, then optionally retrieves
3656 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * @param mbox Address of the mailbox.
3659 * @param rx_msg Address of the receive message descriptor.
3660 * @param buffer Address of the buffer to receive data, or NULL to defer data
3661 * retrieval and message disposal until later.
3662 * @param timeout Waiting period for a message to be received (in
3663 * milliseconds), or one of the special values K_NO_WAIT
3664 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003665 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003666 * @retval 0 Message received.
3667 * @retval -ENOMSG Returned without waiting.
3668 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003669 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003670 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003671extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003672 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003673
3674/**
3675 * @brief Retrieve mailbox message data into a buffer.
3676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003677 * This routine completes the processing of a received message by retrieving
3678 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003679 *
3680 * Alternatively, this routine can be used to dispose of a received message
3681 * without retrieving its data.
3682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * @param rx_msg Address of the receive message descriptor.
3684 * @param buffer Address of the buffer to receive data, or NULL to discard
3685 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003686 *
3687 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003688 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003689 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003690extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003691
3692/**
3693 * @brief Retrieve mailbox message data into a memory pool block.
3694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003695 * This routine completes the processing of a received message by retrieving
3696 * its data into a memory pool block, then disposing of the message.
3697 * The memory pool block that results from successful retrieval must be
3698 * returned to the pool once the data has been processed, even in cases
3699 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003700 *
3701 * Alternatively, this routine can be used to dispose of a received message
3702 * without retrieving its data. In this case there is no need to return a
3703 * memory pool block to the pool.
3704 *
3705 * This routine allocates a new memory pool block for the data only if the
3706 * data is not already in one. If a new block cannot be allocated, the routine
3707 * returns a failure code and the received message is left unchanged. This
3708 * permits the caller to reattempt data retrieval at a later time or to dispose
3709 * of the received message without retrieving its data.
3710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003711 * @param rx_msg Address of a receive message descriptor.
3712 * @param pool Address of memory pool, or NULL to discard data.
3713 * @param block Address of the area to hold memory pool block info.
3714 * @param timeout Waiting period to wait for a memory pool block (in
3715 * milliseconds), or one of the special values K_NO_WAIT
3716 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003717 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003718 * @retval 0 Data retrieved.
3719 * @retval -ENOMEM Returned without waiting.
3720 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003721 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003722 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003723extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003724 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003725 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003726
Anas Nashif166f5192018-02-25 08:02:36 -06003727/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003728
3729/**
Anas Nashifce78d162018-05-24 12:43:11 -05003730 * @defgroup pipe_apis Pipe APIs
3731 * @ingroup kernel_apis
3732 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003733 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003734
Anas Nashifce78d162018-05-24 12:43:11 -05003735/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003736struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003737 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3738 size_t size; /**< Buffer size */
3739 size_t bytes_used; /**< # bytes used in buffer */
3740 size_t read_index; /**< Where in buffer to read from */
3741 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003742
3743 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003744 _wait_q_t readers; /**< Reader wait queue */
3745 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746 } wait_q;
3747
Anas Nashif2f203c22016-12-18 06:57:45 -05003748 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003749 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750};
3751
Anas Nashifce78d162018-05-24 12:43:11 -05003752/**
3753 * @cond INTERNAL_HIDDEN
3754 */
3755#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3756
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003757#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003758 { \
3759 .buffer = pipe_buffer, \
3760 .size = pipe_buffer_size, \
3761 .bytes_used = 0, \
3762 .read_index = 0, \
3763 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003764 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3765 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003766 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003767 }
3768
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003769#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3770
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003771/**
Allan Stephensc98da842016-11-11 15:45:03 -05003772 * INTERNAL_HIDDEN @endcond
3773 */
3774
3775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003776 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003778 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003779 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003780 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003782 * @param name Name of the pipe.
3783 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3784 * or zero if no ring buffer is used.
3785 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003786 *
3787 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003788 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003789#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3790 static unsigned char __kernel_noinit __aligned(pipe_align) \
3791 _k_pipe_buf_##name[pipe_buffer_size]; \
3792 struct k_pipe name \
3793 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003794 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003795
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003797 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003799 * This routine initializes a pipe object, prior to its first use.
3800 *
3801 * @param pipe Address of the pipe.
3802 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3803 * is used.
3804 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3805 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003806 *
3807 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003808 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003810void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3811
3812/**
3813 * @brief Release a pipe's allocated buffer
3814 *
3815 * If a pipe object was given a dynamically allocated buffer via
3816 * k_pipe_alloc_init(), this will free it. This function does nothing
3817 * if the buffer wasn't dynamically allocated.
3818 *
3819 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003820 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003821 */
3822void k_pipe_cleanup(struct k_pipe *pipe);
3823
3824/**
3825 * @brief Initialize a pipe and allocate a buffer for it
3826 *
3827 * Storage for the buffer region will be allocated from the calling thread's
3828 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3829 * or userspace is enabled and the pipe object loses all references to it.
3830 *
3831 * This function should only be called on uninitialized pipe objects.
3832 *
3833 * @param pipe Address of the pipe.
3834 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3835 * buffer is used.
3836 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003837 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003838 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003839 */
3840__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841
3842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003843 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003847 * @param pipe Address of the pipe.
3848 * @param data Address of data to write.
3849 * @param bytes_to_write Size of data (in bytes).
3850 * @param bytes_written Address of area to hold the number of bytes written.
3851 * @param min_xfer Minimum number of bytes to write.
3852 * @param timeout Waiting period to wait for the data to be written (in
3853 * milliseconds), or one of the special values K_NO_WAIT
3854 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003856 * @retval 0 At least @a min_xfer bytes of data were written.
3857 * @retval -EIO Returned without waiting; zero data bytes were written.
3858 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003859 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003860 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003861 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003862__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3863 size_t bytes_to_write, size_t *bytes_written,
3864 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003865
3866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003867 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003869 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003871 * @param pipe Address of the pipe.
3872 * @param data Address to place the data read from pipe.
3873 * @param bytes_to_read Maximum number of data bytes to read.
3874 * @param bytes_read Address of area to hold the number of bytes read.
3875 * @param min_xfer Minimum number of data bytes to read.
3876 * @param timeout Waiting period to wait for the data to be read (in
3877 * milliseconds), or one of the special values K_NO_WAIT
3878 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003879 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003880 * @retval 0 At least @a min_xfer bytes of data were read.
3881 * @retval -EIO Returned without waiting; zero data bytes were read.
3882 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003883 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003884 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003885 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003886__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3887 size_t bytes_to_read, size_t *bytes_read,
3888 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003889
3890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003891 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003893 * This routine writes the data contained in a memory block to @a pipe.
3894 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003895 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003897 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898 * @param block Memory block containing data to send
3899 * @param size Number of data bytes in memory block to send
3900 * @param sem Semaphore to signal upon completion (else NULL)
3901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003902 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003903 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003904 */
3905extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3906 size_t size, struct k_sem *sem);
3907
Anas Nashif166f5192018-02-25 08:02:36 -06003908/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003909
Allan Stephensc98da842016-11-11 15:45:03 -05003910/**
3911 * @cond INTERNAL_HIDDEN
3912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003913
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003914struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003915 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003916 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003917 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003918 char *buffer;
3919 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003920 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003921
Anas Nashif2f203c22016-12-18 06:57:45 -05003922 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003923};
3924
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003925#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003926 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003927 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003928 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003929 .num_blocks = slab_num_blocks, \
3930 .block_size = slab_block_size, \
3931 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003932 .free_list = NULL, \
3933 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003934 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003935 }
3936
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003937#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3938
3939
Peter Mitsis578f9112016-10-07 13:50:31 -04003940/**
Allan Stephensc98da842016-11-11 15:45:03 -05003941 * INTERNAL_HIDDEN @endcond
3942 */
3943
3944/**
3945 * @defgroup mem_slab_apis Memory Slab APIs
3946 * @ingroup kernel_apis
3947 * @{
3948 */
3949
3950/**
Allan Stephensda827222016-11-09 14:23:58 -06003951 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003952 *
Allan Stephensda827222016-11-09 14:23:58 -06003953 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003954 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003955 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3956 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003957 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003958 *
Allan Stephensda827222016-11-09 14:23:58 -06003959 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003961 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003962 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * @param name Name of the memory slab.
3965 * @param slab_block_size Size of each memory block (in bytes).
3966 * @param slab_num_blocks Number memory blocks.
3967 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003968 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003969 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003970#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3971 char __noinit __aligned(slab_align) \
3972 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3973 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003974 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003975 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003976 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003977
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003978/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003979 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003981 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982 *
Allan Stephensda827222016-11-09 14:23:58 -06003983 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3984 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3985 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3986 * To ensure that each memory block is similarly aligned to this boundary,
3987 * @a slab_block_size must also be a multiple of N.
3988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003989 * @param slab Address of the memory slab.
3990 * @param buffer Pointer to buffer used for the memory blocks.
3991 * @param block_size Size of each memory block (in bytes).
3992 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003993 *
3994 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003995 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003996 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003997extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003998 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003999
4000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004003 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004005 * @param slab Address of the memory slab.
4006 * @param mem Pointer to block address area.
4007 * @param timeout Maximum time to wait for operation to complete
4008 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4009 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004010 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004011 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004012 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004013 * @retval -ENOMEM Returned without waiting.
4014 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004015 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004016 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004017extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004018 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004019
4020/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * This routine releases a previously allocated memory block back to its
4024 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004026 * @param slab Address of the memory slab.
4027 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004028 *
4029 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004030 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004031 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004032extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004033
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004034/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004035 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004037 * This routine gets the number of memory blocks that are currently
4038 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004040 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004042 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004043 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004044 */
Kumar Galacc334c72017-04-21 10:55:34 -05004045static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004046{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004047 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004048}
4049
Peter Mitsisc001aa82016-10-13 13:53:37 -04004050/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004051 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004053 * This routine gets the number of memory blocks that are currently
4054 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004056 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004058 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004059 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004060 */
Kumar Galacc334c72017-04-21 10:55:34 -05004061static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004062{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004063 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004064}
4065
Anas Nashif166f5192018-02-25 08:02:36 -06004066/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004067
4068/**
4069 * @cond INTERNAL_HIDDEN
4070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004071
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004072struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004073 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004074 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004075};
4076
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004077/**
Allan Stephensc98da842016-11-11 15:45:03 -05004078 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004079 */
4080
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004081/**
Allan Stephensc98da842016-11-11 15:45:03 -05004082 * @addtogroup mem_pool_apis
4083 * @{
4084 */
4085
4086/**
4087 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004089 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4090 * long. The memory pool allows blocks to be repeatedly partitioned into
4091 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004092 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004093 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004094 * If the pool is to be accessed outside the module where it is defined, it
4095 * can be declared via
4096 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004097 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004099 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004100 * @param minsz Size of the smallest blocks in the pool (in bytes).
4101 * @param maxsz Size of the largest blocks in the pool (in bytes).
4102 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004103 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004104 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004105 */
Andy Ross73cb9582017-05-09 10:42:39 -07004106#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4107 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4108 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004109 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004110 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004111 .base = { \
4112 .buf = _mpool_buf_##name, \
4113 .max_sz = maxsz, \
4114 .n_max = nmax, \
4115 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4116 .levels = _mpool_lvls_##name, \
4117 .flags = SYS_MEM_POOL_KERNEL \
4118 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004119 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004120
Peter Mitsis937042c2016-10-13 13:18:26 -04004121/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004122 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004124 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004126 * @param pool Address of the memory pool.
4127 * @param block Pointer to block descriptor for the allocated memory.
4128 * @param size Amount of memory to allocate (in bytes).
4129 * @param timeout Maximum time to wait for operation to complete
4130 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4131 * or K_FOREVER to wait as long as necessary.
4132 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004133 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004134 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004135 * @retval -ENOMEM Returned without waiting.
4136 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004137 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004138 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004139extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004140 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004141
4142/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004143 * @brief Allocate memory from a memory pool with malloc() semantics
4144 *
4145 * Such memory must be released using k_free().
4146 *
4147 * @param pool Address of the memory pool.
4148 * @param size Amount of memory to allocate (in bytes).
4149 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004150 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004151 */
4152extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4153
4154/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004155 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004157 * This routine releases a previously allocated memory block back to its
4158 * memory pool.
4159 *
4160 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004161 *
4162 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004163 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004164 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004165extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004166
4167/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004168 * @brief Free memory allocated from a memory pool.
4169 *
4170 * This routine releases a previously allocated memory block back to its
4171 * memory pool
4172 *
4173 * @param id Memory block identifier.
4174 *
4175 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004176 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004177 */
4178extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4179
4180/**
Anas Nashif166f5192018-02-25 08:02:36 -06004181 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004182 */
4183
4184/**
4185 * @defgroup heap_apis Heap Memory Pool APIs
4186 * @ingroup kernel_apis
4187 * @{
4188 */
4189
4190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004191 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004193 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004194 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004196 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004198 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004199 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004200 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004201extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004202
4203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004204 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004205 *
4206 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004207 * returned must have been allocated from the heap memory pool or
4208 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004209 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004210 * If @a ptr is NULL, no operation is performed.
4211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004212 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004213 *
4214 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004215 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004216 */
4217extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004218
Allan Stephensc98da842016-11-11 15:45:03 -05004219/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004220 * @brief Allocate memory from heap, array style
4221 *
4222 * This routine provides traditional calloc() semantics. Memory is
4223 * allocated from the heap memory pool and zeroed.
4224 *
4225 * @param nmemb Number of elements in the requested array
4226 * @param size Size of each array element (in bytes).
4227 *
4228 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004229 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004230 */
4231extern void *k_calloc(size_t nmemb, size_t size);
4232
Anas Nashif166f5192018-02-25 08:02:36 -06004233/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004234
Benjamin Walshacc68c12017-01-29 18:57:45 -05004235/* polling API - PRIVATE */
4236
Benjamin Walshb0179862017-02-02 16:39:57 -05004237#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004238#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004239#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004240#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004241#endif
4242
Benjamin Walshacc68c12017-01-29 18:57:45 -05004243/* private - implementation data created as needed, per-type */
4244struct _poller {
4245 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004246 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004247};
4248
4249/* private - types bit positions */
4250enum _poll_types_bits {
4251 /* can be used to ignore an event */
4252 _POLL_TYPE_IGNORE,
4253
4254 /* to be signaled by k_poll_signal() */
4255 _POLL_TYPE_SIGNAL,
4256
4257 /* semaphore availability */
4258 _POLL_TYPE_SEM_AVAILABLE,
4259
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004260 /* queue/fifo/lifo data availability */
4261 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004262
4263 _POLL_NUM_TYPES
4264};
4265
4266#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4267
4268/* private - states bit positions */
4269enum _poll_states_bits {
4270 /* default state when creating event */
4271 _POLL_STATE_NOT_READY,
4272
Benjamin Walshacc68c12017-01-29 18:57:45 -05004273 /* signaled by k_poll_signal() */
4274 _POLL_STATE_SIGNALED,
4275
4276 /* semaphore is available */
4277 _POLL_STATE_SEM_AVAILABLE,
4278
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004279 /* data is available to read on queue/fifo/lifo */
4280 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004281
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004282 /* queue/fifo/lifo wait was cancelled */
4283 _POLL_STATE_CANCELLED,
4284
Benjamin Walshacc68c12017-01-29 18:57:45 -05004285 _POLL_NUM_STATES
4286};
4287
4288#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4289
4290#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004291 (32 - (0 \
4292 + 8 /* tag */ \
4293 + _POLL_NUM_TYPES \
4294 + _POLL_NUM_STATES \
4295 + 1 /* modes */ \
4296 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297
Benjamin Walshacc68c12017-01-29 18:57:45 -05004298/* end of polling API - PRIVATE */
4299
4300
4301/**
4302 * @defgroup poll_apis Async polling APIs
4303 * @ingroup kernel_apis
4304 * @{
4305 */
4306
4307/* Public polling API */
4308
4309/* public - values for k_poll_event.type bitfield */
4310#define K_POLL_TYPE_IGNORE 0
4311#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4312#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004313#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4314#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004315
4316/* public - polling modes */
4317enum k_poll_modes {
4318 /* polling thread does not take ownership of objects when available */
4319 K_POLL_MODE_NOTIFY_ONLY = 0,
4320
4321 K_POLL_NUM_MODES
4322};
4323
4324/* public - values for k_poll_event.state bitfield */
4325#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004326#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4327#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004328#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4329#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004330#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004331
4332/* public - poll signal object */
4333struct k_poll_signal {
4334 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004335 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004336
4337 /*
4338 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4339 * user resets it to 0.
4340 */
4341 unsigned int signaled;
4342
4343 /* custom result value passed to k_poll_signal() if needed */
4344 int result;
4345};
4346
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004347#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004348 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004349 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004350 .signaled = 0, \
4351 .result = 0, \
4352 }
4353
4354struct k_poll_event {
4355 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004356 sys_dnode_t _node;
4357
4358 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004359 struct _poller *poller;
4360
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004361 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004362 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004363
Benjamin Walshacc68c12017-01-29 18:57:45 -05004364 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004365 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004366
4367 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004368 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004369
4370 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004371 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004372
4373 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004374 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004375
4376 /* per-type data */
4377 union {
4378 void *obj;
4379 struct k_poll_signal *signal;
4380 struct k_sem *sem;
4381 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004382 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004383 };
4384};
4385
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004386#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004387 { \
4388 .poller = NULL, \
4389 .type = event_type, \
4390 .state = K_POLL_STATE_NOT_READY, \
4391 .mode = event_mode, \
4392 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004393 { .obj = event_obj }, \
4394 }
4395
4396#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4397 event_tag) \
4398 { \
4399 .type = event_type, \
4400 .tag = event_tag, \
4401 .state = K_POLL_STATE_NOT_READY, \
4402 .mode = event_mode, \
4403 .unused = 0, \
4404 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004405 }
4406
4407/**
4408 * @brief Initialize one struct k_poll_event instance
4409 *
4410 * After this routine is called on a poll event, the event it ready to be
4411 * placed in an event array to be passed to k_poll().
4412 *
4413 * @param event The event to initialize.
4414 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4415 * values. Only values that apply to the same object being polled
4416 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4417 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004418 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004419 * @param obj Kernel object or poll signal.
4420 *
4421 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004422 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004423 */
4424
Kumar Galacc334c72017-04-21 10:55:34 -05004425extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004426 int mode, void *obj);
4427
4428/**
4429 * @brief Wait for one or many of multiple poll events to occur
4430 *
4431 * This routine allows a thread to wait concurrently for one or many of
4432 * multiple poll events to have occurred. Such events can be a kernel object
4433 * being available, like a semaphore, or a poll signal event.
4434 *
4435 * When an event notifies that a kernel object is available, the kernel object
4436 * is not "given" to the thread calling k_poll(): it merely signals the fact
4437 * that the object was available when the k_poll() call was in effect. Also,
4438 * all threads trying to acquire an object the regular way, i.e. by pending on
4439 * the object, have precedence over the thread polling on the object. This
4440 * means that the polling thread will never get the poll event on an object
4441 * until the object becomes available and its pend queue is empty. For this
4442 * reason, the k_poll() call is more effective when the objects being polled
4443 * only have one thread, the polling thread, trying to acquire them.
4444 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004445 * When k_poll() returns 0, the caller should loop on all the events that were
4446 * passed to k_poll() and check the state field for the values that were
4447 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004448 *
4449 * Before being reused for another call to k_poll(), the user has to reset the
4450 * state field to K_POLL_STATE_NOT_READY.
4451 *
Andrew Boie3772f772018-05-07 16:52:57 -07004452 * When called from user mode, a temporary memory allocation is required from
4453 * the caller's resource pool.
4454 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004455 * @param events An array of pointers to events to be polled for.
4456 * @param num_events The number of events in the array.
4457 * @param timeout Waiting period for an event to be ready (in milliseconds),
4458 * or one of the special values K_NO_WAIT and K_FOREVER.
4459 *
4460 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004461 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004462 * @retval -EINTR Polling has been interrupted, e.g. with
4463 * k_queue_cancel_wait(). All output events are still set and valid,
4464 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4465 * words, -EINTR status means that at least one of output events is
4466 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004467 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4468 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004469 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004470 */
4471
Andrew Boie3772f772018-05-07 16:52:57 -07004472__syscall int k_poll(struct k_poll_event *events, int num_events,
4473 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004474
4475/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004476 * @brief Initialize a poll signal object.
4477 *
4478 * Ready a poll signal object to be signaled via k_poll_signal().
4479 *
4480 * @param signal A poll signal.
4481 *
4482 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004483 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004484 */
4485
Andrew Boie3772f772018-05-07 16:52:57 -07004486__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4487
4488/*
4489 * @brief Reset a poll signal object's state to unsignaled.
4490 *
4491 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004492 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004493 */
4494__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4495
4496static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4497{
4498 signal->signaled = 0;
4499}
4500
4501/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004502 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004503 *
4504 * @param signal A poll signal object
4505 * @param signaled An integer buffer which will be written nonzero if the
4506 * object was signaled
4507 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004508 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004509 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004510 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004511 */
4512__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4513 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004514
4515/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004516 * @brief Signal a poll signal object.
4517 *
4518 * This routine makes ready a poll signal, which is basically a poll event of
4519 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4520 * made ready to run. A @a result value can be specified.
4521 *
4522 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004523 * k_poll_signal(), stays set until the user sets it back to 0 with
4524 * k_poll_signal_reset(). It thus has to be reset by the user before being
4525 * passed again to k_poll() or k_poll() will consider it being signaled, and
4526 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004527 *
4528 * @param signal A poll signal.
4529 * @param result The value to store in the result field of the signal.
4530 *
4531 * @retval 0 The signal was delivered successfully.
4532 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004533 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004534 */
4535
Andrew Boie3772f772018-05-07 16:52:57 -07004536__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004537
Anas Nashif954d5502018-02-25 08:37:28 -06004538/**
4539 * @internal
4540 */
Andy Ross8606fab2018-03-26 10:54:40 -07004541extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004542
Anas Nashif166f5192018-02-25 08:02:36 -06004543/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004544
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004545/**
4546 * @brief Make the CPU idle.
4547 *
4548 * This function makes the CPU idle until an event wakes it up.
4549 *
4550 * In a regular system, the idle thread should be the only thread responsible
4551 * for making the CPU idle and triggering any type of power management.
4552 * However, in some more constrained systems, such as a single-threaded system,
4553 * the only thread would be responsible for this if needed.
4554 *
4555 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004556 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004557 */
4558extern void k_cpu_idle(void);
4559
4560/**
4561 * @brief Make the CPU idle in an atomic fashion.
4562 *
4563 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4564 * must be done atomically before making the CPU idle.
4565 *
4566 * @param key Interrupt locking key obtained from irq_lock().
4567 *
4568 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004569 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004570 */
4571extern void k_cpu_atomic_idle(unsigned int key);
4572
Anas Nashif954d5502018-02-25 08:37:28 -06004573
4574/**
4575 * @internal
4576 */
Kumar Galacc334c72017-04-21 10:55:34 -05004577extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004578
Andrew Boiecdb94d62017-04-18 15:22:05 -07004579#ifdef _ARCH_EXCEPT
4580/* This archtecture has direct support for triggering a CPU exception */
4581#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4582#else
4583
Andrew Boiecdb94d62017-04-18 15:22:05 -07004584/* NOTE: This is the implementation for arches that do not implement
4585 * _ARCH_EXCEPT() to generate a real CPU exception.
4586 *
4587 * We won't have a real exception frame to determine the PC value when
4588 * the oops occurred, so print file and line number before we jump into
4589 * the fatal error handler.
4590 */
4591#define _k_except_reason(reason) do { \
4592 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4593 _NanoFatalErrorHandler(reason, &_default_esf); \
4594 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004595 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004596
4597#endif /* _ARCH__EXCEPT */
4598
4599/**
4600 * @brief Fatally terminate a thread
4601 *
4602 * This should be called when a thread has encountered an unrecoverable
4603 * runtime condition and needs to terminate. What this ultimately
4604 * means is determined by the _fatal_error_handler() implementation, which
4605 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4606 *
4607 * If this is called from ISR context, the default system fatal error handler
4608 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004609 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004610 */
4611#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4612
4613/**
4614 * @brief Fatally terminate the system
4615 *
4616 * This should be called when the Zephyr kernel has encountered an
4617 * unrecoverable runtime condition and needs to terminate. What this ultimately
4618 * means is determined by the _fatal_error_handler() implementation, which
4619 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004620 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004621 */
4622#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4623
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004624/*
4625 * private APIs that are utilized by one or more public APIs
4626 */
4627
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004628#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004629/**
4630 * @internal
4631 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004632extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004633#else
Anas Nashif954d5502018-02-25 08:37:28 -06004634/**
4635 * @internal
4636 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004637#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004638#endif
4639
Anas Nashif954d5502018-02-25 08:37:28 -06004640/**
4641 * @internal
4642 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004643extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004644/**
4645 * @internal
4646 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004647extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004648
Andrew Boiedc5d9352017-06-02 12:56:47 -07004649/* arch/cpu.h may declare an architecture or platform-specific macro
4650 * for properly declaring stacks, compatible with MMU/MPU constraints if
4651 * enabled
4652 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004653
4654/**
4655 * @brief Obtain an extern reference to a stack
4656 *
4657 * This macro properly brings the symbol of a thread stack declared
4658 * elsewhere into scope.
4659 *
4660 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004661 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004662 */
4663#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4664
Andrew Boiedc5d9352017-06-02 12:56:47 -07004665#ifdef _ARCH_THREAD_STACK_DEFINE
4666#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4667#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4668 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304669#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004670#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4671#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004672static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004673{
4674 return _ARCH_THREAD_STACK_BUFFER(sym);
4675}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004676#else
4677/**
4678 * @brief Declare a toplevel thread stack memory region
4679 *
4680 * This declares a region of memory suitable for use as a thread's stack.
4681 *
4682 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4683 * 'noinit' section so that it isn't zeroed at boot
4684 *
Andrew Boie507852a2017-07-25 18:47:07 -07004685 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004686 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004687 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004688 *
4689 * It is legal to precede this definition with the 'static' keyword.
4690 *
4691 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4692 * parameter of k_thread_create(), it may not be the same as the
4693 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4694 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004695 * Some arches may round the size of the usable stack region up to satisfy
4696 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4697 * size.
4698 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004699 * @param sym Thread stack symbol name
4700 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004701 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004702 */
4703#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004704 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004705
4706/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304707 * @brief Calculate size of stacks to be allocated in a stack array
4708 *
4709 * This macro calculates the size to be allocated for the stacks
4710 * inside a stack array. It accepts the indicated "size" as a parameter
4711 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4712 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4713 *
4714 * @param size Size of the stack memory region
4715 * @req K-TSTACK-001
4716 */
4717#define K_THREAD_STACK_LEN(size) (size)
4718
4719/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004720 * @brief Declare a toplevel array of thread stack memory regions
4721 *
4722 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4723 * definition for additional details and constraints.
4724 *
4725 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4726 * 'noinit' section so that it isn't zeroed at boot
4727 *
4728 * @param sym Thread stack symbol name
4729 * @param nmemb Number of stacks to declare
4730 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004731 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004732 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004733#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004734 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304735 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004736
4737/**
4738 * @brief Declare an embedded stack memory region
4739 *
4740 * Used for stacks embedded within other data structures. Use is highly
4741 * discouraged but in some cases necessary. For memory protection scenarios,
4742 * it is very important that any RAM preceding this member not be writable
4743 * by threads else a stack overflow will lead to silent corruption. In other
4744 * words, the containing data structure should live in RAM owned by the kernel.
4745 *
4746 * @param sym Thread stack symbol name
4747 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004748 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004749 */
4750#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004751 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004752
4753/**
4754 * @brief Return the size in bytes of a stack memory region
4755 *
4756 * Convenience macro for passing the desired stack size to k_thread_create()
4757 * since the underlying implementation may actually create something larger
4758 * (for instance a guard area).
4759 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004760 * The value returned here is not guaranteed to match the 'size' parameter
4761 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004762 *
4763 * @param sym Stack memory symbol
4764 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004765 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004766 */
4767#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4768
4769/**
4770 * @brief Get a pointer to the physical stack buffer
4771 *
4772 * Convenience macro to get at the real underlying stack buffer used by
4773 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4774 * This is really only intended for diagnostic tools which want to examine
4775 * stack memory contents.
4776 *
4777 * @param sym Declared stack symbol name
4778 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004779 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004780 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004781static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004782{
4783 return (char *)sym;
4784}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004785
4786#endif /* _ARCH_DECLARE_STACK */
4787
Chunlin Hane9c97022017-07-07 20:29:30 +08004788/**
4789 * @defgroup mem_domain_apis Memory domain APIs
4790 * @ingroup kernel_apis
4791 * @{
4792 */
4793
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004794/**
4795 * @def MEM_PARTITION_ENTRY
4796 * @brief Used to declare a memory partition entry
4797 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004798 */
4799#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4800 {\
4801 .start = _start, \
4802 .size = _size, \
4803 .attr = _attr, \
4804 }
4805
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004806/**
4807 * @def K_MEM_PARTITION_DEFINE
4808 * @brief Used to declare a memory partition
4809 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004810 */
4811#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4812#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4813 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304814 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004815 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4816#else
4817#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304818 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004819 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4820#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4821
Chunlin Hane9c97022017-07-07 20:29:30 +08004822/* memory partition */
4823struct k_mem_partition {
4824 /* start address of memory partition */
4825 u32_t start;
4826 /* size of memory partition */
4827 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304828#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004829 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304830 k_mem_partition_attr_t attr;
4831#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004832};
4833
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004834/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304835 * Note: Always declare this structure with __kernel prefix
4836 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004837struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304838#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004839 /* partitions in the domain */
4840 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304841#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004842 /* domain q */
4843 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004844 /* number of partitions in the domain */
4845 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004846};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304847
Chunlin Hane9c97022017-07-07 20:29:30 +08004848
4849/**
4850 * @brief Initialize a memory domain.
4851 *
4852 * Initialize a memory domain with given name and memory partitions.
4853 *
4854 * @param domain The memory domain to be initialized.
4855 * @param num_parts The number of array items of "parts" parameter.
4856 * @param parts An array of pointers to the memory partitions. Can be NULL
4857 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004858 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004859 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004860extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004861 struct k_mem_partition *parts[]);
4862/**
4863 * @brief Destroy a memory domain.
4864 *
4865 * Destroy a memory domain.
4866 *
4867 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004868 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004869 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004870extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4871
4872/**
4873 * @brief Add a memory partition into a memory domain.
4874 *
4875 * Add a memory partition into a memory domain.
4876 *
4877 * @param domain The memory domain to be added a memory partition.
4878 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004879 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004880 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004881extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4882 struct k_mem_partition *part);
4883
4884/**
4885 * @brief Remove a memory partition from a memory domain.
4886 *
4887 * Remove a memory partition from a memory domain.
4888 *
4889 * @param domain The memory domain to be removed a memory partition.
4890 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004891 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004892 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004893extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4894 struct k_mem_partition *part);
4895
4896/**
4897 * @brief Add a thread into a memory domain.
4898 *
4899 * Add a thread into a memory domain.
4900 *
4901 * @param domain The memory domain that the thread is going to be added into.
4902 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004903 *
4904 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004905 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004906extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4907 k_tid_t thread);
4908
4909/**
4910 * @brief Remove a thread from its memory domain.
4911 *
4912 * Remove a thread from its memory domain.
4913 *
4914 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004915 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004916 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004917extern void k_mem_domain_remove_thread(k_tid_t thread);
4918
Anas Nashif166f5192018-02-25 08:02:36 -06004919/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004920
Andrew Boie756f9072017-10-10 16:01:49 -07004921/**
4922 * @brief Emit a character buffer to the console device
4923 *
4924 * @param c String of characters to print
4925 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004926 *
4927 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004928 */
4929__syscall void k_str_out(char *c, size_t n);
4930
Andy Rosse7172672018-01-24 15:48:32 -08004931/**
4932 * @brief Start a numbered CPU on a MP-capable system
4933
4934 * This starts and initializes a specific CPU. The main thread on
4935 * startup is running on CPU zero, other processors are numbered
4936 * sequentially. On return from this function, the CPU is known to
4937 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004938 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004939 * with the provided key will work to enable them.
4940 *
4941 * Normally, in SMP mode this function will be called by the kernel
4942 * initialization and should not be used as a user API. But it is
4943 * defined here for special-purpose apps which want Zephyr running on
4944 * one core and to use others for design-specific processing.
4945 *
4946 * @param cpu_num Integer number of the CPU
4947 * @param stack Stack memory for the CPU
4948 * @param sz Stack buffer size, in bytes
4949 * @param fn Function to begin running on the CPU. First argument is
4950 * an irq_unlock() key.
4951 * @param arg Untyped argument to be passed to "fn"
4952 */
4953extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4954 void (*fn)(int, void *), void *arg);
4955
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004956#ifdef __cplusplus
4957}
4958#endif
4959
Andrew Boiee004dec2016-11-07 09:01:19 -08004960#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4961/*
4962 * Define new and delete operators.
4963 * At this moment, the operators do nothing since objects are supposed
4964 * to be statically allocated.
4965 */
4966inline void operator delete(void *ptr)
4967{
4968 (void)ptr;
4969}
4970
4971inline void operator delete[](void *ptr)
4972{
4973 (void)ptr;
4974}
4975
4976inline void *operator new(size_t size)
4977{
4978 (void)size;
4979 return NULL;
4980}
4981
4982inline void *operator new[](size_t size)
4983{
4984 (void)size;
4985 return NULL;
4986}
4987
4988/* Placement versions of operator new and delete */
4989inline void operator delete(void *ptr1, void *ptr2)
4990{
4991 (void)ptr1;
4992 (void)ptr2;
4993}
4994
4995inline void operator delete[](void *ptr1, void *ptr2)
4996{
4997 (void)ptr1;
4998 (void)ptr2;
4999}
5000
5001inline void *operator new(size_t size, void *ptr)
5002{
5003 (void)size;
5004 return ptr;
5005}
5006
5007inline void *operator new[](size_t size, void *ptr)
5008{
5009 (void)size;
5010 return ptr;
5011}
5012
5013#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
5014
Anas Nashifb6304e62018-07-04 08:03:03 -05005015#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005016#include <syscalls/kernel.h>
5017
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005018#endif /* !_ASMLANGUAGE */
5019
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005020#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */