blob: ba6c20f935b4e74641b3c0a32f5bb4f8d529a9ee [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 Boie1e06ffc2017-09-11 09:30:04 -0700358/**
359 * @typedef k_thread_entry_t
360 * @brief Thread entry point function type.
361 *
362 * A thread's entry point function is invoked when the thread starts executing.
363 * Up to 3 argument values can be passed to the function.
364 *
365 * The thread terminates execution permanently if the entry point function
366 * returns. The thread is responsible for releasing any shared resources
367 * it may own (such as mutexes and dynamically allocated memory), prior to
368 * returning.
369 *
370 * @param p1 First argument.
371 * @param p2 Second argument.
372 * @param p3 Third argument.
373 *
374 * @return N/A
375 */
376typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700377
378#ifdef CONFIG_THREAD_MONITOR
379struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700380 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700381 void *parameter1;
382 void *parameter2;
383 void *parameter3;
384};
385#endif
386
387/* can be used for creating 'dummy' threads, e.g. for pending on objects */
388struct _thread_base {
389
390 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700391 union {
392 sys_dlist_t qnode_dlist;
393 struct rbnode qnode_rb;
394 };
395
Andy Ross1acd8c22018-05-03 14:51:49 -0700396 /* wait queue on which the thread is pended (needed only for
397 * trees, not dumb lists)
398 */
399 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700400
401 /* user facing 'thread options'; values defined in include/kernel.h */
402 u8_t user_options;
403
404 /* thread state */
405 u8_t thread_state;
406
407 /*
408 * scheduler lock count and thread priority
409 *
410 * These two fields control the preemptibility of a thread.
411 *
412 * When the scheduler is locked, sched_locked is decremented, which
413 * means that the scheduler is locked for values from 0xff to 0x01. A
414 * thread is coop if its prio is negative, thus 0x80 to 0xff when
415 * looked at the value as unsigned.
416 *
417 * By putting them end-to-end, this means that a thread is
418 * non-preemptible if the bundled value is greater than or equal to
419 * 0x0080.
420 */
421 union {
422 struct {
423#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
424 u8_t sched_locked;
425 s8_t prio;
426#else /* LITTLE and PDP */
427 s8_t prio;
428 u8_t sched_locked;
429#endif
430 };
431 u16_t preempt;
432 };
433
Andy Ross4a2e50f2018-05-15 11:06:25 -0700434#ifdef CONFIG_SCHED_DEADLINE
435 int prio_deadline;
436#endif
437
Andy Ross1acd8c22018-05-03 14:51:49 -0700438 u32_t order_key;
439
Andy Ross2724fd12018-01-29 14:55:20 -0800440#ifdef CONFIG_SMP
441 /* True for the per-CPU idle threads */
442 u8_t is_idle;
443
Andy Ross2724fd12018-01-29 14:55:20 -0800444 /* CPU index on which thread was last run */
445 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700446
447 /* Recursive count of irq_lock() calls */
448 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800449#endif
450
Andrew Boie73abd322017-04-04 13:19:13 -0700451 /* data returned by APIs */
452 void *swap_data;
453
454#ifdef CONFIG_SYS_CLOCK_EXISTS
455 /* this thread's entry in a timeout queue */
456 struct _timeout timeout;
457#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700458};
459
460typedef struct _thread_base _thread_base_t;
461
462#if defined(CONFIG_THREAD_STACK_INFO)
463/* Contains the stack information of a thread */
464struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700465 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
466 * object. Represents thread-writable stack area without any extras.
467 */
Andrew Boie73abd322017-04-04 13:19:13 -0700468 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700469
470 /* Stack Size - Thread writable stack buffer size. Represents
471 * the size of the actual area, starting from the start member,
472 * that should be writable by the thread
473 */
Andrew Boie73abd322017-04-04 13:19:13 -0700474 u32_t size;
475};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700476
477typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700478#endif /* CONFIG_THREAD_STACK_INFO */
479
Chunlin Hane9c97022017-07-07 20:29:30 +0800480#if defined(CONFIG_USERSPACE)
481struct _mem_domain_info {
482 /* memory domain queue node */
483 sys_dnode_t mem_domain_q_node;
484 /* memory domain of the thread */
485 struct k_mem_domain *mem_domain;
486};
487
488#endif /* CONFIG_USERSPACE */
489
Daniel Leungfc182432018-08-16 15:42:28 -0700490#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
491struct _thread_userspace_local_data {
492 int errno_var;
493};
494#endif
495
Anas Nashifce78d162018-05-24 12:43:11 -0500496/**
497 * @ingroup thread_apis
498 * Thread Structure
499 */
Andrew Boie73abd322017-04-04 13:19:13 -0700500struct k_thread {
501
502 struct _thread_base base;
503
Anas Nashifce78d162018-05-24 12:43:11 -0500504 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700505 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500506 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700507 struct _callee_saved callee_saved;
508
Anas Nashifce78d162018-05-24 12:43:11 -0500509 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700510 void *init_data;
511
Anas Nashifce78d162018-05-24 12:43:11 -0500512 /**
513 * abort function
514 * @req K-THREAD-002
515 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700516 void (*fn_abort)(void);
517
518#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500519 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700520 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700521
Anas Nashifce78d162018-05-24 12:43:11 -0500522 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700523 struct k_thread *next_thread;
524#endif
525
Anas Nashif57554052018-03-03 02:31:05 -0600526#if defined(CONFIG_THREAD_NAME)
527 /* Thread name */
528 const char *name;
529#endif
530
Andrew Boie73abd322017-04-04 13:19:13 -0700531#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700533 void *custom_data;
534#endif
535
Daniel Leungfc182432018-08-16 15:42:28 -0700536#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
537 struct _thread_userspace_local_data *userspace_local_data;
538#endif
539
Andrew Boie73abd322017-04-04 13:19:13 -0700540#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700541#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500542 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700543 int errno_var;
544#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700545#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700546
547#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500548 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700549 struct _thread_stack_info stack_info;
550#endif /* CONFIG_THREAD_STACK_INFO */
551
Chunlin Hane9c97022017-07-07 20:29:30 +0800552#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500553 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800554 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700556 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800557#endif /* CONFIG_USERSPACE */
558
Andy Ross042d8ec2017-12-09 08:37:20 -0800559#if defined(CONFIG_USE_SWITCH)
560 /* When using __switch() a few previously arch-specific items
561 * become part of the core OS
562 */
563
Anas Nashifce78d162018-05-24 12:43:11 -0500564 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800565 int swap_retval;
566
Anas Nashifce78d162018-05-24 12:43:11 -0500567 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800568 void *switch_handle;
569#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700571 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800572
Anas Nashifce78d162018-05-24 12:43:11 -0500573 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700574 struct _thread_arch arch;
575};
576
577typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400578typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700579#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400580
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400581enum execution_context_types {
582 K_ISR = 0,
583 K_COOP_THREAD,
584 K_PREEMPT_THREAD,
585};
586
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400587/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100588 * @defgroup profiling_apis Profiling APIs
589 * @ingroup kernel_apis
590 * @{
591 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530592typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
593 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100594
595/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530596 * @brief Iterate over all the threads in the system.
597 *
598 * This routine iterates over all the threads in the system and
599 * calls the user_cb function for each thread.
600 *
601 * @param user_cb Pointer to the user callback function.
602 * @param user_data Pointer to user data.
603 *
604 * @note CONFIG_THREAD_MONITOR must be set for this function
605 * to be effective. Also this API uses irq_lock to protect the
606 * _kernel.threads list which means creation of new threads and
607 * terminations of existing threads are blocked until this
608 * API returns.
609 *
610 * @return N/A
611 */
612extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
613
Anas Nashif166f5192018-02-25 08:02:36 -0600614/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100615
616/**
Allan Stephensc98da842016-11-11 15:45:03 -0500617 * @defgroup thread_apis Thread APIs
618 * @ingroup kernel_apis
619 * @{
620 */
621
Benjamin Walshed240f22017-01-22 13:05:08 -0500622#endif /* !_ASMLANGUAGE */
623
624
625/*
626 * Thread user options. May be needed by assembly code. Common part uses low
627 * bits, arch-specific use high bits.
628 */
629
Anas Nashifa541e932018-05-24 11:19:16 -0500630/**
631 * @brief system thread that must not abort
632 * @req K-THREAD-000
633 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700634#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500635
636#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500637/**
638 * @brief thread uses floating point registers
639 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700640#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500641#endif
642
Anas Nashifa541e932018-05-24 11:19:16 -0500643/**
644 * @brief user mode thread
645 *
646 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700647 * has additional restrictions
648 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700649#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700650
Anas Nashifa541e932018-05-24 11:19:16 -0500651/**
652 * @brief Inherit Permissions
653 *
654 * @details
655 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700656 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
657 * is not enabled.
658 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700659#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700660
Benjamin Walshed240f22017-01-22 13:05:08 -0500661#ifdef CONFIG_X86
662/* x86 Bitmask definitions for threads user options */
663
664#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
665/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700666#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500667#endif
668#endif
669
670/* end - thread options */
671
672#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700674 * @brief Create a thread.
675 *
676 * This routine initializes a thread, then schedules it for execution.
677 *
678 * The new thread may be scheduled for immediate execution or a delayed start.
679 * If the newly spawned thread does not have a delayed start the kernel
680 * scheduler may preempt the current thread to allow the new thread to
681 * execute.
682 *
683 * Thread options are architecture-specific, and can include K_ESSENTIAL,
684 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
685 * them using "|" (the logical OR operator).
686 *
687 * Historically, users often would use the beginning of the stack memory region
688 * to store the struct k_thread data, although corruption will occur if the
689 * stack overflows this region and stack protection features may not detect this
690 * situation.
691 *
692 * @param new_thread Pointer to uninitialized struct k_thread
693 * @param stack Pointer to the stack space.
694 * @param stack_size Stack size in bytes.
695 * @param entry Thread entry function.
696 * @param p1 1st entry point parameter.
697 * @param p2 2nd entry point parameter.
698 * @param p3 3rd entry point parameter.
699 * @param prio Thread priority.
700 * @param options Thread options.
701 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
702 *
703 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400704 *
705 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700706 */
Andrew Boie662c3452017-10-02 10:51:18 -0700707__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700708 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700709 size_t stack_size,
710 k_thread_entry_t entry,
711 void *p1, void *p2, void *p3,
712 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700713
Andrew Boie3f091b52017-08-30 14:34:14 -0700714/**
715 * @brief Drop a thread's privileges permanently to user mode
716 *
717 * @param entry Function to start executing from
718 * @param p1 1st entry point parameter
719 * @param p2 2nd entry point parameter
720 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400721 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700722 */
723extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
724 void *p1, void *p2,
725 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700726
Andrew Boied26cf2d2017-03-30 13:07:02 -0700727/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700728 * @brief Grant a thread access to a NULL-terminated set of kernel objects
729 *
730 * This is a convenience function. For the provided thread, grant access to
731 * the remaining arguments, which must be pointers to kernel objects.
732 * The final argument must be a NULL.
733 *
734 * The thread object must be initialized (i.e. running). The objects don't
735 * need to be.
736 *
737 * @param thread Thread to grant access to objects
738 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400739 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700740 */
741extern void __attribute__((sentinel))
742 k_thread_access_grant(struct k_thread *thread, ...);
743
744/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700745 * @brief Assign a resource memory pool to a thread
746 *
747 * By default, threads have no resource pool assigned unless their parent
748 * thread has a resource pool, in which case it is inherited. Multiple
749 * threads may be assigned to the same memory pool.
750 *
751 * Changing a thread's resource pool will not migrate allocations from the
752 * previous pool.
753 *
754 * @param thread Target thread to assign a memory pool for resource requests,
755 * or NULL if the thread should no longer have a memory pool.
756 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400757 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700758 */
759static inline void k_thread_resource_pool_assign(struct k_thread *thread,
760 struct k_mem_pool *pool)
761{
762 thread->resource_pool = pool;
763}
764
765#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
766/**
767 * @brief Assign the system heap as a thread's resource pool
768 *
769 * Similar to k_thread_resource_pool_assign(), but the thread will use
770 * the kernel heap to draw memory.
771 *
772 * Use with caution, as a malicious thread could perform DoS attacks on the
773 * kernel heap.
774 *
775 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400776 *
777 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700778 */
779void k_thread_system_pool_assign(struct k_thread *thread);
780#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
781
782/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500783 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
Allan Stephensc98da842016-11-11 15:45:03 -0500785 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
790 * @return N/A
791 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700792__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
797 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 * @return N/A
801 */
Kumar Galacc334c72017-04-21 10:55:34 -0500802extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803
804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
811 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400812 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 */
Andrew Boie468190a2017-09-29 14:00:48 -0700814__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815
816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * If @a thread is not currently sleeping, the routine has no effect.
822 *
823 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
825 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400826 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 */
Andrew Boie468190a2017-09-29 14:00:48 -0700828__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829
830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400834 *
835 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700837__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838
839/**
Allan Stephensc98da842016-11-11 15:45:03 -0500840 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine permanently stops execution of @a thread. The thread is taken
843 * off all kernel queues it is part of (i.e. the ready queue, the timeout
844 * queue, or a kernel object wait queue). However, any kernel resources the
845 * thread might currently own (such as mutexes or memory blocks) are not
846 * released. It is the responsibility of the caller of this routine to ensure
847 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
851 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400852 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 */
Andrew Boie468190a2017-09-29 14:00:48 -0700854__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400855
Andrew Boie7d627c52017-08-30 11:01:56 -0700856
857/**
858 * @brief Start an inactive thread
859 *
860 * If a thread was created with K_FOREVER in the delay parameter, it will
861 * not be added to the scheduling queue until this function is called
862 * on it.
863 *
864 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400865 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700866 */
Andrew Boie468190a2017-09-29 14:00:48 -0700867__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700868
Allan Stephensc98da842016-11-11 15:45:03 -0500869/**
870 * @cond INTERNAL_HIDDEN
871 */
872
Benjamin Walshd211a522016-12-06 11:44:01 -0500873/* timeout has timed out and is not on _timeout_q anymore */
874#define _EXPIRED (-2)
875
876/* timeout is not in use */
877#define _INACTIVE (-1)
878
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400879struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700880 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700881 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400882 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700883 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500884 void *init_p1;
885 void *init_p2;
886 void *init_p3;
887 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500888 u32_t init_options;
889 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500890 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600891 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400892};
893
Andrew Boied26cf2d2017-03-30 13:07:02 -0700894#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400895 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600896 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500897 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700898 .init_thread = (thread), \
899 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500900 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700901 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400902 .init_p1 = (void *)p1, \
903 .init_p2 = (void *)p2, \
904 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500905 .init_prio = (prio), \
906 .init_options = (options), \
907 .init_delay = (delay), \
908 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600909 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400910 }
911
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400912/**
Allan Stephensc98da842016-11-11 15:45:03 -0500913 * INTERNAL_HIDDEN @endcond
914 */
915
916/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500917 * @brief Statically define and initialize a thread.
918 *
919 * The thread may be scheduled for immediate execution or a delayed start.
920 *
921 * Thread options are architecture-specific, and can include K_ESSENTIAL,
922 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
923 * them using "|" (the logical OR operator).
924 *
925 * The ID of the thread can be accessed using:
926 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500927 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 *
929 * @param name Name of the thread.
930 * @param stack_size Stack size in bytes.
931 * @param entry Thread entry function.
932 * @param p1 1st entry point parameter.
933 * @param p2 2nd entry point parameter.
934 * @param p3 3rd entry point parameter.
935 * @param prio Thread priority.
936 * @param options Thread options.
937 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400938 *
Anas Nashif47420d02018-05-24 14:20:56 -0400939 * @req K-THREAD-010
940 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400941 * @internal It has been observed that the x86 compiler by default aligns
942 * these _static_thread_data structures to 32-byte boundaries, thereby
943 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400944 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400945 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500946#define K_THREAD_DEFINE(name, stack_size, \
947 entry, p1, p2, p3, \
948 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700949 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700950 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500951 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500952 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700953 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
954 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500955 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600956 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700957 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400958
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500960 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @param thread ID of thread whose priority is needed.
965 *
966 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400967 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700969__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970
971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 *
976 * Rescheduling can occur immediately depending on the priority @a thread is
977 * set to:
978 *
979 * - If its priority is raised above the priority of the caller of this
980 * function, and the caller is preemptible, @a thread will be scheduled in.
981 *
982 * - If the caller operates on itself, it lowers its priority below that of
983 * other threads in the system, and the caller is preemptible, the thread of
984 * highest priority will be scheduled in.
985 *
986 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
987 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
988 * highest priority.
989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 * @param prio New priority.
992 *
993 * @warning Changing the priority of a thread currently involved in mutex
994 * priority inheritance may result in undefined behavior.
995 *
996 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400997 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 */
Andrew Boie468190a2017-09-29 14:00:48 -0700999__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001000
Andy Ross4a2e50f2018-05-15 11:06:25 -07001001
1002#ifdef CONFIG_SCHED_DEADLINE
1003/**
1004 * @brief Set deadline expiration time for scheduler
1005 *
1006 * This sets the "deadline" expiration as a time delta from the
1007 * current time, in the same units used by k_cycle_get_32(). The
1008 * scheduler (when deadline scheduling is enabled) will choose the
1009 * next expiring thread when selecting between threads at the same
1010 * static priority. Threads at different priorities will be scheduled
1011 * according to their static priority.
1012 *
1013 * @note Deadlines that are negative (i.e. in the past) are still seen
1014 * as higher priority than others, even if the thread has "finished"
1015 * its work. If you don't want it scheduled anymore, you have to
1016 * reset the deadline into the future, block/pend the thread, or
1017 * modify its priority with k_thread_priority_set().
1018 *
1019 * @note Despite the API naming, the scheduler makes no guarantees the
1020 * the thread WILL be scheduled within that deadline, nor does it take
1021 * extra metadata (like e.g. the "runtime" and "period" parameters in
1022 * Linux sched_setattr()) that allows the kernel to validate the
1023 * scheduling for achievability. Such features could be implemented
1024 * above this call, which is simply input to the priority selection
1025 * logic.
1026 *
1027 * @param thread A thread on which to set the deadline
1028 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001029 *
1030 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001031 */
1032__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1033#endif
1034
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001036 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * This routine prevents the kernel scheduler from making @a thread the
1039 * current thread. All other internal operations on @a thread are still
1040 * performed; for example, any timeout it is waiting on keeps ticking,
1041 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * If @a thread is already suspended, the routine has no effect.
1044 *
1045 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 *
1047 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001048 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049 */
Andrew Boie468190a2017-09-29 14:00:48 -07001050__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051
1052/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001053 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * This routine allows the kernel scheduler to make @a thread the current
1056 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * If @a thread is not currently suspended, the routine has no effect.
1059 *
1060 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 *
1062 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001063 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064 */
Andrew Boie468190a2017-09-29 14:00:48 -07001065__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001068 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * This routine specifies how the scheduler will perform time slicing of
1071 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * To enable time slicing, @a slice must be non-zero. The scheduler
1074 * ensures that no thread runs for more than the specified time limit
1075 * before other threads of that priority are given a chance to execute.
1076 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001077 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 * execute. Once the scheduler selects a thread for execution, there is no
1081 * minimum guaranteed time the thread will execute before threads of greater or
1082 * equal priority are scheduled.
1083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001084 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 * for execution, this routine has no effect; the thread is immediately
1086 * rescheduled after the slice period expires.
1087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001088 * To disable timeslicing, set both @a slice and @a prio to zero.
1089 *
1090 * @param slice Maximum time slice length (in milliseconds).
1091 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
1093 * @return N/A
1094 */
Kumar Galacc334c72017-04-21 10:55:34 -05001095extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096
Anas Nashif166f5192018-02-25 08:02:36 -06001097/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001098
1099/**
1100 * @addtogroup isr_apis
1101 * @{
1102 */
1103
1104/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001105 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106 *
Allan Stephensc98da842016-11-11 15:45:03 -05001107 * This routine allows the caller to customize its actions, depending on
1108 * whether it is a thread or an ISR.
1109 *
1110 * @note Can be called by ISRs.
1111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * @return 0 if invoked by a thread.
1113 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001115extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001116
Benjamin Walsh445830d2016-11-10 15:54:27 -05001117/**
1118 * @brief Determine if code is running in a preemptible thread.
1119 *
Allan Stephensc98da842016-11-11 15:45:03 -05001120 * This routine allows the caller to customize its actions, depending on
1121 * whether it can be preempted by another thread. The routine returns a 'true'
1122 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001123 *
Allan Stephensc98da842016-11-11 15:45:03 -05001124 * - The code is running in a thread, not at ISR.
1125 * - The thread's priority is in the preemptible range.
1126 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001127 *
Allan Stephensc98da842016-11-11 15:45:03 -05001128 * @note Can be called by ISRs.
1129 *
1130 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001131 * @return Non-zero if invoked by a preemptible thread.
1132 */
Andrew Boie468190a2017-09-29 14:00:48 -07001133__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001134
Allan Stephensc98da842016-11-11 15:45:03 -05001135/**
Anas Nashif166f5192018-02-25 08:02:36 -06001136 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001137 */
1138
1139/**
1140 * @addtogroup thread_apis
1141 * @{
1142 */
1143
1144/**
1145 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001146 *
Allan Stephensc98da842016-11-11 15:45:03 -05001147 * This routine prevents the current thread from being preempted by another
1148 * thread by instructing the scheduler to treat it as a cooperative thread.
1149 * If the thread subsequently performs an operation that makes it unready,
1150 * it will be context switched out in the normal manner. When the thread
1151 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001152 *
Allan Stephensc98da842016-11-11 15:45:03 -05001153 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001154 *
Allan Stephensc98da842016-11-11 15:45:03 -05001155 * @note k_sched_lock() and k_sched_unlock() should normally be used
1156 * when the operation being performed can be safely interrupted by ISRs.
1157 * However, if the amount of processing involved is very small, better
1158 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001159 *
1160 * @return N/A
1161 */
1162extern void k_sched_lock(void);
1163
Allan Stephensc98da842016-11-11 15:45:03 -05001164/**
1165 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001166 *
Allan Stephensc98da842016-11-11 15:45:03 -05001167 * This routine reverses the effect of a previous call to k_sched_lock().
1168 * A thread must call the routine once for each time it called k_sched_lock()
1169 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001170 *
1171 * @return N/A
1172 */
1173extern void k_sched_unlock(void);
1174
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * Custom data is not used by the kernel itself, and is freely available
1181 * for a thread to use as it sees fit. It can be used as a framework
1182 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001184 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 *
1186 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001187 *
1188 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001189 */
Andrew Boie468190a2017-09-29 14:00:48 -07001190__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191
1192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001193 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001195 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001198 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001199 */
Andrew Boie468190a2017-09-29 14:00:48 -07001200__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201
1202/**
Anas Nashif57554052018-03-03 02:31:05 -06001203 * @brief Set current thread name
1204 *
1205 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1206 * tracing and debugging.
1207 *
1208 */
1209__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1210
1211/**
1212 * @brief Get thread name
1213 *
1214 * Get the name of a thread
1215 *
1216 * @param thread_id Thread ID
1217 *
1218 */
1219__syscall const char *k_thread_name_get(k_tid_t thread_id);
1220
1221/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001222 * @addtogroup clock_apis
1223 * @{
1224 */
1225
1226/**
1227 * @brief Generate null timeout delay.
1228 *
1229 * This macro generates a timeout delay that that instructs a kernel API
1230 * not to wait if the requested operation cannot be performed immediately.
1231 *
1232 * @return Timeout delay value.
1233 */
1234#define K_NO_WAIT 0
1235
1236/**
1237 * @brief Generate timeout delay from milliseconds.
1238 *
1239 * This macro generates a timeout delay that that instructs a kernel API
1240 * to wait up to @a ms milliseconds to perform the requested operation.
1241 *
1242 * @param ms Duration in milliseconds.
1243 *
1244 * @return Timeout delay value.
1245 */
Johan Hedberg14471692016-11-13 10:52:15 +02001246#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001247
1248/**
1249 * @brief Generate timeout delay from seconds.
1250 *
1251 * This macro generates a timeout delay that that instructs a kernel API
1252 * to wait up to @a s seconds to perform the requested operation.
1253 *
1254 * @param s Duration in seconds.
1255 *
1256 * @return Timeout delay value.
1257 */
Johan Hedberg14471692016-11-13 10:52:15 +02001258#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001259
1260/**
1261 * @brief Generate timeout delay from minutes.
1262 *
1263 * This macro generates a timeout delay that that instructs a kernel API
1264 * to wait up to @a m minutes to perform the requested operation.
1265 *
1266 * @param m Duration in minutes.
1267 *
1268 * @return Timeout delay value.
1269 */
Johan Hedberg14471692016-11-13 10:52:15 +02001270#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001271
1272/**
1273 * @brief Generate timeout delay from hours.
1274 *
1275 * This macro generates a timeout delay that that instructs a kernel API
1276 * to wait up to @a h hours to perform the requested operation.
1277 *
1278 * @param h Duration in hours.
1279 *
1280 * @return Timeout delay value.
1281 */
Johan Hedberg14471692016-11-13 10:52:15 +02001282#define K_HOURS(h) K_MINUTES((h) * 60)
1283
Allan Stephensc98da842016-11-11 15:45:03 -05001284/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001285 * @brief Generate infinite timeout delay.
1286 *
1287 * This macro generates a timeout delay that that instructs a kernel API
1288 * to wait as long as necessary to perform the requested operation.
1289 *
1290 * @return Timeout delay value.
1291 */
1292#define K_FOREVER (-1)
1293
1294/**
Anas Nashif166f5192018-02-25 08:02:36 -06001295 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001296 */
1297
1298/**
Allan Stephensc98da842016-11-11 15:45:03 -05001299 * @cond INTERNAL_HIDDEN
1300 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001301
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001302struct k_timer {
1303 /*
1304 * _timeout structure must be first here if we want to use
1305 * dynamic timer allocation. timeout.node is used in the double-linked
1306 * list of free timers
1307 */
1308 struct _timeout timeout;
1309
Allan Stephens45bfa372016-10-12 12:39:42 -05001310 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001311 _wait_q_t wait_q;
1312
1313 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001314 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315
1316 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001317 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001318
1319 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001320 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001321
Allan Stephens45bfa372016-10-12 12:39:42 -05001322 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001323 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001324
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001325 /* user-specific data, also used to support legacy features */
1326 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001327
Anas Nashif2f203c22016-12-18 06:57:45 -05001328 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001329};
1330
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001331#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001332 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001333 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001334 .timeout.thread = NULL, \
1335 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001336 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001337 .expiry_fn = expiry, \
1338 .stop_fn = stop, \
1339 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001340 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001341 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001342 }
1343
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001344#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1345
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346/**
Allan Stephensc98da842016-11-11 15:45:03 -05001347 * INTERNAL_HIDDEN @endcond
1348 */
1349
1350/**
1351 * @defgroup timer_apis Timer APIs
1352 * @ingroup kernel_apis
1353 * @{
1354 */
1355
1356/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001357 * @typedef k_timer_expiry_t
1358 * @brief Timer expiry function type.
1359 *
1360 * A timer's expiry function is executed by the system clock interrupt handler
1361 * each time the timer expires. The expiry function is optional, and is only
1362 * invoked if the timer has been initialized with one.
1363 *
1364 * @param timer Address of timer.
1365 *
1366 * @return N/A
1367 */
1368typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1369
1370/**
1371 * @typedef k_timer_stop_t
1372 * @brief Timer stop function type.
1373 *
1374 * A timer's stop function is executed if the timer is stopped prematurely.
1375 * The function runs in the context of the thread that stops the timer.
1376 * The stop function is optional, and is only invoked if the timer has been
1377 * initialized with one.
1378 *
1379 * @param timer Address of timer.
1380 *
1381 * @return N/A
1382 */
1383typedef void (*k_timer_stop_t)(struct k_timer *timer);
1384
1385/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001389 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001390 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001391 *
1392 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * @param expiry_fn Function to invoke each time the timer expires.
1394 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001396#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001397 struct k_timer name \
1398 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001399 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
Allan Stephens45bfa372016-10-12 12:39:42 -05001401/**
1402 * @brief Initialize a timer.
1403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001404 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001405 *
1406 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * @param expiry_fn Function to invoke each time the timer expires.
1408 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001409 *
1410 * @return N/A
1411 */
1412extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001413 k_timer_expiry_t expiry_fn,
1414 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001415
Allan Stephens45bfa372016-10-12 12:39:42 -05001416/**
1417 * @brief Start a timer.
1418 *
1419 * This routine starts a timer, and resets its status to zero. The timer
1420 * begins counting down using the specified duration and period values.
1421 *
1422 * Attempting to start a timer that is already running is permitted.
1423 * The timer's status is reset to zero and the timer begins counting down
1424 * using the new duration and period values.
1425 *
1426 * @param timer Address of timer.
1427 * @param duration Initial timer duration (in milliseconds).
1428 * @param period Timer period (in milliseconds).
1429 *
1430 * @return N/A
1431 */
Andrew Boiea354d492017-09-29 16:22:28 -07001432__syscall void k_timer_start(struct k_timer *timer,
1433 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001434
1435/**
1436 * @brief Stop a timer.
1437 *
1438 * This routine stops a running timer prematurely. The timer's stop function,
1439 * if one exists, is invoked by the caller.
1440 *
1441 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001442 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001443 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001444 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1445 * if @a k_timer_stop is to be called from ISRs.
1446 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001447 * @param timer Address of timer.
1448 *
1449 * @return N/A
1450 */
Andrew Boiea354d492017-09-29 16:22:28 -07001451__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001452
1453/**
1454 * @brief Read timer status.
1455 *
1456 * This routine reads the timer's status, which indicates the number of times
1457 * it has expired since its status was last read.
1458 *
1459 * Calling this routine resets the timer's status to zero.
1460 *
1461 * @param timer Address of timer.
1462 *
1463 * @return Timer status.
1464 */
Andrew Boiea354d492017-09-29 16:22:28 -07001465__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001466
1467/**
1468 * @brief Synchronize thread to timer expiration.
1469 *
1470 * This routine blocks the calling thread until the timer's status is non-zero
1471 * (indicating that it has expired at least once since it was last examined)
1472 * or the timer is stopped. If the timer status is already non-zero,
1473 * or the timer is already stopped, the caller continues without waiting.
1474 *
1475 * Calling this routine resets the timer's status to zero.
1476 *
1477 * This routine must not be used by interrupt handlers, since they are not
1478 * allowed to block.
1479 *
1480 * @param timer Address of timer.
1481 *
1482 * @return Timer status.
1483 */
Andrew Boiea354d492017-09-29 16:22:28 -07001484__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001485
Andy Ross52e444b2018-09-28 09:06:37 -07001486extern s32_t z_timeout_remaining(struct _timeout *timeout);
1487
Allan Stephens45bfa372016-10-12 12:39:42 -05001488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001490 *
1491 * This routine computes the (approximate) time remaining before a running
1492 * timer next expires. If the timer is not running, it returns zero.
1493 *
1494 * @param timer Address of timer.
1495 *
1496 * @return Remaining time (in milliseconds).
1497 */
Andrew Boiea354d492017-09-29 16:22:28 -07001498__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1499
1500static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001501{
Andy Ross52e444b2018-09-28 09:06:37 -07001502 return __ticks_to_ms(z_timeout_remaining(&timer->timeout));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001503}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001504
Allan Stephensc98da842016-11-11 15:45:03 -05001505/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001506 * @brief Associate user-specific data with a timer.
1507 *
1508 * This routine records the @a user_data with the @a timer, to be retrieved
1509 * later.
1510 *
1511 * It can be used e.g. in a timer handler shared across multiple subsystems to
1512 * retrieve data specific to the subsystem this timer is associated with.
1513 *
1514 * @param timer Address of timer.
1515 * @param user_data User data to associate with the timer.
1516 *
1517 * @return N/A
1518 */
Andrew Boiea354d492017-09-29 16:22:28 -07001519__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1520
Anas Nashif954d5502018-02-25 08:37:28 -06001521/**
1522 * @internal
1523 */
Andrew Boiea354d492017-09-29 16:22:28 -07001524static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1525 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001526{
1527 timer->user_data = user_data;
1528}
1529
1530/**
1531 * @brief Retrieve the user-specific data from a timer.
1532 *
1533 * @param timer Address of timer.
1534 *
1535 * @return The user data.
1536 */
Andrew Boiea354d492017-09-29 16:22:28 -07001537__syscall void *k_timer_user_data_get(struct k_timer *timer);
1538
1539static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001540{
1541 return timer->user_data;
1542}
1543
Anas Nashif166f5192018-02-25 08:02:36 -06001544/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001545
Allan Stephensc98da842016-11-11 15:45:03 -05001546/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001547 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001548 * @{
1549 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001550
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001552 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001554 * This routine returns the elapsed time since the system booted,
1555 * in milliseconds.
1556 *
1557 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001558 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001559__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001560
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001561/**
1562 * @brief Enable clock always on in tickless kernel
1563 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001564 * This routine enables keeping the clock running (that is, it always
1565 * keeps an active timer interrupt scheduled) when there are no timer
1566 * events programmed in tickless kernel scheduling. This is necessary
1567 * if the clock is used to track passage of time (e.g. via
1568 * k_uptime_get_32()), otherwise the internal hardware counter may
1569 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001570 *
1571 * @retval prev_status Previous status of always on flag
1572 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001573int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001574
1575/**
1576 * @brief Disable clock always on in tickless kernel
1577 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001578 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001579 * there are no timer events programmed in tickless kernel
1580 * scheduling. To save power, this routine should be called
1581 * immediately when clock is not used to track time.
1582 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001583void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001584
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001586 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * This routine returns the lower 32-bits of the elapsed time since the system
1589 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * This routine can be more efficient than k_uptime_get(), as it reduces the
1592 * need for interrupt locking and 64-bit math. However, the 32-bit result
1593 * cannot hold a system uptime time larger than approximately 50 days, so the
1594 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001596 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001597 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001598__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001599
1600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001601 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * This routine computes the elapsed time between the current system uptime
1604 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * @param reftime Pointer to a reference time, which is updated to the current
1607 * uptime upon return.
1608 *
1609 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001610 */
Kumar Galacc334c72017-04-21 10:55:34 -05001611extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001612
1613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001616 * This routine computes the elapsed time between the current system uptime
1617 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1620 * need for interrupt locking and 64-bit math. However, the 32-bit result
1621 * cannot hold an elapsed time larger than approximately 50 days, so the
1622 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * @param reftime Pointer to a reference time, which is updated to the current
1625 * uptime upon return.
1626 *
1627 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001628 */
Kumar Galacc334c72017-04-21 10:55:34 -05001629extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001630
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001631/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001632 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001634 * This routine returns the current time, as measured by the system's hardware
1635 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001637 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001638 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001639#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001640
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001641/**
Anas Nashif166f5192018-02-25 08:02:36 -06001642 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643 */
1644
Allan Stephensc98da842016-11-11 15:45:03 -05001645/**
1646 * @cond INTERNAL_HIDDEN
1647 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001649struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001650 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001651 union {
1652 _wait_q_t wait_q;
1653
1654 _POLL_EVENT;
1655 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001656
1657 _OBJECT_TRACING_NEXT_PTR(k_queue);
1658};
1659
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001660#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001661 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001662 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001663 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001664 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001665 _OBJECT_TRACING_INIT \
1666 }
1667
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001668#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1669
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001670extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1671
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001672/**
1673 * INTERNAL_HIDDEN @endcond
1674 */
1675
1676/**
1677 * @defgroup queue_apis Queue APIs
1678 * @ingroup kernel_apis
1679 * @{
1680 */
1681
1682/**
1683 * @brief Initialize a queue.
1684 *
1685 * This routine initializes a queue object, prior to its first use.
1686 *
1687 * @param queue Address of the queue.
1688 *
1689 * @return N/A
1690 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001691__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001692
1693/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001694 * @brief Cancel waiting on a queue.
1695 *
1696 * This routine causes first thread pending on @a queue, if any, to
1697 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001698 * If the queue is being waited on by k_poll(), it will return with
1699 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1700 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001701 *
1702 * @note Can be called by ISRs.
1703 *
1704 * @param queue Address of the queue.
1705 *
1706 * @return N/A
1707 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001708__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001709
1710/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001711 * @brief Append an element to the end of a queue.
1712 *
1713 * This routine appends a data item to @a queue. A queue data item must be
1714 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1715 * reserved for the kernel's use.
1716 *
1717 * @note Can be called by ISRs.
1718 *
1719 * @param queue Address of the queue.
1720 * @param data Address of the data item.
1721 *
1722 * @return N/A
1723 */
1724extern void k_queue_append(struct k_queue *queue, void *data);
1725
1726/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001727 * @brief Append an element to a queue.
1728 *
1729 * This routine appends a data item to @a queue. There is an implicit
1730 * memory allocation from the calling thread's resource pool, which is
1731 * automatically freed when the item is removed from the queue.
1732 *
1733 * @note Can be called by ISRs.
1734 *
1735 * @param queue Address of the queue.
1736 * @param data Address of the data item.
1737 *
1738 * @retval 0 on success
1739 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1740 */
1741__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1742
1743/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001744 * @brief Prepend an element to a queue.
1745 *
1746 * This routine prepends a data item to @a queue. A queue data item must be
1747 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1748 * reserved for the kernel's use.
1749 *
1750 * @note Can be called by ISRs.
1751 *
1752 * @param queue Address of the queue.
1753 * @param data Address of the data item.
1754 *
1755 * @return N/A
1756 */
1757extern void k_queue_prepend(struct k_queue *queue, void *data);
1758
1759/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001760 * @brief Prepend an element to a queue.
1761 *
1762 * This routine prepends a data item to @a queue. There is an implicit
1763 * memory allocation from the calling thread's resource pool, which is
1764 * automatically freed when the item is removed from the queue.
1765 *
1766 * @note Can be called by ISRs.
1767 *
1768 * @param queue Address of the queue.
1769 * @param data Address of the data item.
1770 *
1771 * @retval 0 on success
1772 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1773 */
1774__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1775
1776/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001777 * @brief Inserts an element to a queue.
1778 *
1779 * This routine inserts a data item to @a queue after previous item. A queue
1780 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1781 * item are reserved for the kernel's use.
1782 *
1783 * @note Can be called by ISRs.
1784 *
1785 * @param queue Address of the queue.
1786 * @param prev Address of the previous data item.
1787 * @param data Address of the data item.
1788 *
1789 * @return N/A
1790 */
1791extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1792
1793/**
1794 * @brief Atomically append a list of elements to a queue.
1795 *
1796 * This routine adds a list of data items to @a queue in one operation.
1797 * The data items must be in a singly-linked list, with the first 32 bits
1798 * in each data item pointing to the next data item; the list must be
1799 * NULL-terminated.
1800 *
1801 * @note Can be called by ISRs.
1802 *
1803 * @param queue Address of the queue.
1804 * @param head Pointer to first node in singly-linked list.
1805 * @param tail Pointer to last node in singly-linked list.
1806 *
1807 * @return N/A
1808 */
1809extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1810
1811/**
1812 * @brief Atomically add a list of elements to a queue.
1813 *
1814 * This routine adds a list of data items to @a queue in one operation.
1815 * The data items must be in a singly-linked list implemented using a
1816 * sys_slist_t object. Upon completion, the original list is empty.
1817 *
1818 * @note Can be called by ISRs.
1819 *
1820 * @param queue Address of the queue.
1821 * @param list Pointer to sys_slist_t object.
1822 *
1823 * @return N/A
1824 */
1825extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1826
1827/**
1828 * @brief Get an element from a queue.
1829 *
1830 * This routine removes first data item from @a queue. The first 32 bits of the
1831 * data item are reserved for the kernel's use.
1832 *
1833 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1834 *
1835 * @param queue Address of the queue.
1836 * @param timeout Waiting period to obtain a data item (in milliseconds),
1837 * or one of the special values K_NO_WAIT and K_FOREVER.
1838 *
1839 * @return Address of the data item if successful; NULL if returned
1840 * without waiting, or waiting period timed out.
1841 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001842__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001843
1844/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001845 * @brief Remove an element from a queue.
1846 *
1847 * This routine removes data item from @a queue. The first 32 bits of the
1848 * data item are reserved for the kernel's use. Removing elements from k_queue
1849 * rely on sys_slist_find_and_remove which is not a constant time operation.
1850 *
1851 * @note Can be called by ISRs
1852 *
1853 * @param queue Address of the queue.
1854 * @param data Address of the data item.
1855 *
1856 * @return true if data item was removed
1857 */
1858static inline bool k_queue_remove(struct k_queue *queue, void *data)
1859{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001860 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001861}
1862
1863/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001864 * @brief Append an element to a queue only if it's not present already.
1865 *
1866 * This routine appends data item to @a queue. The first 32 bits of the
1867 * data item are reserved for the kernel's use. Appending elements to k_queue
1868 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1869 *
1870 * @note Can be called by ISRs
1871 *
1872 * @param queue Address of the queue.
1873 * @param data Address of the data item.
1874 *
1875 * @return true if data item was added, false if not
1876 */
1877static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1878{
1879 sys_sfnode_t *test;
1880
1881 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1882 if (test == (sys_sfnode_t *) data) {
1883 return false;
1884 }
1885 }
1886
1887 k_queue_append(queue, data);
1888 return true;
1889}
1890
1891/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001892 * @brief Query a queue to see if it has data available.
1893 *
1894 * Note that the data might be already gone by the time this function returns
1895 * if other threads are also trying to read from the queue.
1896 *
1897 * @note Can be called by ISRs.
1898 *
1899 * @param queue Address of the queue.
1900 *
1901 * @return Non-zero if the queue is empty.
1902 * @return 0 if data is available.
1903 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001904__syscall int k_queue_is_empty(struct k_queue *queue);
1905
1906static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001907{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001908 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001909}
1910
1911/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001912 * @brief Peek element at the head of queue.
1913 *
1914 * Return element from the head of queue without removing it.
1915 *
1916 * @param queue Address of the queue.
1917 *
1918 * @return Head element, or NULL if queue is empty.
1919 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001920__syscall void *k_queue_peek_head(struct k_queue *queue);
1921
1922static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001923{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001924 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001925}
1926
1927/**
1928 * @brief Peek element at the tail of queue.
1929 *
1930 * Return element from the tail of queue without removing it.
1931 *
1932 * @param queue Address of the queue.
1933 *
1934 * @return Tail element, or NULL if queue is empty.
1935 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001936__syscall void *k_queue_peek_tail(struct k_queue *queue);
1937
1938static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001939{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001940 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001941}
1942
1943/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001944 * @brief Statically define and initialize a queue.
1945 *
1946 * The queue can be accessed outside the module where it is defined using:
1947 *
1948 * @code extern struct k_queue <name>; @endcode
1949 *
1950 * @param name Name of the queue.
1951 */
1952#define K_QUEUE_DEFINE(name) \
1953 struct k_queue name \
1954 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001955 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001956
Anas Nashif166f5192018-02-25 08:02:36 -06001957/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001958
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001959struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001960 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001961};
1962
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001963/**
1964 * @cond INTERNAL_HIDDEN
1965 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001966#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001967 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001968 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001969 }
1970
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001971#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1972
Allan Stephensc98da842016-11-11 15:45:03 -05001973/**
1974 * INTERNAL_HIDDEN @endcond
1975 */
1976
1977/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001978 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001979 * @ingroup kernel_apis
1980 * @{
1981 */
1982
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001984 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001986 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001988 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001989 *
1990 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001991 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001993#define k_fifo_init(fifo) \
1994 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995
1996/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001997 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001998 *
1999 * This routine causes first thread pending on @a fifo, if any, to
2000 * return from k_fifo_get() call with NULL value (as if timeout
2001 * expired).
2002 *
2003 * @note Can be called by ISRs.
2004 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002005 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002006 *
2007 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002008 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002009 */
2010#define k_fifo_cancel_wait(fifo) \
2011 k_queue_cancel_wait((struct k_queue *) fifo)
2012
2013/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002014 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002016 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002017 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2018 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002020 * @note Can be called by ISRs.
2021 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002022 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002023 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002024 *
2025 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002026 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002028#define k_fifo_put(fifo, data) \
2029 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030
2031/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002032 * @brief Add an element to a FIFO queue.
2033 *
2034 * This routine adds a data item to @a fifo. There is an implicit
2035 * memory allocation from the calling thread's resource pool, which is
2036 * automatically freed when the item is removed.
2037 *
2038 * @note Can be called by ISRs.
2039 *
2040 * @param fifo Address of the FIFO.
2041 * @param data Address of the data item.
2042 *
2043 * @retval 0 on success
2044 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002045 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002046 */
2047#define k_fifo_alloc_put(fifo, data) \
2048 k_queue_alloc_append((struct k_queue *) fifo, data)
2049
2050/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002051 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002053 * This routine adds a list of data items to @a fifo in one operation.
2054 * The data items must be in a singly-linked list, with the first 32 bits
2055 * each data item pointing to the next data item; the list must be
2056 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002058 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002060 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002061 * @param head Pointer to first node in singly-linked list.
2062 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002063 *
2064 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002065 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002066 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002067#define k_fifo_put_list(fifo, head, tail) \
2068 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069
2070/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * This routine adds a list of data items to @a fifo in one operation.
2074 * The data items must be in a singly-linked list implemented using a
2075 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 * and must be re-initialized via sys_slist_init().
2077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002078 * @note Can be called by ISRs.
2079 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002080 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002081 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082 *
2083 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002084 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002086#define k_fifo_put_slist(fifo, list) \
2087 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088
2089/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002090 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 * This routine removes a data item from @a fifo in a "first in, first out"
2093 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2096 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002097 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002099 * or one of the special values K_NO_WAIT and K_FOREVER.
2100 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002101 * @return Address of the data item if successful; NULL if returned
2102 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002103 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002105#define k_fifo_get(fifo, timeout) \
2106 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002107
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002108/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002109 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002110 *
2111 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002113 *
2114 * @note Can be called by ISRs.
2115 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002116 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002117 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002118 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002119 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002120 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002121 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002122#define k_fifo_is_empty(fifo) \
2123 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002124
2125/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002126 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002127 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002128 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302129 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002130 * on each iteration of processing, a head container will be peeked,
2131 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002132 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002133 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002134 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002135 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002136 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002137 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002138 */
2139#define k_fifo_peek_head(fifo) \
2140 k_queue_peek_head((struct k_queue *) fifo)
2141
2142/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002144 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002145 * Return element from the tail of FIFO queue (without removing it). A usecase
2146 * for this is if elements of the FIFO queue are themselves containers. Then
2147 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002148 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002149 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002150 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002151 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002152 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002153 */
2154#define k_fifo_peek_tail(fifo) \
2155 k_queue_peek_tail((struct k_queue *) fifo)
2156
2157/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002158 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002159 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002160 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002162 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002164 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002165 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002168 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002169 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002170 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002171
Anas Nashif166f5192018-02-25 08:02:36 -06002172/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002173
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002175 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002176};
2177
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002178/**
2179 * @cond INTERNAL_HIDDEN
2180 */
2181
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002182#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002183 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002184 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002185 }
2186
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002187#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2188
Allan Stephensc98da842016-11-11 15:45:03 -05002189/**
2190 * INTERNAL_HIDDEN @endcond
2191 */
2192
2193/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002194 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002195 * @ingroup kernel_apis
2196 * @{
2197 */
2198
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002199/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002200 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002201 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002202 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002203 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205 *
2206 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002207 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002208 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002209#define k_lifo_init(lifo) \
2210 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211
2212/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002216 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2217 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002219 * @note Can be called by ISRs.
2220 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002221 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002222 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 *
2224 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002225 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002227#define k_lifo_put(lifo, data) \
2228 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229
2230/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002231 * @brief Add an element to a LIFO queue.
2232 *
2233 * This routine adds a data item to @a lifo. There is an implicit
2234 * memory allocation from the calling thread's resource pool, which is
2235 * automatically freed when the item is removed.
2236 *
2237 * @note Can be called by ISRs.
2238 *
2239 * @param lifo Address of the LIFO.
2240 * @param data Address of the data item.
2241 *
2242 * @retval 0 on success
2243 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002244 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002245 */
2246#define k_lifo_alloc_put(lifo, data) \
2247 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2248
2249/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002250 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002252 * This routine removes a data item from @a lifo in a "last in, first out"
2253 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002255 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2256 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002258 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002259 * or one of the special values K_NO_WAIT and K_FOREVER.
2260 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002261 * @return Address of the data item if successful; NULL if returned
2262 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002263 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002265#define k_lifo_get(lifo, timeout) \
2266 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002271 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002273 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002275 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002276 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002279 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002280 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002281 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282
Anas Nashif166f5192018-02-25 08:02:36 -06002283/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002284
2285/**
2286 * @cond INTERNAL_HIDDEN
2287 */
Andrew Boief3bee952018-05-02 17:44:39 -07002288#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289
2290struct k_stack {
2291 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002292 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293
Anas Nashif2f203c22016-12-18 06:57:45 -05002294 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002295 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296};
2297
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002298#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002299 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002300 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002301 .base = stack_buffer, \
2302 .next = stack_buffer, \
2303 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002304 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002305 }
2306
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002307#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2308
Allan Stephensc98da842016-11-11 15:45:03 -05002309/**
2310 * INTERNAL_HIDDEN @endcond
2311 */
2312
2313/**
2314 * @defgroup stack_apis Stack APIs
2315 * @ingroup kernel_apis
2316 * @{
2317 */
2318
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319/**
2320 * @brief Initialize a stack.
2321 *
2322 * This routine initializes a stack object, prior to its first use.
2323 *
2324 * @param stack Address of the stack.
2325 * @param buffer Address of array used to hold stacked values.
2326 * @param num_entries Maximum number of values that can be stacked.
2327 *
2328 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002329 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 */
Andrew Boief3bee952018-05-02 17:44:39 -07002331void k_stack_init(struct k_stack *stack,
2332 u32_t *buffer, unsigned int num_entries);
2333
2334
2335/**
2336 * @brief Initialize a stack.
2337 *
2338 * This routine initializes a stack object, prior to its first use. Internal
2339 * buffers will be allocated from the calling thread's resource pool.
2340 * This memory will be released if k_stack_cleanup() is called, or
2341 * userspace is enabled and the stack object loses all references to it.
2342 *
2343 * @param stack Address of the stack.
2344 * @param num_entries Maximum number of values that can be stacked.
2345 *
2346 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002347 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002348 */
2349
2350__syscall int k_stack_alloc_init(struct k_stack *stack,
2351 unsigned int num_entries);
2352
2353/**
2354 * @brief Release a stack's allocated buffer
2355 *
2356 * If a stack object was given a dynamically allocated buffer via
2357 * k_stack_alloc_init(), this will free it. This function does nothing
2358 * if the buffer wasn't dynamically allocated.
2359 *
2360 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002361 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002362 */
2363void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002364
2365/**
2366 * @brief Push an element onto a stack.
2367 *
2368 * This routine adds a 32-bit value @a data to @a stack.
2369 *
2370 * @note Can be called by ISRs.
2371 *
2372 * @param stack Address of the stack.
2373 * @param data Value to push onto the stack.
2374 *
2375 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002376 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 */
Andrew Boiee8734462017-09-29 16:42:07 -07002378__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379
2380/**
2381 * @brief Pop an element from a stack.
2382 *
2383 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2384 * manner and stores the value in @a data.
2385 *
2386 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2387 *
2388 * @param stack Address of the stack.
2389 * @param data Address of area to hold the value popped from the stack.
2390 * @param timeout Waiting period to obtain a value (in milliseconds),
2391 * or one of the special values K_NO_WAIT and K_FOREVER.
2392 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002393 * @retval 0 Element popped from stack.
2394 * @retval -EBUSY Returned without waiting.
2395 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002396 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 */
Andrew Boiee8734462017-09-29 16:42:07 -07002398__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002401 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002405 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * @param name Name of the stack.
2408 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002409 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002411#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002412 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002413 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002414 struct k_stack name \
2415 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002416 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002417 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418
Anas Nashif166f5192018-02-25 08:02:36 -06002419/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002420
Allan Stephens6bba9b02016-11-16 14:56:54 -05002421struct k_work;
2422
Allan Stephensc98da842016-11-11 15:45:03 -05002423/**
2424 * @defgroup workqueue_apis Workqueue Thread APIs
2425 * @ingroup kernel_apis
2426 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427 */
2428
Allan Stephens6bba9b02016-11-16 14:56:54 -05002429/**
2430 * @typedef k_work_handler_t
2431 * @brief Work item handler function type.
2432 *
2433 * A work item's handler function is executed by a workqueue's thread
2434 * when the work item is processed by the workqueue.
2435 *
2436 * @param work Address of the work item.
2437 *
2438 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002439 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002440 */
2441typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442
2443/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002444 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002446
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002448 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002449 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450};
2451
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002453 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454};
2455
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002457 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458 k_work_handler_t handler;
2459 atomic_t flags[1];
2460};
2461
Allan Stephens6bba9b02016-11-16 14:56:54 -05002462struct k_delayed_work {
2463 struct k_work work;
2464 struct _timeout timeout;
2465 struct k_work_q *work_q;
2466};
2467
2468extern struct k_work_q k_sys_work_q;
2469
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002471 * INTERNAL_HIDDEN @endcond
2472 */
2473
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002474#define _K_WORK_INITIALIZER(work_handler) \
2475 { \
2476 ._reserved = NULL, \
2477 .handler = work_handler, \
2478 .flags = { 0 } \
2479 }
2480
2481#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2482
Allan Stephens6bba9b02016-11-16 14:56:54 -05002483/**
2484 * @brief Initialize a statically-defined work item.
2485 *
2486 * This macro can be used to initialize a statically-defined workqueue work
2487 * item, prior to its first use. For example,
2488 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002489 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002490 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002491 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002492 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002493 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002495#define K_WORK_DEFINE(work, work_handler) \
2496 struct k_work work \
2497 __in_section(_k_work, static, work) = \
2498 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499
2500/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002501 * @brief Initialize a work item.
2502 *
2503 * This routine initializes a workqueue work item, prior to its first use.
2504 *
2505 * @param work Address of work item.
2506 * @param handler Function to invoke each time work item is processed.
2507 *
2508 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002509 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510 */
2511static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2512{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002513 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002514 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515}
2516
2517/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002518 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002519 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002520 * This routine submits work item @a work to be processed by workqueue
2521 * @a work_q. If the work item is already pending in the workqueue's queue
2522 * as a result of an earlier submission, this routine has no effect on the
2523 * work item. If the work item has already been processed, or is currently
2524 * being processed, its work is considered complete and the work item can be
2525 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002526 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002527 * @warning
2528 * A submitted work item must not be modified until it has been processed
2529 * by the workqueue.
2530 *
2531 * @note Can be called by ISRs.
2532 *
2533 * @param work_q Address of workqueue.
2534 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002535 *
2536 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002537 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538 */
2539static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2540 struct k_work *work)
2541{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002542 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002543 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544 }
2545}
2546
2547/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002548 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002550 * This routine indicates if work item @a work is pending in a workqueue's
2551 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002552 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002553 * @note Can be called by ISRs.
2554 *
2555 * @param work Address of work item.
2556 *
2557 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002558 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002559 */
2560static inline int k_work_pending(struct k_work *work)
2561{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002562 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002563}
2564
2565/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002566 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002567 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002568 * This routine starts workqueue @a work_q. The workqueue spawns its work
2569 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002570 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002571 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002572 * @param stack Pointer to work queue thread's stack space, as defined by
2573 * K_THREAD_STACK_DEFINE()
2574 * @param stack_size Size of the work queue thread's stack (in bytes), which
2575 * should either be the same constant passed to
2576 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002577 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578 *
2579 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002580 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581 */
Andrew Boie507852a2017-07-25 18:47:07 -07002582extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002583 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002584 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002586/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002587 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002588 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002589 * This routine initializes a workqueue delayed work item, prior to
2590 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002591 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002592 * @param work Address of delayed work item.
2593 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002594 *
2595 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002596 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002598extern void k_delayed_work_init(struct k_delayed_work *work,
2599 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600
2601/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002604 * This routine schedules work item @a work to be processed by workqueue
2605 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002606 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002607 * Only when the countdown completes is the work item actually submitted to
2608 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002610 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002611 * counting down cancels the existing submission and restarts the
2612 * countdown using the new delay. Note that this behavior is
2613 * inherently subject to race conditions with the pre-existing
2614 * timeouts and work queue, so care must be taken to synchronize such
2615 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * @warning
2618 * A delayed work item must not be modified until it has been processed
2619 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002621 * @note Can be called by ISRs.
2622 *
2623 * @param work_q Address of workqueue.
2624 * @param work Address of delayed work item.
2625 * @param delay Delay before submitting the work item (in milliseconds).
2626 *
2627 * @retval 0 Work item countdown started.
2628 * @retval -EINPROGRESS Work item is already pending.
2629 * @retval -EINVAL Work item is being processed or has completed its work.
2630 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002631 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002633extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2634 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002635 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636
2637/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002638 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002641 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002642 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002644 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002646 * @param work Address of delayed work item.
2647 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002648 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002649 * @retval -EINPROGRESS Work item is already pending.
2650 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002651 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002653extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002655/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656 * @brief Submit a work item to the system workqueue.
2657 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002658 * This routine submits work item @a work to be processed by the system
2659 * workqueue. If the work item is already pending in the workqueue's queue
2660 * as a result of an earlier submission, this routine has no effect on the
2661 * work item. If the work item has already been processed, or is currently
2662 * being processed, its work is considered complete and the work item can be
2663 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002665 * @warning
2666 * Work items submitted to the system workqueue should avoid using handlers
2667 * that block or yield since this may prevent the system workqueue from
2668 * processing other work items in a timely manner.
2669 *
2670 * @note Can be called by ISRs.
2671 *
2672 * @param work Address of work item.
2673 *
2674 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002675 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676 */
2677static inline void k_work_submit(struct k_work *work)
2678{
2679 k_work_submit_to_queue(&k_sys_work_q, work);
2680}
2681
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002682/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683 * @brief Submit a delayed work item to the system workqueue.
2684 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002685 * This routine schedules work item @a work to be processed by the system
2686 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002687 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688 * Only when the countdown completes is the work item actually submitted to
2689 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002691 * Submitting a previously submitted delayed work item that is still
2692 * counting down cancels the existing submission and restarts the countdown
2693 * using the new delay. If the work item is currently pending on the
2694 * workqueue's queue because the countdown has completed it is too late to
2695 * resubmit the item, and resubmission fails without impacting the work item.
2696 * If the work item has already been processed, or is currently being processed,
2697 * its work is considered complete and the work item can be resubmitted.
2698 *
2699 * @warning
2700 * Work items submitted to the system workqueue should avoid using handlers
2701 * that block or yield since this may prevent the system workqueue from
2702 * processing other work items in a timely manner.
2703 *
2704 * @note Can be called by ISRs.
2705 *
2706 * @param work Address of delayed work item.
2707 * @param delay Delay before submitting the work item (in milliseconds).
2708 *
2709 * @retval 0 Work item countdown started.
2710 * @retval -EINPROGRESS Work item is already pending.
2711 * @retval -EINVAL Work item is being processed or has completed its work.
2712 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002713 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714 */
2715static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002716 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002718 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719}
2720
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002722 * @brief Get time remaining before a delayed work gets scheduled.
2723 *
2724 * This routine computes the (approximate) time remaining before a
2725 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002726 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002727 *
2728 * @param work Delayed work item.
2729 *
2730 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002731 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002732 */
Kumar Galacc334c72017-04-21 10:55:34 -05002733static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002734{
Andy Ross52e444b2018-09-28 09:06:37 -07002735 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002736}
2737
Anas Nashif166f5192018-02-25 08:02:36 -06002738/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002739/**
Anas Nashifce78d162018-05-24 12:43:11 -05002740 * @defgroup mutex_apis Mutex APIs
2741 * @ingroup kernel_apis
2742 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002743 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744
Anas Nashifce78d162018-05-24 12:43:11 -05002745/**
2746 * Mutex Structure
2747 * @ingroup mutex_apis
2748 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749struct k_mutex {
2750 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002751 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002752 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002753 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755
Anas Nashif2f203c22016-12-18 06:57:45 -05002756 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757};
2758
Anas Nashifce78d162018-05-24 12:43:11 -05002759/**
2760 * @cond INTERNAL_HIDDEN
2761 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002762#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002764 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 .owner = NULL, \
2766 .lock_count = 0, \
2767 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002768 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 }
2770
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002771#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2772
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002773/**
Allan Stephensc98da842016-11-11 15:45:03 -05002774 * INTERNAL_HIDDEN @endcond
2775 */
2776
2777/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002778 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002780 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002781 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002782 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002785 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002786 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002788 struct k_mutex name \
2789 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002790 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002797 * Upon completion, the mutex is available and does not have an owner.
2798 *
2799 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002800 *
2801 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002802 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002803 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002804__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002805
2806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002807 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * This routine locks @a mutex. If the mutex is locked by another thread,
2810 * the calling thread waits until the mutex becomes available or until
2811 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * A thread is permitted to lock a mutex it has already locked. The operation
2814 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * @param mutex Address of the mutex.
2817 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 * or one of the special values K_NO_WAIT and K_FOREVER.
2819 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002820 * @retval 0 Mutex locked.
2821 * @retval -EBUSY Returned without waiting.
2822 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002823 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002824 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002825__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002826
2827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * This routine unlocks @a mutex. The mutex must already be locked by the
2831 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832 *
2833 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002835 * thread.
2836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002837 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002838 *
2839 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002840 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002841 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002842__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843
Allan Stephensc98da842016-11-11 15:45:03 -05002844/**
Anas Nashif166f5192018-02-25 08:02:36 -06002845 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002846 */
2847
2848/**
2849 * @cond INTERNAL_HIDDEN
2850 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002851
2852struct k_sem {
2853 _wait_q_t wait_q;
2854 unsigned int count;
2855 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002856 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002857
Anas Nashif2f203c22016-12-18 06:57:45 -05002858 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859};
2860
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002861#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002862 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002863 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002864 .count = initial_count, \
2865 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002866 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002867 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002868 }
2869
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002870#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2871
Allan Stephensc98da842016-11-11 15:45:03 -05002872/**
2873 * INTERNAL_HIDDEN @endcond
2874 */
2875
2876/**
2877 * @defgroup semaphore_apis Semaphore APIs
2878 * @ingroup kernel_apis
2879 * @{
2880 */
2881
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * @param sem Address of the semaphore.
2888 * @param initial_count Initial semaphore count.
2889 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002890 *
2891 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002892 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002893 */
Andrew Boie99280232017-09-29 14:17:47 -07002894__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2895 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002896
2897/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2903 *
2904 * @param sem Address of the semaphore.
2905 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002906 * or one of the special values K_NO_WAIT and K_FOREVER.
2907 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002908 * @note When porting code from the nanokernel legacy API to the new API, be
2909 * careful with the return value of this function. The return value is the
2910 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2911 * non-zero means failure, while the nano_sem_take family returns 1 for success
2912 * and 0 for failure.
2913 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002914 * @retval 0 Semaphore taken.
2915 * @retval -EBUSY Returned without waiting.
2916 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002917 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002918 */
Andrew Boie99280232017-09-29 14:17:47 -07002919__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002920
2921/**
2922 * @brief Give a semaphore.
2923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002924 * This routine gives @a sem, unless the semaphore is already at its maximum
2925 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002930 *
2931 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002932 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002933 */
Andrew Boie99280232017-09-29 14:17:47 -07002934__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002936/**
2937 * @brief Reset a semaphore's count to zero.
2938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002942 *
2943 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002944 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002945 */
Andrew Boie990bf162017-10-03 12:36:49 -07002946__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002947
Anas Nashif954d5502018-02-25 08:37:28 -06002948/**
2949 * @internal
2950 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002951static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002952{
2953 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002954}
2955
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002956/**
2957 * @brief Get a semaphore's count.
2958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002964 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002965 */
Andrew Boie990bf162017-10-03 12:36:49 -07002966__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002967
Anas Nashif954d5502018-02-25 08:37:28 -06002968/**
2969 * @internal
2970 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002971static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972{
2973 return sem->count;
2974}
2975
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002980 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002981 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @param name Name of the semaphore.
2984 * @param initial_count Initial semaphore count.
2985 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002986 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002987 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002989 struct k_sem name \
2990 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002991 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302992 BUILD_ASSERT(((count_limit) != 0) && \
2993 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002994
Anas Nashif166f5192018-02-25 08:02:36 -06002995/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002996
2997/**
2998 * @defgroup alert_apis Alert APIs
2999 * @ingroup kernel_apis
3000 * @{
3001 */
3002
Allan Stephens5eceb852016-11-16 10:16:30 -05003003/**
3004 * @typedef k_alert_handler_t
3005 * @brief Alert handler function type.
3006 *
3007 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003008 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003009 * and is only invoked if the alert has been initialized with one.
3010 *
3011 * @param alert Address of the alert.
3012 *
3013 * @return 0 if alert has been consumed; non-zero if alert should pend.
3014 */
3015typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003016
Anas Nashif166f5192018-02-25 08:02:36 -06003017/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003018
3019/**
3020 * @cond INTERNAL_HIDDEN
3021 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003022
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003023#define K_ALERT_DEFAULT NULL
3024#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003026struct k_alert {
3027 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028 atomic_t send_count;
3029 struct k_work work_item;
3030 struct k_sem sem;
3031
Anas Nashif2f203c22016-12-18 06:57:45 -05003032 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033};
3034
Anas Nashif954d5502018-02-25 08:37:28 -06003035/**
3036 * @internal
3037 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003038extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003040#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003042 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003043 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003044 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3045 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003046 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047 }
3048
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003049#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3050
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003051/**
Allan Stephensc98da842016-11-11 15:45:03 -05003052 * INTERNAL_HIDDEN @endcond
3053 */
3054
3055/**
3056 * @addtogroup alert_apis
3057 * @{
3058 */
3059
3060/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003061 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003064 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003065 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003066 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003067 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * @param name Name of the alert.
3070 * @param alert_handler Action to take when alert is sent. Specify either
3071 * the address of a function to be invoked by the system workqueue
3072 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3073 * K_ALERT_DEFAULT (which causes the alert to pend).
3074 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003075 *
3076 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003077 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003078#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003079 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003080 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003081 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003082 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003084/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003087 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @param alert Address of the alert.
3090 * @param handler Action to take when alert is sent. Specify either the address
3091 * of a function to be invoked by the system workqueue thread,
3092 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3093 * K_ALERT_DEFAULT (which causes the alert to pend).
3094 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003095 *
3096 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003097 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003098 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003099extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3100 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003101
3102/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003103 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003105 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003107 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3108 *
3109 * @param alert Address of the alert.
3110 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003111 * or one of the special values K_NO_WAIT and K_FOREVER.
3112 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003113 * @retval 0 Alert received.
3114 * @retval -EBUSY Returned without waiting.
3115 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003116 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003117 */
Andrew Boie310e9872017-09-29 04:41:15 -07003118__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119
3120/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * This routine signals @a alert. The action specified for @a alert will
3124 * be taken, which may trigger the execution of an alert handler function
3125 * and/or cause the alert to pend (assuming the alert has not reached its
3126 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003128 * @note Can be called by ISRs.
3129 *
3130 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 *
3132 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003133 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003134 */
Andrew Boie310e9872017-09-29 04:41:15 -07003135__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136
3137/**
Anas Nashif166f5192018-02-25 08:02:36 -06003138 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139 */
3140
Allan Stephensc98da842016-11-11 15:45:03 -05003141/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003142 * @defgroup msgq_apis Message Queue APIs
3143 * @ingroup kernel_apis
3144 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003145 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003147/**
3148 * @brief Message Queue Structure
3149 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150struct k_msgq {
3151 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003152 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003153 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154 char *buffer_start;
3155 char *buffer_end;
3156 char *read_ptr;
3157 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003158 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003159
Anas Nashif2f203c22016-12-18 06:57:45 -05003160 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003161 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003163/**
3164 * @cond INTERNAL_HIDDEN
3165 */
3166
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003167
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003168#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003169 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003170 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003171 .max_msgs = q_max_msgs, \
3172 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003174 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175 .read_ptr = q_buffer, \
3176 .write_ptr = q_buffer, \
3177 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003178 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003180#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003181/**
3182 * INTERNAL_HIDDEN @endcond
3183 */
3184
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003185
Andrew Boie0fe789f2018-04-12 18:35:56 -07003186#define K_MSGQ_FLAG_ALLOC BIT(0)
3187
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003188/**
3189 * @brief Message Queue Attributes
3190 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303191struct k_msgq_attrs {
3192 size_t msg_size;
3193 u32_t max_msgs;
3194 u32_t used_msgs;
3195};
3196
Allan Stephensc98da842016-11-11 15:45:03 -05003197
3198/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3202 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003203 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3204 * message is similarly aligned to this boundary, @a q_msg_size must also be
3205 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * The message queue can be accessed outside the module where it is defined
3208 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003209 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003210 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * @param q_name Name of the message queue.
3213 * @param q_msg_size Message size (in bytes).
3214 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003215 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003216 *
3217 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003218 */
3219#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003220 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003221 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003222 struct k_msgq q_name \
3223 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003224 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003225 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226
Peter Mitsisd7a37502016-10-13 11:37:40 -04003227/**
3228 * @brief Initialize a message queue.
3229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003230 * This routine initializes a message queue object, prior to its first use.
3231 *
Allan Stephensda827222016-11-09 14:23:58 -06003232 * The message queue's ring buffer must contain space for @a max_msgs messages,
3233 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3234 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3235 * that each message is similarly aligned to this boundary, @a q_msg_size
3236 * must also be a multiple of N.
3237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * @param q Address of the message queue.
3239 * @param buffer Pointer to ring buffer that holds queued messages.
3240 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003241 * @param max_msgs Maximum number of messages that can be queued.
3242 *
3243 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003244 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003245 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003246void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3247 u32_t max_msgs);
3248
3249/**
3250 * @brief Initialize a message queue.
3251 *
3252 * This routine initializes a message queue object, prior to its first use,
3253 * allocating its internal ring buffer from the calling thread's resource
3254 * pool.
3255 *
3256 * Memory allocated for the ring buffer can be released by calling
3257 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3258 * all of its references.
3259 *
3260 * @param q Address of the message queue.
3261 * @param msg_size Message size (in bytes).
3262 * @param max_msgs Maximum number of messages that can be queued.
3263 *
3264 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3265 * thread's resource pool, or -EINVAL if the size parameters cause
3266 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003267 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003268 */
3269__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3270 u32_t max_msgs);
3271
3272
3273void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003274
3275/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003279 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003280 * @note Can be called by ISRs.
3281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @param q Address of the message queue.
3283 * @param data Pointer to the message.
3284 * @param timeout Waiting period to add the message (in milliseconds),
3285 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003286 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003287 * @retval 0 Message sent.
3288 * @retval -ENOMSG Returned without waiting or queue purged.
3289 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003290 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003291 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003292__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003293
3294/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * This routine receives a message from message queue @a q in a "first in,
3298 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003299 *
Allan Stephensc98da842016-11-11 15:45:03 -05003300 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * @param q Address of the message queue.
3303 * @param data Address of area to hold the received message.
3304 * @param timeout Waiting period to receive the message (in milliseconds),
3305 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003306 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003307 * @retval 0 Message received.
3308 * @retval -ENOMSG Returned without waiting.
3309 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003310 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003311 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003312__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003313
3314/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003317 * This routine discards all unreceived messages in a message queue's ring
3318 * buffer. Any threads that are blocked waiting to send a message to the
3319 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003321 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003322 *
3323 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003324 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003325 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003326__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327
Peter Mitsis67be2492016-10-07 11:44:34 -04003328/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * This routine returns the number of unused entries in a message queue's
3332 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * @param q Address of the message queue.
3335 *
3336 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003337 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003338 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003339__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3340
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303341/**
3342 * @brief Get basic attributes of a message queue.
3343 *
3344 * This routine fetches basic attributes of message queue into attr argument.
3345 *
3346 * @param q Address of the message queue.
3347 * @param attrs pointer to message queue attribute structure.
3348 *
3349 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003350 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303351 */
3352__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3353
3354
Andrew Boie82edb6e2017-10-02 10:53:06 -07003355static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003356{
3357 return q->max_msgs - q->used_msgs;
3358}
3359
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * @param q Address of the message queue.
3366 *
3367 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003368 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003369 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003370__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3371
3372static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003373{
3374 return q->used_msgs;
3375}
3376
Anas Nashif166f5192018-02-25 08:02:36 -06003377/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003378
3379/**
3380 * @defgroup mem_pool_apis Memory Pool APIs
3381 * @ingroup kernel_apis
3382 * @{
3383 */
3384
Andy Ross73cb9582017-05-09 10:42:39 -07003385/* Note on sizing: the use of a 20 bit field for block means that,
3386 * assuming a reasonable minimum block size of 16 bytes, we're limited
3387 * to 16M of memory managed by a single pool. Long term it would be
3388 * good to move to a variable bit size based on configuration.
3389 */
3390struct k_mem_block_id {
3391 u32_t pool : 8;
3392 u32_t level : 4;
3393 u32_t block : 20;
3394};
3395
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003398 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399};
3400
Anas Nashif166f5192018-02-25 08:02:36 -06003401/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003402
3403/**
3404 * @defgroup mailbox_apis Mailbox APIs
3405 * @ingroup kernel_apis
3406 * @{
3407 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408
3409struct k_mbox_msg {
3410 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003411 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003412 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003413 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003415 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416 /** sender's message data buffer */
3417 void *tx_data;
3418 /** internal use only - needed for legacy API support */
3419 void *_rx_data;
3420 /** message data block descriptor */
3421 struct k_mem_block tx_block;
3422 /** source thread id */
3423 k_tid_t rx_source_thread;
3424 /** target thread id */
3425 k_tid_t tx_target_thread;
3426 /** internal use only - thread waiting on send (may be a dummy) */
3427 k_tid_t _syncing_thread;
3428#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3429 /** internal use only - semaphore used during asynchronous send */
3430 struct k_sem *_async_sem;
3431#endif
3432};
3433
3434struct k_mbox {
3435 _wait_q_t tx_msg_queue;
3436 _wait_q_t rx_msg_queue;
3437
Anas Nashif2f203c22016-12-18 06:57:45 -05003438 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003440/**
3441 * @cond INTERNAL_HIDDEN
3442 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003443
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003444#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003445 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003446 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3447 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003448 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003449 }
3450
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003451#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3452
Peter Mitsis12092702016-10-14 12:57:23 -04003453/**
Allan Stephensc98da842016-11-11 15:45:03 -05003454 * INTERNAL_HIDDEN @endcond
3455 */
3456
3457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003461 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003462 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003464 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003465 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003466 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003468 struct k_mbox name \
3469 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003470 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471
Peter Mitsis12092702016-10-14 12:57:23 -04003472/**
3473 * @brief Initialize a mailbox.
3474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * This routine initializes a mailbox object, prior to its first use.
3476 *
3477 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003478 *
3479 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003480 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482extern void k_mbox_init(struct k_mbox *mbox);
3483
Peter Mitsis12092702016-10-14 12:57:23 -04003484/**
3485 * @brief Send a mailbox message in a synchronous manner.
3486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * This routine sends a message to @a mbox and waits for a receiver to both
3488 * receive and process it. The message data may be in a buffer, in a memory
3489 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * @param mbox Address of the mailbox.
3492 * @param tx_msg Address of the transmit message descriptor.
3493 * @param timeout Waiting period for the message to be received (in
3494 * milliseconds), or one of the special values K_NO_WAIT
3495 * and K_FOREVER. Once the message has been received,
3496 * this routine waits as long as necessary for the message
3497 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003498 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003499 * @retval 0 Message sent.
3500 * @retval -ENOMSG Returned without waiting.
3501 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003502 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003503 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003504extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003505 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003506
Peter Mitsis12092702016-10-14 12:57:23 -04003507/**
3508 * @brief Send a mailbox message in an asynchronous manner.
3509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * This routine sends a message to @a mbox without waiting for a receiver
3511 * to process it. The message data may be in a buffer, in a memory pool block,
3512 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3513 * will be given when the message has been both received and completely
3514 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * @param mbox Address of the mailbox.
3517 * @param tx_msg Address of the transmit message descriptor.
3518 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003519 *
3520 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003521 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003522 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003523extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003524 struct k_sem *sem);
3525
Peter Mitsis12092702016-10-14 12:57:23 -04003526/**
3527 * @brief Receive a mailbox message.
3528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * This routine receives a message from @a mbox, then optionally retrieves
3530 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * @param mbox Address of the mailbox.
3533 * @param rx_msg Address of the receive message descriptor.
3534 * @param buffer Address of the buffer to receive data, or NULL to defer data
3535 * retrieval and message disposal until later.
3536 * @param timeout Waiting period for a message to be received (in
3537 * milliseconds), or one of the special values K_NO_WAIT
3538 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003539 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003540 * @retval 0 Message received.
3541 * @retval -ENOMSG Returned without waiting.
3542 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003543 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003544 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003545extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003546 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003547
3548/**
3549 * @brief Retrieve mailbox message data into a buffer.
3550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * This routine completes the processing of a received message by retrieving
3552 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003553 *
3554 * Alternatively, this routine can be used to dispose of a received message
3555 * without retrieving its data.
3556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * @param rx_msg Address of the receive message descriptor.
3558 * @param buffer Address of the buffer to receive data, or NULL to discard
3559 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003560 *
3561 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003562 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003563 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003564extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003565
3566/**
3567 * @brief Retrieve mailbox message data into a memory pool block.
3568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * This routine completes the processing of a received message by retrieving
3570 * its data into a memory pool block, then disposing of the message.
3571 * The memory pool block that results from successful retrieval must be
3572 * returned to the pool once the data has been processed, even in cases
3573 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003574 *
3575 * Alternatively, this routine can be used to dispose of a received message
3576 * without retrieving its data. In this case there is no need to return a
3577 * memory pool block to the pool.
3578 *
3579 * This routine allocates a new memory pool block for the data only if the
3580 * data is not already in one. If a new block cannot be allocated, the routine
3581 * returns a failure code and the received message is left unchanged. This
3582 * permits the caller to reattempt data retrieval at a later time or to dispose
3583 * of the received message without retrieving its data.
3584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @param rx_msg Address of a receive message descriptor.
3586 * @param pool Address of memory pool, or NULL to discard data.
3587 * @param block Address of the area to hold memory pool block info.
3588 * @param timeout Waiting period to wait for a memory pool block (in
3589 * milliseconds), or one of the special values K_NO_WAIT
3590 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003591 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003592 * @retval 0 Data retrieved.
3593 * @retval -ENOMEM Returned without waiting.
3594 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003595 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003596 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003597extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003598 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003599 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003600
Anas Nashif166f5192018-02-25 08:02:36 -06003601/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003602
3603/**
Anas Nashifce78d162018-05-24 12:43:11 -05003604 * @defgroup pipe_apis Pipe APIs
3605 * @ingroup kernel_apis
3606 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003607 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003608
Anas Nashifce78d162018-05-24 12:43:11 -05003609/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003610struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003611 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3612 size_t size; /**< Buffer size */
3613 size_t bytes_used; /**< # bytes used in buffer */
3614 size_t read_index; /**< Where in buffer to read from */
3615 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616
3617 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003618 _wait_q_t readers; /**< Reader wait queue */
3619 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003620 } wait_q;
3621
Anas Nashif2f203c22016-12-18 06:57:45 -05003622 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003623 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003624};
3625
Anas Nashifce78d162018-05-24 12:43:11 -05003626/**
3627 * @cond INTERNAL_HIDDEN
3628 */
3629#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3630
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003631#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003632 { \
3633 .buffer = pipe_buffer, \
3634 .size = pipe_buffer_size, \
3635 .bytes_used = 0, \
3636 .read_index = 0, \
3637 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003638 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3639 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003640 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003641 }
3642
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003643#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3644
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003645/**
Allan Stephensc98da842016-11-11 15:45:03 -05003646 * INTERNAL_HIDDEN @endcond
3647 */
3648
3649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003653 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003654 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @param name Name of the pipe.
3657 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3658 * or zero if no ring buffer is used.
3659 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003660 *
3661 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003662 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003663#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3664 static unsigned char __kernel_noinit __aligned(pipe_align) \
3665 _k_pipe_buf_##name[pipe_buffer_size]; \
3666 struct k_pipe name \
3667 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003668 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003669
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003671 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003673 * This routine initializes a pipe object, prior to its first use.
3674 *
3675 * @param pipe Address of the pipe.
3676 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3677 * is used.
3678 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3679 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680 *
3681 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003682 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003683 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003684void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3685
3686/**
3687 * @brief Release a pipe's allocated buffer
3688 *
3689 * If a pipe object was given a dynamically allocated buffer via
3690 * k_pipe_alloc_init(), this will free it. This function does nothing
3691 * if the buffer wasn't dynamically allocated.
3692 *
3693 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003694 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003695 */
3696void k_pipe_cleanup(struct k_pipe *pipe);
3697
3698/**
3699 * @brief Initialize a pipe and allocate a buffer for it
3700 *
3701 * Storage for the buffer region will be allocated from the calling thread's
3702 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3703 * or userspace is enabled and the pipe object loses all references to it.
3704 *
3705 * This function should only be called on uninitialized pipe objects.
3706 *
3707 * @param pipe Address of the pipe.
3708 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3709 * buffer is used.
3710 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003711 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003712 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003713 */
3714__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715
3716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003719 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * @param pipe Address of the pipe.
3722 * @param data Address of data to write.
3723 * @param bytes_to_write Size of data (in bytes).
3724 * @param bytes_written Address of area to hold the number of bytes written.
3725 * @param min_xfer Minimum number of bytes to write.
3726 * @param timeout Waiting period to wait for the data to be written (in
3727 * milliseconds), or one of the special values K_NO_WAIT
3728 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003730 * @retval 0 At least @a min_xfer bytes of data were written.
3731 * @retval -EIO Returned without waiting; zero data bytes were written.
3732 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003733 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003734 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003736__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3737 size_t bytes_to_write, size_t *bytes_written,
3738 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739
3740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003743 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003745 * @param pipe Address of the pipe.
3746 * @param data Address to place the data read from pipe.
3747 * @param bytes_to_read Maximum number of data bytes to read.
3748 * @param bytes_read Address of area to hold the number of bytes read.
3749 * @param min_xfer Minimum number of data bytes to read.
3750 * @param timeout Waiting period to wait for the data to be read (in
3751 * milliseconds), or one of the special values K_NO_WAIT
3752 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003753 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003754 * @retval 0 At least @a min_xfer bytes of data were read.
3755 * @retval -EIO Returned without waiting; zero data bytes were read.
3756 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003757 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003758 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003759 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003760__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3761 size_t bytes_to_read, size_t *bytes_read,
3762 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003763
3764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003767 * This routine writes the data contained in a memory block to @a pipe.
3768 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003769 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003771 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003772 * @param block Memory block containing data to send
3773 * @param size Number of data bytes in memory block to send
3774 * @param sem Semaphore to signal upon completion (else NULL)
3775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003776 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003777 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003778 */
3779extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3780 size_t size, struct k_sem *sem);
3781
Anas Nashif166f5192018-02-25 08:02:36 -06003782/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003783
Allan Stephensc98da842016-11-11 15:45:03 -05003784/**
3785 * @cond INTERNAL_HIDDEN
3786 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003788struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003789 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003790 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003791 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792 char *buffer;
3793 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003794 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003795
Anas Nashif2f203c22016-12-18 06:57:45 -05003796 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797};
3798
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003799#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003800 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003802 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003803 .num_blocks = slab_num_blocks, \
3804 .block_size = slab_block_size, \
3805 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003806 .free_list = NULL, \
3807 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003808 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809 }
3810
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003811#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3812
3813
Peter Mitsis578f9112016-10-07 13:50:31 -04003814/**
Allan Stephensc98da842016-11-11 15:45:03 -05003815 * INTERNAL_HIDDEN @endcond
3816 */
3817
3818/**
3819 * @defgroup mem_slab_apis Memory Slab APIs
3820 * @ingroup kernel_apis
3821 * @{
3822 */
3823
3824/**
Allan Stephensda827222016-11-09 14:23:58 -06003825 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003826 *
Allan Stephensda827222016-11-09 14:23:58 -06003827 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003828 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003829 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3830 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003831 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003832 *
Allan Stephensda827222016-11-09 14:23:58 -06003833 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003834 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003835 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003836 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003838 * @param name Name of the memory slab.
3839 * @param slab_block_size Size of each memory block (in bytes).
3840 * @param slab_num_blocks Number memory blocks.
3841 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003842 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003843 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003844#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3845 char __noinit __aligned(slab_align) \
3846 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3847 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003848 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003849 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003850 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003851
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003852/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003853 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003856 *
Allan Stephensda827222016-11-09 14:23:58 -06003857 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3858 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3859 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3860 * To ensure that each memory block is similarly aligned to this boundary,
3861 * @a slab_block_size must also be a multiple of N.
3862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003863 * @param slab Address of the memory slab.
3864 * @param buffer Pointer to buffer used for the memory blocks.
3865 * @param block_size Size of each memory block (in bytes).
3866 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003867 *
3868 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003869 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003870 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003871extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003872 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003873
3874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003875 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003877 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003879 * @param slab Address of the memory slab.
3880 * @param mem Pointer to block address area.
3881 * @param timeout Maximum time to wait for operation to complete
3882 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3883 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003884 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003885 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003886 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003887 * @retval -ENOMEM Returned without waiting.
3888 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003889 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003890 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003891extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003892 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003893
3894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003897 * This routine releases a previously allocated memory block back to its
3898 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003900 * @param slab Address of the memory slab.
3901 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003902 *
3903 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003904 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003905 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003906extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003907
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003909 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003911 * This routine gets the number of memory blocks that are currently
3912 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003914 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003916 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003917 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003918 */
Kumar Galacc334c72017-04-21 10:55:34 -05003919static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003920{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003921 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003922}
3923
Peter Mitsisc001aa82016-10-13 13:53:37 -04003924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003925 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003927 * This routine gets the number of memory blocks that are currently
3928 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003930 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003933 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003934 */
Kumar Galacc334c72017-04-21 10:55:34 -05003935static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003936{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003937 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003938}
3939
Anas Nashif166f5192018-02-25 08:02:36 -06003940/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003941
3942/**
3943 * @cond INTERNAL_HIDDEN
3944 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003945
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003946struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003947 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003948 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003949};
3950
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003951/**
Allan Stephensc98da842016-11-11 15:45:03 -05003952 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003953 */
3954
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003955/**
Allan Stephensc98da842016-11-11 15:45:03 -05003956 * @addtogroup mem_pool_apis
3957 * @{
3958 */
3959
3960/**
3961 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3964 * long. The memory pool allows blocks to be repeatedly partitioned into
3965 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003966 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003967 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003968 * If the pool is to be accessed outside the module where it is defined, it
3969 * can be declared via
3970 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003971 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003973 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003974 * @param minsz Size of the smallest blocks in the pool (in bytes).
3975 * @param maxsz Size of the largest blocks in the pool (in bytes).
3976 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003977 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003978 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003979 */
Andy Ross73cb9582017-05-09 10:42:39 -07003980#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3981 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3982 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003983 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003984 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003985 .base = { \
3986 .buf = _mpool_buf_##name, \
3987 .max_sz = maxsz, \
3988 .n_max = nmax, \
3989 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3990 .levels = _mpool_lvls_##name, \
3991 .flags = SYS_MEM_POOL_KERNEL \
3992 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003993 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003994
Peter Mitsis937042c2016-10-13 13:18:26 -04003995/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003998 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004000 * @param pool Address of the memory pool.
4001 * @param block Pointer to block descriptor for the allocated memory.
4002 * @param size Amount of memory to allocate (in bytes).
4003 * @param timeout Maximum time to wait for operation to complete
4004 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4005 * or K_FOREVER to wait as long as necessary.
4006 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004007 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004008 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004009 * @retval -ENOMEM Returned without waiting.
4010 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004011 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004012 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004013extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004014 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004015
4016/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004017 * @brief Allocate memory from a memory pool with malloc() semantics
4018 *
4019 * Such memory must be released using k_free().
4020 *
4021 * @param pool Address of the memory pool.
4022 * @param size Amount of memory to allocate (in bytes).
4023 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004024 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004025 */
4026extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4027
4028/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004029 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004031 * This routine releases a previously allocated memory block back to its
4032 * memory pool.
4033 *
4034 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004035 *
4036 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004037 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004038 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004039extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004040
4041/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004042 * @brief Free memory allocated from a memory pool.
4043 *
4044 * This routine releases a previously allocated memory block back to its
4045 * memory pool
4046 *
4047 * @param id Memory block identifier.
4048 *
4049 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004050 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004051 */
4052extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4053
4054/**
Anas Nashif166f5192018-02-25 08:02:36 -06004055 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004056 */
4057
4058/**
4059 * @defgroup heap_apis Heap Memory Pool APIs
4060 * @ingroup kernel_apis
4061 * @{
4062 */
4063
4064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004065 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004067 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004068 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004070 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004072 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004073 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004074 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004075extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004076
4077/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004078 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004079 *
4080 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004081 * returned must have been allocated from the heap memory pool or
4082 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004083 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004084 * If @a ptr is NULL, no operation is performed.
4085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004086 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004087 *
4088 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004089 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004090 */
4091extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004092
Allan Stephensc98da842016-11-11 15:45:03 -05004093/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004094 * @brief Allocate memory from heap, array style
4095 *
4096 * This routine provides traditional calloc() semantics. Memory is
4097 * allocated from the heap memory pool and zeroed.
4098 *
4099 * @param nmemb Number of elements in the requested array
4100 * @param size Size of each array element (in bytes).
4101 *
4102 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004103 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004104 */
4105extern void *k_calloc(size_t nmemb, size_t size);
4106
Anas Nashif166f5192018-02-25 08:02:36 -06004107/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004108
Benjamin Walshacc68c12017-01-29 18:57:45 -05004109/* polling API - PRIVATE */
4110
Benjamin Walshb0179862017-02-02 16:39:57 -05004111#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004112#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004113#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004114#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004115#endif
4116
Benjamin Walshacc68c12017-01-29 18:57:45 -05004117/* private - implementation data created as needed, per-type */
4118struct _poller {
4119 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004120 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004121};
4122
4123/* private - types bit positions */
4124enum _poll_types_bits {
4125 /* can be used to ignore an event */
4126 _POLL_TYPE_IGNORE,
4127
4128 /* to be signaled by k_poll_signal() */
4129 _POLL_TYPE_SIGNAL,
4130
4131 /* semaphore availability */
4132 _POLL_TYPE_SEM_AVAILABLE,
4133
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004134 /* queue/fifo/lifo data availability */
4135 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004136
4137 _POLL_NUM_TYPES
4138};
4139
4140#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4141
4142/* private - states bit positions */
4143enum _poll_states_bits {
4144 /* default state when creating event */
4145 _POLL_STATE_NOT_READY,
4146
Benjamin Walshacc68c12017-01-29 18:57:45 -05004147 /* signaled by k_poll_signal() */
4148 _POLL_STATE_SIGNALED,
4149
4150 /* semaphore is available */
4151 _POLL_STATE_SEM_AVAILABLE,
4152
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004153 /* data is available to read on queue/fifo/lifo */
4154 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004155
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004156 /* queue/fifo/lifo wait was cancelled */
4157 _POLL_STATE_CANCELLED,
4158
Benjamin Walshacc68c12017-01-29 18:57:45 -05004159 _POLL_NUM_STATES
4160};
4161
4162#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4163
4164#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004165 (32 - (0 \
4166 + 8 /* tag */ \
4167 + _POLL_NUM_TYPES \
4168 + _POLL_NUM_STATES \
4169 + 1 /* modes */ \
4170 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004171
Benjamin Walshacc68c12017-01-29 18:57:45 -05004172/* end of polling API - PRIVATE */
4173
4174
4175/**
4176 * @defgroup poll_apis Async polling APIs
4177 * @ingroup kernel_apis
4178 * @{
4179 */
4180
4181/* Public polling API */
4182
4183/* public - values for k_poll_event.type bitfield */
4184#define K_POLL_TYPE_IGNORE 0
4185#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4186#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004187#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4188#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004189
4190/* public - polling modes */
4191enum k_poll_modes {
4192 /* polling thread does not take ownership of objects when available */
4193 K_POLL_MODE_NOTIFY_ONLY = 0,
4194
4195 K_POLL_NUM_MODES
4196};
4197
4198/* public - values for k_poll_event.state bitfield */
4199#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004200#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4201#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004202#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4203#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004204#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004205
4206/* public - poll signal object */
4207struct k_poll_signal {
4208 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004209 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004210
4211 /*
4212 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4213 * user resets it to 0.
4214 */
4215 unsigned int signaled;
4216
4217 /* custom result value passed to k_poll_signal() if needed */
4218 int result;
4219};
4220
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004221#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004222 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004223 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004224 .signaled = 0, \
4225 .result = 0, \
4226 }
4227
4228struct k_poll_event {
4229 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004230 sys_dnode_t _node;
4231
4232 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004233 struct _poller *poller;
4234
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004235 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004236 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004237
Benjamin Walshacc68c12017-01-29 18:57:45 -05004238 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004239 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004240
4241 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004242 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004243
4244 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004245 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004246
4247 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004248 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249
4250 /* per-type data */
4251 union {
4252 void *obj;
4253 struct k_poll_signal *signal;
4254 struct k_sem *sem;
4255 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004256 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004257 };
4258};
4259
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004260#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261 { \
4262 .poller = NULL, \
4263 .type = event_type, \
4264 .state = K_POLL_STATE_NOT_READY, \
4265 .mode = event_mode, \
4266 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004267 { .obj = event_obj }, \
4268 }
4269
4270#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4271 event_tag) \
4272 { \
4273 .type = event_type, \
4274 .tag = event_tag, \
4275 .state = K_POLL_STATE_NOT_READY, \
4276 .mode = event_mode, \
4277 .unused = 0, \
4278 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004279 }
4280
4281/**
4282 * @brief Initialize one struct k_poll_event instance
4283 *
4284 * After this routine is called on a poll event, the event it ready to be
4285 * placed in an event array to be passed to k_poll().
4286 *
4287 * @param event The event to initialize.
4288 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4289 * values. Only values that apply to the same object being polled
4290 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4291 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004292 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293 * @param obj Kernel object or poll signal.
4294 *
4295 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004296 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297 */
4298
Kumar Galacc334c72017-04-21 10:55:34 -05004299extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004300 int mode, void *obj);
4301
4302/**
4303 * @brief Wait for one or many of multiple poll events to occur
4304 *
4305 * This routine allows a thread to wait concurrently for one or many of
4306 * multiple poll events to have occurred. Such events can be a kernel object
4307 * being available, like a semaphore, or a poll signal event.
4308 *
4309 * When an event notifies that a kernel object is available, the kernel object
4310 * is not "given" to the thread calling k_poll(): it merely signals the fact
4311 * that the object was available when the k_poll() call was in effect. Also,
4312 * all threads trying to acquire an object the regular way, i.e. by pending on
4313 * the object, have precedence over the thread polling on the object. This
4314 * means that the polling thread will never get the poll event on an object
4315 * until the object becomes available and its pend queue is empty. For this
4316 * reason, the k_poll() call is more effective when the objects being polled
4317 * only have one thread, the polling thread, trying to acquire them.
4318 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004319 * When k_poll() returns 0, the caller should loop on all the events that were
4320 * passed to k_poll() and check the state field for the values that were
4321 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004322 *
4323 * Before being reused for another call to k_poll(), the user has to reset the
4324 * state field to K_POLL_STATE_NOT_READY.
4325 *
Andrew Boie3772f772018-05-07 16:52:57 -07004326 * When called from user mode, a temporary memory allocation is required from
4327 * the caller's resource pool.
4328 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004329 * @param events An array of pointers to events to be polled for.
4330 * @param num_events The number of events in the array.
4331 * @param timeout Waiting period for an event to be ready (in milliseconds),
4332 * or one of the special values K_NO_WAIT and K_FOREVER.
4333 *
4334 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004335 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004336 * @retval -EINTR Polling has been interrupted, e.g. with
4337 * k_queue_cancel_wait(). All output events are still set and valid,
4338 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4339 * words, -EINTR status means that at least one of output events is
4340 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004341 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4342 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004343 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004344 */
4345
Andrew Boie3772f772018-05-07 16:52:57 -07004346__syscall int k_poll(struct k_poll_event *events, int num_events,
4347 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004348
4349/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004350 * @brief Initialize a poll signal object.
4351 *
4352 * Ready a poll signal object to be signaled via k_poll_signal().
4353 *
4354 * @param signal A poll signal.
4355 *
4356 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004357 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004358 */
4359
Andrew Boie3772f772018-05-07 16:52:57 -07004360__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4361
4362/*
4363 * @brief Reset a poll signal object's state to unsignaled.
4364 *
4365 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004366 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004367 */
4368__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4369
4370static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4371{
4372 signal->signaled = 0;
4373}
4374
4375/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004376 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004377 *
4378 * @param signal A poll signal object
4379 * @param signaled An integer buffer which will be written nonzero if the
4380 * object was signaled
4381 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004382 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004383 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004384 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004385 */
4386__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4387 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004388
4389/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004390 * @brief Signal a poll signal object.
4391 *
4392 * This routine makes ready a poll signal, which is basically a poll event of
4393 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4394 * made ready to run. A @a result value can be specified.
4395 *
4396 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004397 * k_poll_signal(), stays set until the user sets it back to 0 with
4398 * k_poll_signal_reset(). It thus has to be reset by the user before being
4399 * passed again to k_poll() or k_poll() will consider it being signaled, and
4400 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004401 *
4402 * @param signal A poll signal.
4403 * @param result The value to store in the result field of the signal.
4404 *
4405 * @retval 0 The signal was delivered successfully.
4406 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004407 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004408 */
4409
Andrew Boie3772f772018-05-07 16:52:57 -07004410__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004411
Anas Nashif954d5502018-02-25 08:37:28 -06004412/**
4413 * @internal
4414 */
Andy Ross8606fab2018-03-26 10:54:40 -07004415extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004416
Anas Nashif166f5192018-02-25 08:02:36 -06004417/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004418
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004419/**
4420 * @brief Make the CPU idle.
4421 *
4422 * This function makes the CPU idle until an event wakes it up.
4423 *
4424 * In a regular system, the idle thread should be the only thread responsible
4425 * for making the CPU idle and triggering any type of power management.
4426 * However, in some more constrained systems, such as a single-threaded system,
4427 * the only thread would be responsible for this if needed.
4428 *
4429 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004430 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004431 */
4432extern void k_cpu_idle(void);
4433
4434/**
4435 * @brief Make the CPU idle in an atomic fashion.
4436 *
4437 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4438 * must be done atomically before making the CPU idle.
4439 *
4440 * @param key Interrupt locking key obtained from irq_lock().
4441 *
4442 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004443 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004444 */
4445extern void k_cpu_atomic_idle(unsigned int key);
4446
Anas Nashif954d5502018-02-25 08:37:28 -06004447
4448/**
4449 * @internal
4450 */
Kumar Galacc334c72017-04-21 10:55:34 -05004451extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004452
Andrew Boiecdb94d62017-04-18 15:22:05 -07004453#ifdef _ARCH_EXCEPT
4454/* This archtecture has direct support for triggering a CPU exception */
4455#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4456#else
4457
Andrew Boiecdb94d62017-04-18 15:22:05 -07004458/* NOTE: This is the implementation for arches that do not implement
4459 * _ARCH_EXCEPT() to generate a real CPU exception.
4460 *
4461 * We won't have a real exception frame to determine the PC value when
4462 * the oops occurred, so print file and line number before we jump into
4463 * the fatal error handler.
4464 */
4465#define _k_except_reason(reason) do { \
4466 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4467 _NanoFatalErrorHandler(reason, &_default_esf); \
4468 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004469 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004470
4471#endif /* _ARCH__EXCEPT */
4472
4473/**
4474 * @brief Fatally terminate a thread
4475 *
4476 * This should be called when a thread has encountered an unrecoverable
4477 * runtime condition and needs to terminate. What this ultimately
4478 * means is determined by the _fatal_error_handler() implementation, which
4479 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4480 *
4481 * If this is called from ISR context, the default system fatal error handler
4482 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004483 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004484 */
4485#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4486
4487/**
4488 * @brief Fatally terminate the system
4489 *
4490 * This should be called when the Zephyr kernel has encountered an
4491 * unrecoverable runtime condition and needs to terminate. What this ultimately
4492 * means is determined by the _fatal_error_handler() implementation, which
4493 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004494 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004495 */
4496#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4497
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004498/*
4499 * private APIs that are utilized by one or more public APIs
4500 */
4501
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004502#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004503/**
4504 * @internal
4505 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004506extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004507#else
Anas Nashif954d5502018-02-25 08:37:28 -06004508/**
4509 * @internal
4510 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004511#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004512#endif
4513
Anas Nashif954d5502018-02-25 08:37:28 -06004514/**
4515 * @internal
4516 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004517extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004518/**
4519 * @internal
4520 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004521extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004522
Andrew Boiedc5d9352017-06-02 12:56:47 -07004523/* arch/cpu.h may declare an architecture or platform-specific macro
4524 * for properly declaring stacks, compatible with MMU/MPU constraints if
4525 * enabled
4526 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004527
4528/**
4529 * @brief Obtain an extern reference to a stack
4530 *
4531 * This macro properly brings the symbol of a thread stack declared
4532 * elsewhere into scope.
4533 *
4534 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004535 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004536 */
4537#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4538
Andrew Boiedc5d9352017-06-02 12:56:47 -07004539#ifdef _ARCH_THREAD_STACK_DEFINE
4540#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4541#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4542 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304543#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004544#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4545#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004546static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004547{
4548 return _ARCH_THREAD_STACK_BUFFER(sym);
4549}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004550#else
4551/**
4552 * @brief Declare a toplevel thread stack memory region
4553 *
4554 * This declares a region of memory suitable for use as a thread's stack.
4555 *
4556 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4557 * 'noinit' section so that it isn't zeroed at boot
4558 *
Andrew Boie507852a2017-07-25 18:47:07 -07004559 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004560 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004561 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004562 *
4563 * It is legal to precede this definition with the 'static' keyword.
4564 *
4565 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4566 * parameter of k_thread_create(), it may not be the same as the
4567 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4568 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004569 * Some arches may round the size of the usable stack region up to satisfy
4570 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4571 * size.
4572 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004573 * @param sym Thread stack symbol name
4574 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004575 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004576 */
4577#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004578 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004579
4580/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304581 * @brief Calculate size of stacks to be allocated in a stack array
4582 *
4583 * This macro calculates the size to be allocated for the stacks
4584 * inside a stack array. It accepts the indicated "size" as a parameter
4585 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4586 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4587 *
4588 * @param size Size of the stack memory region
4589 * @req K-TSTACK-001
4590 */
4591#define K_THREAD_STACK_LEN(size) (size)
4592
4593/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004594 * @brief Declare a toplevel array of thread stack memory regions
4595 *
4596 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4597 * definition for additional details and constraints.
4598 *
4599 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4600 * 'noinit' section so that it isn't zeroed at boot
4601 *
4602 * @param sym Thread stack symbol name
4603 * @param nmemb Number of stacks to declare
4604 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004605 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004606 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004607#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004608 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304609 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004610
4611/**
4612 * @brief Declare an embedded stack memory region
4613 *
4614 * Used for stacks embedded within other data structures. Use is highly
4615 * discouraged but in some cases necessary. For memory protection scenarios,
4616 * it is very important that any RAM preceding this member not be writable
4617 * by threads else a stack overflow will lead to silent corruption. In other
4618 * words, the containing data structure should live in RAM owned by the kernel.
4619 *
4620 * @param sym Thread stack symbol name
4621 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004622 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004623 */
4624#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004625 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004626
4627/**
4628 * @brief Return the size in bytes of a stack memory region
4629 *
4630 * Convenience macro for passing the desired stack size to k_thread_create()
4631 * since the underlying implementation may actually create something larger
4632 * (for instance a guard area).
4633 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004634 * The value returned here is not guaranteed to match the 'size' parameter
4635 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004636 *
4637 * @param sym Stack memory symbol
4638 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004639 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004640 */
4641#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4642
4643/**
4644 * @brief Get a pointer to the physical stack buffer
4645 *
4646 * Convenience macro to get at the real underlying stack buffer used by
4647 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4648 * This is really only intended for diagnostic tools which want to examine
4649 * stack memory contents.
4650 *
4651 * @param sym Declared stack symbol name
4652 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004653 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004654 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004655static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004656{
4657 return (char *)sym;
4658}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004659
4660#endif /* _ARCH_DECLARE_STACK */
4661
Chunlin Hane9c97022017-07-07 20:29:30 +08004662/**
4663 * @defgroup mem_domain_apis Memory domain APIs
4664 * @ingroup kernel_apis
4665 * @{
4666 */
4667
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004668/**
4669 * @def MEM_PARTITION_ENTRY
4670 * @brief Used to declare a memory partition entry
4671 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004672 */
4673#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4674 {\
4675 .start = _start, \
4676 .size = _size, \
4677 .attr = _attr, \
4678 }
4679
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004680/**
4681 * @def K_MEM_PARTITION_DEFINE
4682 * @brief Used to declare a memory partition
4683 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004684 */
4685#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4686#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4687 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304688 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004689 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4690#else
4691#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304692 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004693 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4694#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4695
Chunlin Hane9c97022017-07-07 20:29:30 +08004696/* memory partition */
4697struct k_mem_partition {
4698 /* start address of memory partition */
4699 u32_t start;
4700 /* size of memory partition */
4701 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304702#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004703 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304704 k_mem_partition_attr_t attr;
4705#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004706};
4707
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004708/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304709 * Note: Always declare this structure with __kernel prefix
4710 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004711struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304712#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004713 /* partitions in the domain */
4714 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304715#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004716 /* domain q */
4717 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004718 /* number of partitions in the domain */
4719 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004720};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304721
Chunlin Hane9c97022017-07-07 20:29:30 +08004722
4723/**
4724 * @brief Initialize a memory domain.
4725 *
4726 * Initialize a memory domain with given name and memory partitions.
4727 *
4728 * @param domain The memory domain to be initialized.
4729 * @param num_parts The number of array items of "parts" parameter.
4730 * @param parts An array of pointers to the memory partitions. Can be NULL
4731 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004732 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004733 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004734extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004735 struct k_mem_partition *parts[]);
4736/**
4737 * @brief Destroy a memory domain.
4738 *
4739 * Destroy a memory domain.
4740 *
4741 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004742 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004743 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004744extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4745
4746/**
4747 * @brief Add a memory partition into a memory domain.
4748 *
4749 * Add a memory partition into a memory domain.
4750 *
4751 * @param domain The memory domain to be added a memory partition.
4752 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004753 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004754 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004755extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4756 struct k_mem_partition *part);
4757
4758/**
4759 * @brief Remove a memory partition from a memory domain.
4760 *
4761 * Remove a memory partition from a memory domain.
4762 *
4763 * @param domain The memory domain to be removed a memory partition.
4764 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004765 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004766 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004767extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4768 struct k_mem_partition *part);
4769
4770/**
4771 * @brief Add a thread into a memory domain.
4772 *
4773 * Add a thread into a memory domain.
4774 *
4775 * @param domain The memory domain that the thread is going to be added into.
4776 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004777 *
4778 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004779 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004780extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4781 k_tid_t thread);
4782
4783/**
4784 * @brief Remove a thread from its memory domain.
4785 *
4786 * Remove a thread from its memory domain.
4787 *
4788 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004789 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004790 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004791extern void k_mem_domain_remove_thread(k_tid_t thread);
4792
Anas Nashif166f5192018-02-25 08:02:36 -06004793/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004794
Andrew Boie756f9072017-10-10 16:01:49 -07004795/**
4796 * @brief Emit a character buffer to the console device
4797 *
4798 * @param c String of characters to print
4799 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004800 *
4801 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004802 */
4803__syscall void k_str_out(char *c, size_t n);
4804
Andy Rosse7172672018-01-24 15:48:32 -08004805/**
4806 * @brief Start a numbered CPU on a MP-capable system
4807
4808 * This starts and initializes a specific CPU. The main thread on
4809 * startup is running on CPU zero, other processors are numbered
4810 * sequentially. On return from this function, the CPU is known to
4811 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004812 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004813 * with the provided key will work to enable them.
4814 *
4815 * Normally, in SMP mode this function will be called by the kernel
4816 * initialization and should not be used as a user API. But it is
4817 * defined here for special-purpose apps which want Zephyr running on
4818 * one core and to use others for design-specific processing.
4819 *
4820 * @param cpu_num Integer number of the CPU
4821 * @param stack Stack memory for the CPU
4822 * @param sz Stack buffer size, in bytes
4823 * @param fn Function to begin running on the CPU. First argument is
4824 * an irq_unlock() key.
4825 * @param arg Untyped argument to be passed to "fn"
4826 */
4827extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4828 void (*fn)(int, void *), void *arg);
4829
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004830#ifdef __cplusplus
4831}
4832#endif
4833
Andrew Boiee004dec2016-11-07 09:01:19 -08004834#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4835/*
4836 * Define new and delete operators.
4837 * At this moment, the operators do nothing since objects are supposed
4838 * to be statically allocated.
4839 */
4840inline void operator delete(void *ptr)
4841{
4842 (void)ptr;
4843}
4844
4845inline void operator delete[](void *ptr)
4846{
4847 (void)ptr;
4848}
4849
4850inline void *operator new(size_t size)
4851{
4852 (void)size;
4853 return NULL;
4854}
4855
4856inline void *operator new[](size_t size)
4857{
4858 (void)size;
4859 return NULL;
4860}
4861
4862/* Placement versions of operator new and delete */
4863inline void operator delete(void *ptr1, void *ptr2)
4864{
4865 (void)ptr1;
4866 (void)ptr2;
4867}
4868
4869inline void operator delete[](void *ptr1, void *ptr2)
4870{
4871 (void)ptr1;
4872 (void)ptr2;
4873}
4874
4875inline void *operator new(size_t size, void *ptr)
4876{
4877 (void)size;
4878 return ptr;
4879}
4880
4881inline void *operator new[](size_t size, void *ptr)
4882{
4883 (void)size;
4884 return ptr;
4885}
4886
4887#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4888
Anas Nashifb6304e62018-07-04 08:03:03 -05004889#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004890#include <syscalls/kernel.h>
4891
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004892#endif /* !_ASMLANGUAGE */
4893
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004894#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */