blob: 4d1825c9c192777840a09d09386d54a5ff69541f [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/**
Andy Rosscfe62032018-09-29 07:34:55 -07001222 * @}
1223 */
1224
1225/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001226 * @addtogroup clock_apis
1227 * @{
1228 */
1229
1230/**
1231 * @brief Generate null timeout delay.
1232 *
1233 * This macro generates a timeout delay that that instructs a kernel API
1234 * not to wait if the requested operation cannot be performed immediately.
1235 *
1236 * @return Timeout delay value.
1237 */
1238#define K_NO_WAIT 0
1239
1240/**
1241 * @brief Generate timeout delay from milliseconds.
1242 *
1243 * This macro generates a timeout delay that that instructs a kernel API
1244 * to wait up to @a ms milliseconds to perform the requested operation.
1245 *
1246 * @param ms Duration in milliseconds.
1247 *
1248 * @return Timeout delay value.
1249 */
Johan Hedberg14471692016-11-13 10:52:15 +02001250#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001251
1252/**
1253 * @brief Generate timeout delay from seconds.
1254 *
1255 * This macro generates a timeout delay that that instructs a kernel API
1256 * to wait up to @a s seconds to perform the requested operation.
1257 *
1258 * @param s Duration in seconds.
1259 *
1260 * @return Timeout delay value.
1261 */
Johan Hedberg14471692016-11-13 10:52:15 +02001262#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001263
1264/**
1265 * @brief Generate timeout delay from minutes.
1266 *
1267 * This macro generates a timeout delay that that instructs a kernel API
1268 * to wait up to @a m minutes to perform the requested operation.
1269 *
1270 * @param m Duration in minutes.
1271 *
1272 * @return Timeout delay value.
1273 */
Johan Hedberg14471692016-11-13 10:52:15 +02001274#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001275
1276/**
1277 * @brief Generate timeout delay from hours.
1278 *
1279 * This macro generates a timeout delay that that instructs a kernel API
1280 * to wait up to @a h hours to perform the requested operation.
1281 *
1282 * @param h Duration in hours.
1283 *
1284 * @return Timeout delay value.
1285 */
Johan Hedberg14471692016-11-13 10:52:15 +02001286#define K_HOURS(h) K_MINUTES((h) * 60)
1287
Allan Stephensc98da842016-11-11 15:45:03 -05001288/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001289 * @brief Generate infinite timeout delay.
1290 *
1291 * This macro generates a timeout delay that that instructs a kernel API
1292 * to wait as long as necessary to perform the requested operation.
1293 *
1294 * @return Timeout delay value.
1295 */
1296#define K_FOREVER (-1)
1297
1298/**
Anas Nashif166f5192018-02-25 08:02:36 -06001299 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001300 */
1301
1302/**
Allan Stephensc98da842016-11-11 15:45:03 -05001303 * @cond INTERNAL_HIDDEN
1304 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001305
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001306struct k_timer {
1307 /*
1308 * _timeout structure must be first here if we want to use
1309 * dynamic timer allocation. timeout.node is used in the double-linked
1310 * list of free timers
1311 */
1312 struct _timeout timeout;
1313
Allan Stephens45bfa372016-10-12 12:39:42 -05001314 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315 _wait_q_t wait_q;
1316
1317 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001318 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001319
1320 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001321 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001322
1323 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001324 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325
Allan Stephens45bfa372016-10-12 12:39:42 -05001326 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001327 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001329 /* user-specific data, also used to support legacy features */
1330 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331
Anas Nashif2f203c22016-12-18 06:57:45 -05001332 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001333};
1334
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001335#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001336 { \
Andy Ross987c0e52018-09-27 16:50:00 -07001337 .timeout.dticks = _INACTIVE, \
1338 .timeout.fn = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001339 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001340 .expiry_fn = expiry, \
1341 .stop_fn = stop, \
1342 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001343 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001344 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001345 }
1346
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001347#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1348
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001349/**
Allan Stephensc98da842016-11-11 15:45:03 -05001350 * INTERNAL_HIDDEN @endcond
1351 */
1352
1353/**
1354 * @defgroup timer_apis Timer APIs
1355 * @ingroup kernel_apis
1356 * @{
1357 */
1358
1359/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001360 * @typedef k_timer_expiry_t
1361 * @brief Timer expiry function type.
1362 *
1363 * A timer's expiry function is executed by the system clock interrupt handler
1364 * each time the timer expires. The expiry function is optional, and is only
1365 * invoked if the timer has been initialized with one.
1366 *
1367 * @param timer Address of timer.
1368 *
1369 * @return N/A
1370 */
1371typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1372
1373/**
1374 * @typedef k_timer_stop_t
1375 * @brief Timer stop function type.
1376 *
1377 * A timer's stop function is executed if the timer is stopped prematurely.
1378 * The function runs in the context of the thread that stops the timer.
1379 * The stop function is optional, and is only invoked if the timer has been
1380 * initialized with one.
1381 *
1382 * @param timer Address of timer.
1383 *
1384 * @return N/A
1385 */
1386typedef void (*k_timer_stop_t)(struct k_timer *timer);
1387
1388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001392 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001393 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001394 *
1395 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 * @param expiry_fn Function to invoke each time the timer expires.
1397 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001398 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001399#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001400 struct k_timer name \
1401 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001402 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403
Allan Stephens45bfa372016-10-12 12:39:42 -05001404/**
1405 * @brief Initialize a timer.
1406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001408 *
1409 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001410 * @param expiry_fn Function to invoke each time the timer expires.
1411 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001412 *
1413 * @return N/A
1414 */
1415extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001416 k_timer_expiry_t expiry_fn,
1417 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001418
Allan Stephens45bfa372016-10-12 12:39:42 -05001419/**
1420 * @brief Start a timer.
1421 *
1422 * This routine starts a timer, and resets its status to zero. The timer
1423 * begins counting down using the specified duration and period values.
1424 *
1425 * Attempting to start a timer that is already running is permitted.
1426 * The timer's status is reset to zero and the timer begins counting down
1427 * using the new duration and period values.
1428 *
1429 * @param timer Address of timer.
1430 * @param duration Initial timer duration (in milliseconds).
1431 * @param period Timer period (in milliseconds).
1432 *
1433 * @return N/A
1434 */
Andrew Boiea354d492017-09-29 16:22:28 -07001435__syscall void k_timer_start(struct k_timer *timer,
1436 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001437
1438/**
1439 * @brief Stop a timer.
1440 *
1441 * This routine stops a running timer prematurely. The timer's stop function,
1442 * if one exists, is invoked by the caller.
1443 *
1444 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001445 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001446 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001447 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1448 * if @a k_timer_stop is to be called from ISRs.
1449 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001450 * @param timer Address of timer.
1451 *
1452 * @return N/A
1453 */
Andrew Boiea354d492017-09-29 16:22:28 -07001454__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001455
1456/**
1457 * @brief Read timer status.
1458 *
1459 * This routine reads the timer's status, which indicates the number of times
1460 * it has expired since its status was last read.
1461 *
1462 * Calling this routine resets the timer's status to zero.
1463 *
1464 * @param timer Address of timer.
1465 *
1466 * @return Timer status.
1467 */
Andrew Boiea354d492017-09-29 16:22:28 -07001468__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001469
1470/**
1471 * @brief Synchronize thread to timer expiration.
1472 *
1473 * This routine blocks the calling thread until the timer's status is non-zero
1474 * (indicating that it has expired at least once since it was last examined)
1475 * or the timer is stopped. If the timer status is already non-zero,
1476 * or the timer is already stopped, the caller continues without waiting.
1477 *
1478 * Calling this routine resets the timer's status to zero.
1479 *
1480 * This routine must not be used by interrupt handlers, since they are not
1481 * allowed to block.
1482 *
1483 * @param timer Address of timer.
1484 *
1485 * @return Timer status.
1486 */
Andrew Boiea354d492017-09-29 16:22:28 -07001487__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001488
Andy Ross52e444b2018-09-28 09:06:37 -07001489extern s32_t z_timeout_remaining(struct _timeout *timeout);
1490
Allan Stephens45bfa372016-10-12 12:39:42 -05001491/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001493 *
1494 * This routine computes the (approximate) time remaining before a running
1495 * timer next expires. If the timer is not running, it returns zero.
1496 *
1497 * @param timer Address of timer.
1498 *
1499 * @return Remaining time (in milliseconds).
1500 */
Andrew Boiea354d492017-09-29 16:22:28 -07001501__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1502
1503static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001504{
Andy Ross52e444b2018-09-28 09:06:37 -07001505 return __ticks_to_ms(z_timeout_remaining(&timer->timeout));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001506}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001507
Allan Stephensc98da842016-11-11 15:45:03 -05001508/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001509 * @brief Associate user-specific data with a timer.
1510 *
1511 * This routine records the @a user_data with the @a timer, to be retrieved
1512 * later.
1513 *
1514 * It can be used e.g. in a timer handler shared across multiple subsystems to
1515 * retrieve data specific to the subsystem this timer is associated with.
1516 *
1517 * @param timer Address of timer.
1518 * @param user_data User data to associate with the timer.
1519 *
1520 * @return N/A
1521 */
Andrew Boiea354d492017-09-29 16:22:28 -07001522__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1523
Anas Nashif954d5502018-02-25 08:37:28 -06001524/**
1525 * @internal
1526 */
Andrew Boiea354d492017-09-29 16:22:28 -07001527static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1528 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001529{
1530 timer->user_data = user_data;
1531}
1532
1533/**
1534 * @brief Retrieve the user-specific data from a timer.
1535 *
1536 * @param timer Address of timer.
1537 *
1538 * @return The user data.
1539 */
Andrew Boiea354d492017-09-29 16:22:28 -07001540__syscall void *k_timer_user_data_get(struct k_timer *timer);
1541
1542static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001543{
1544 return timer->user_data;
1545}
1546
Anas Nashif166f5192018-02-25 08:02:36 -06001547/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001548
Allan Stephensc98da842016-11-11 15:45:03 -05001549/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001550 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001551 * @{
1552 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001553
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001555 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * This routine returns the elapsed time since the system booted,
1558 * in milliseconds.
1559 *
1560 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001561 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001562__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001563
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001564/**
1565 * @brief Enable clock always on in tickless kernel
1566 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001567 * This routine enables keeping the clock running (that is, it always
1568 * keeps an active timer interrupt scheduled) when there are no timer
1569 * events programmed in tickless kernel scheduling. This is necessary
1570 * if the clock is used to track passage of time (e.g. via
1571 * k_uptime_get_32()), otherwise the internal hardware counter may
1572 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001573 *
1574 * @retval prev_status Previous status of always on flag
1575 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001576int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001577
1578/**
1579 * @brief Disable clock always on in tickless kernel
1580 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001581 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001582 * there are no timer events programmed in tickless kernel
1583 * scheduling. To save power, this routine should be called
1584 * immediately when clock is not used to track time.
1585 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001586void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001587
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * This routine returns the lower 32-bits of the elapsed time since the system
1592 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * This routine can be more efficient than k_uptime_get(), as it reduces the
1595 * need for interrupt locking and 64-bit math. However, the 32-bit result
1596 * cannot hold a system uptime time larger than approximately 50 days, so the
1597 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001599 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001600 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001601__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602
1603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001604 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * This routine computes the elapsed time between the current system uptime
1607 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * @param reftime Pointer to a reference time, which is updated to the current
1610 * uptime upon return.
1611 *
1612 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001613 */
Andy Ross987c0e52018-09-27 16:50:00 -07001614static inline s64_t k_uptime_delta(s64_t *reftime)
1615{
1616 s64_t uptime, delta;
1617
1618 uptime = k_uptime_get();
1619 delta = uptime - *reftime;
1620 *reftime = uptime;
1621
1622 return delta;
1623}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001624
1625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001628 * This routine computes the elapsed time between the current system uptime
1629 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1632 * need for interrupt locking and 64-bit math. However, the 32-bit result
1633 * cannot hold an elapsed time larger than approximately 50 days, so the
1634 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001636 * @param reftime Pointer to a reference time, which is updated to the current
1637 * uptime upon return.
1638 *
1639 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640 */
Andy Ross987c0e52018-09-27 16:50:00 -07001641static inline u32_t k_uptime_delta_32(s64_t *reftime)
1642{
1643 return (u32_t)k_uptime_delta(reftime);
1644}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001645
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001647 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * This routine returns the current time, as measured by the system's hardware
1650 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001652 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001653 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001654#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001655
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656/**
Anas Nashif166f5192018-02-25 08:02:36 -06001657 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658 */
1659
Allan Stephensc98da842016-11-11 15:45:03 -05001660/**
1661 * @cond INTERNAL_HIDDEN
1662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001664struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001665 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001666 union {
1667 _wait_q_t wait_q;
1668
1669 _POLL_EVENT;
1670 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001671
1672 _OBJECT_TRACING_NEXT_PTR(k_queue);
1673};
1674
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001675#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001676 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001677 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001678 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001679 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001680 _OBJECT_TRACING_INIT \
1681 }
1682
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001683#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1684
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001685extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1686
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001687/**
1688 * INTERNAL_HIDDEN @endcond
1689 */
1690
1691/**
1692 * @defgroup queue_apis Queue APIs
1693 * @ingroup kernel_apis
1694 * @{
1695 */
1696
1697/**
1698 * @brief Initialize a queue.
1699 *
1700 * This routine initializes a queue object, prior to its first use.
1701 *
1702 * @param queue Address of the queue.
1703 *
1704 * @return N/A
1705 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001706__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001707
1708/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001709 * @brief Cancel waiting on a queue.
1710 *
1711 * This routine causes first thread pending on @a queue, if any, to
1712 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001713 * If the queue is being waited on by k_poll(), it will return with
1714 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1715 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001716 *
1717 * @note Can be called by ISRs.
1718 *
1719 * @param queue Address of the queue.
1720 *
1721 * @return N/A
1722 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001723__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001724
1725/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001726 * @brief Append an element to the end of a queue.
1727 *
1728 * This routine appends a data item to @a queue. A queue data item must be
1729 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1730 * reserved for the kernel's use.
1731 *
1732 * @note Can be called by ISRs.
1733 *
1734 * @param queue Address of the queue.
1735 * @param data Address of the data item.
1736 *
1737 * @return N/A
1738 */
1739extern void k_queue_append(struct k_queue *queue, void *data);
1740
1741/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001742 * @brief Append an element to a queue.
1743 *
1744 * This routine appends a data item to @a queue. There is an implicit
1745 * memory allocation from the calling thread's resource pool, which is
1746 * automatically freed when the item is removed from the queue.
1747 *
1748 * @note Can be called by ISRs.
1749 *
1750 * @param queue Address of the queue.
1751 * @param data Address of the data item.
1752 *
1753 * @retval 0 on success
1754 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1755 */
1756__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1757
1758/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001759 * @brief Prepend an element to a queue.
1760 *
1761 * This routine prepends a data item to @a queue. A queue data item must be
1762 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1763 * reserved for the kernel's use.
1764 *
1765 * @note Can be called by ISRs.
1766 *
1767 * @param queue Address of the queue.
1768 * @param data Address of the data item.
1769 *
1770 * @return N/A
1771 */
1772extern void k_queue_prepend(struct k_queue *queue, void *data);
1773
1774/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001775 * @brief Prepend an element to a queue.
1776 *
1777 * This routine prepends a data item to @a queue. There is an implicit
1778 * memory allocation from the calling thread's resource pool, which is
1779 * automatically freed when the item is removed from the queue.
1780 *
1781 * @note Can be called by ISRs.
1782 *
1783 * @param queue Address of the queue.
1784 * @param data Address of the data item.
1785 *
1786 * @retval 0 on success
1787 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1788 */
1789__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1790
1791/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001792 * @brief Inserts an element to a queue.
1793 *
1794 * This routine inserts a data item to @a queue after previous item. A queue
1795 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1796 * item are reserved for the kernel's use.
1797 *
1798 * @note Can be called by ISRs.
1799 *
1800 * @param queue Address of the queue.
1801 * @param prev Address of the previous data item.
1802 * @param data Address of the data item.
1803 *
1804 * @return N/A
1805 */
1806extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1807
1808/**
1809 * @brief Atomically append a list of elements to a queue.
1810 *
1811 * This routine adds a list of data items to @a queue in one operation.
1812 * The data items must be in a singly-linked list, with the first 32 bits
1813 * in each data item pointing to the next data item; the list must be
1814 * NULL-terminated.
1815 *
1816 * @note Can be called by ISRs.
1817 *
1818 * @param queue Address of the queue.
1819 * @param head Pointer to first node in singly-linked list.
1820 * @param tail Pointer to last node in singly-linked list.
1821 *
1822 * @return N/A
1823 */
1824extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1825
1826/**
1827 * @brief Atomically add a list of elements to a queue.
1828 *
1829 * This routine adds a list of data items to @a queue in one operation.
1830 * The data items must be in a singly-linked list implemented using a
1831 * sys_slist_t object. Upon completion, the original list is empty.
1832 *
1833 * @note Can be called by ISRs.
1834 *
1835 * @param queue Address of the queue.
1836 * @param list Pointer to sys_slist_t object.
1837 *
1838 * @return N/A
1839 */
1840extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1841
1842/**
1843 * @brief Get an element from a queue.
1844 *
1845 * This routine removes first data item from @a queue. The first 32 bits of the
1846 * data item are reserved for the kernel's use.
1847 *
1848 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1849 *
1850 * @param queue Address of the queue.
1851 * @param timeout Waiting period to obtain a data item (in milliseconds),
1852 * or one of the special values K_NO_WAIT and K_FOREVER.
1853 *
1854 * @return Address of the data item if successful; NULL if returned
1855 * without waiting, or waiting period timed out.
1856 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001857__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001858
1859/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001860 * @brief Remove an element from a queue.
1861 *
1862 * This routine removes data item from @a queue. The first 32 bits of the
1863 * data item are reserved for the kernel's use. Removing elements from k_queue
1864 * rely on sys_slist_find_and_remove which is not a constant time operation.
1865 *
1866 * @note Can be called by ISRs
1867 *
1868 * @param queue Address of the queue.
1869 * @param data Address of the data item.
1870 *
1871 * @return true if data item was removed
1872 */
1873static inline bool k_queue_remove(struct k_queue *queue, void *data)
1874{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001875 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001876}
1877
1878/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001879 * @brief Append an element to a queue only if it's not present already.
1880 *
1881 * This routine appends data item to @a queue. The first 32 bits of the
1882 * data item are reserved for the kernel's use. Appending elements to k_queue
1883 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1884 *
1885 * @note Can be called by ISRs
1886 *
1887 * @param queue Address of the queue.
1888 * @param data Address of the data item.
1889 *
1890 * @return true if data item was added, false if not
1891 */
1892static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1893{
1894 sys_sfnode_t *test;
1895
1896 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1897 if (test == (sys_sfnode_t *) data) {
1898 return false;
1899 }
1900 }
1901
1902 k_queue_append(queue, data);
1903 return true;
1904}
1905
1906/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001907 * @brief Query a queue to see if it has data available.
1908 *
1909 * Note that the data might be already gone by the time this function returns
1910 * if other threads are also trying to read from the queue.
1911 *
1912 * @note Can be called by ISRs.
1913 *
1914 * @param queue Address of the queue.
1915 *
1916 * @return Non-zero if the queue is empty.
1917 * @return 0 if data is available.
1918 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001919__syscall int k_queue_is_empty(struct k_queue *queue);
1920
1921static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001922{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001923 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001924}
1925
1926/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001927 * @brief Peek element at the head of queue.
1928 *
1929 * Return element from the head of queue without removing it.
1930 *
1931 * @param queue Address of the queue.
1932 *
1933 * @return Head element, or NULL if queue is empty.
1934 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001935__syscall void *k_queue_peek_head(struct k_queue *queue);
1936
1937static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001938{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001939 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001940}
1941
1942/**
1943 * @brief Peek element at the tail of queue.
1944 *
1945 * Return element from the tail of queue without removing it.
1946 *
1947 * @param queue Address of the queue.
1948 *
1949 * @return Tail element, or NULL if queue is empty.
1950 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001951__syscall void *k_queue_peek_tail(struct k_queue *queue);
1952
1953static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001954{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001955 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001956}
1957
1958/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001959 * @brief Statically define and initialize a queue.
1960 *
1961 * The queue can be accessed outside the module where it is defined using:
1962 *
1963 * @code extern struct k_queue <name>; @endcode
1964 *
1965 * @param name Name of the queue.
1966 */
1967#define K_QUEUE_DEFINE(name) \
1968 struct k_queue name \
1969 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001970 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001971
Anas Nashif166f5192018-02-25 08:02:36 -06001972/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001973
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001975 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001976};
1977
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001978/**
1979 * @cond INTERNAL_HIDDEN
1980 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001981#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001982 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001983 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001984 }
1985
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001986#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1987
Allan Stephensc98da842016-11-11 15:45:03 -05001988/**
1989 * INTERNAL_HIDDEN @endcond
1990 */
1991
1992/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001993 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001994 * @ingroup kernel_apis
1995 * @{
1996 */
1997
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001999 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002001 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002003 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
2005 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002006 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002008#define k_fifo_init(fifo) \
2009 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010
2011/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002012 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002013 *
2014 * This routine causes first thread pending on @a fifo, if any, to
2015 * return from k_fifo_get() call with NULL value (as if timeout
2016 * expired).
2017 *
2018 * @note Can be called by ISRs.
2019 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002020 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002021 *
2022 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002023 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002024 */
2025#define k_fifo_cancel_wait(fifo) \
2026 k_queue_cancel_wait((struct k_queue *) fifo)
2027
2028/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002029 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002031 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2033 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * @note Can be called by ISRs.
2036 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
2040 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002041 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002043#define k_fifo_put(fifo, data) \
2044 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045
2046/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002047 * @brief Add an element to a FIFO queue.
2048 *
2049 * This routine adds a data item to @a fifo. There is an implicit
2050 * memory allocation from the calling thread's resource pool, which is
2051 * automatically freed when the item is removed.
2052 *
2053 * @note Can be called by ISRs.
2054 *
2055 * @param fifo Address of the FIFO.
2056 * @param data Address of the data item.
2057 *
2058 * @retval 0 on success
2059 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002060 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002061 */
2062#define k_fifo_alloc_put(fifo, data) \
2063 k_queue_alloc_append((struct k_queue *) fifo, data)
2064
2065/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002066 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * This routine adds a list of data items to @a fifo in one operation.
2069 * The data items must be in a singly-linked list, with the first 32 bits
2070 * each data item pointing to the next data item; the list must be
2071 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002075 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @param head Pointer to first node in singly-linked list.
2077 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 *
2079 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002080 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002082#define k_fifo_put_list(fifo, head, tail) \
2083 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084
2085/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * This routine adds a list of data items to @a fifo in one operation.
2089 * The data items must be in a singly-linked list implemented using a
2090 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 * and must be re-initialized via sys_slist_init().
2092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * @note Can be called by ISRs.
2094 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002095 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097 *
2098 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002099 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002101#define k_fifo_put_slist(fifo, list) \
2102 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103
2104/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002105 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * This routine removes a data item from @a fifo in a "first in, first out"
2108 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2111 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002113 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114 * or one of the special values K_NO_WAIT and K_FOREVER.
2115 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002116 * @return Address of the data item if successful; NULL if returned
2117 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002118 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002120#define k_fifo_get(fifo, timeout) \
2121 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002123/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002124 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002125 *
2126 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002127 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002128 *
2129 * @note Can be called by ISRs.
2130 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002131 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002132 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002133 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002134 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002135 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002136 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002137#define k_fifo_is_empty(fifo) \
2138 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002139
2140/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002141 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002142 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302144 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002145 * on each iteration of processing, a head container will be peeked,
2146 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002147 * it will be completely remove from the 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 Head element, or NULL if the 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_head(fifo) \
2155 k_queue_peek_head((struct k_queue *) fifo)
2156
2157/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002158 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002159 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002160 * Return element from the tail of FIFO queue (without removing it). A usecase
2161 * for this is if elements of the FIFO queue are themselves containers. Then
2162 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002163 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002164 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002165 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002166 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002167 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002168 */
2169#define k_fifo_peek_tail(fifo) \
2170 k_queue_peek_tail((struct k_queue *) fifo)
2171
2172/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002173 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002175 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002177 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002179 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002180 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002183 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002184 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002185 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186
Anas Nashif166f5192018-02-25 08:02:36 -06002187/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002188
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002190 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191};
2192
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002193/**
2194 * @cond INTERNAL_HIDDEN
2195 */
2196
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002197#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002198 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002199 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002200 }
2201
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002202#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2203
Allan Stephensc98da842016-11-11 15:45:03 -05002204/**
2205 * INTERNAL_HIDDEN @endcond
2206 */
2207
2208/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002209 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002210 * @ingroup kernel_apis
2211 * @{
2212 */
2213
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
2221 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002222 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002224#define k_lifo_init(lifo) \
2225 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226
2227/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002228 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002230 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2232 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002234 * @note Can be called by ISRs.
2235 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002236 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002237 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002238 *
2239 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002240 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002242#define k_lifo_put(lifo, data) \
2243 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244
2245/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002246 * @brief Add an element to a LIFO queue.
2247 *
2248 * This routine adds a data item to @a lifo. There is an implicit
2249 * memory allocation from the calling thread's resource pool, which is
2250 * automatically freed when the item is removed.
2251 *
2252 * @note Can be called by ISRs.
2253 *
2254 * @param lifo Address of the LIFO.
2255 * @param data Address of the data item.
2256 *
2257 * @retval 0 on success
2258 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002259 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002260 */
2261#define k_lifo_alloc_put(lifo, data) \
2262 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2263
2264/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002265 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002267 * This routine removes a data item from @a lifo in a "last in, first out"
2268 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2271 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002272 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002273 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002274 * or one of the special values K_NO_WAIT and K_FOREVER.
2275 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002276 * @return Address of the data item if successful; NULL if returned
2277 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002278 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002280#define k_lifo_get(lifo, timeout) \
2281 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002284 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002286 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002288 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002291 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002294 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002295 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002296 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297
Anas Nashif166f5192018-02-25 08:02:36 -06002298/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002299
2300/**
2301 * @cond INTERNAL_HIDDEN
2302 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302303#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304
2305struct k_stack {
2306 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002307 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308
Anas Nashif2f203c22016-12-18 06:57:45 -05002309 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002310 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311};
2312
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002313#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002314 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002315 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002316 .base = stack_buffer, \
2317 .next = stack_buffer, \
2318 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002319 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002320 }
2321
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002322#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2323
Allan Stephensc98da842016-11-11 15:45:03 -05002324/**
2325 * INTERNAL_HIDDEN @endcond
2326 */
2327
2328/**
2329 * @defgroup stack_apis Stack APIs
2330 * @ingroup kernel_apis
2331 * @{
2332 */
2333
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334/**
2335 * @brief Initialize a stack.
2336 *
2337 * This routine initializes a stack object, prior to its first use.
2338 *
2339 * @param stack Address of the stack.
2340 * @param buffer Address of array used to hold stacked values.
2341 * @param num_entries Maximum number of values that can be stacked.
2342 *
2343 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002344 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002345 */
Andrew Boief3bee952018-05-02 17:44:39 -07002346void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302347 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002348
2349
2350/**
2351 * @brief Initialize a stack.
2352 *
2353 * This routine initializes a stack object, prior to its first use. Internal
2354 * buffers will be allocated from the calling thread's resource pool.
2355 * This memory will be released if k_stack_cleanup() is called, or
2356 * userspace is enabled and the stack object loses all references to it.
2357 *
2358 * @param stack Address of the stack.
2359 * @param num_entries Maximum number of values that can be stacked.
2360 *
2361 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002362 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002363 */
2364
Adithya Baglody28080d32018-10-15 11:48:51 +05302365__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2366 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002367
2368/**
2369 * @brief Release a stack's allocated buffer
2370 *
2371 * If a stack object was given a dynamically allocated buffer via
2372 * k_stack_alloc_init(), this will free it. This function does nothing
2373 * if the buffer wasn't dynamically allocated.
2374 *
2375 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002376 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002377 */
2378void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379
2380/**
2381 * @brief Push an element onto a stack.
2382 *
2383 * This routine adds a 32-bit value @a data to @a stack.
2384 *
2385 * @note Can be called by ISRs.
2386 *
2387 * @param stack Address of the stack.
2388 * @param data Value to push onto the stack.
2389 *
2390 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002391 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002392 */
Andrew Boiee8734462017-09-29 16:42:07 -07002393__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394
2395/**
2396 * @brief Pop an element from a stack.
2397 *
2398 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2399 * manner and stores the value in @a data.
2400 *
2401 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2402 *
2403 * @param stack Address of the stack.
2404 * @param data Address of area to hold the value popped from the stack.
2405 * @param timeout Waiting period to obtain a value (in milliseconds),
2406 * or one of the special values K_NO_WAIT and K_FOREVER.
2407 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002408 * @retval 0 Element popped from stack.
2409 * @retval -EBUSY Returned without waiting.
2410 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002411 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 */
Andrew Boiee8734462017-09-29 16:42:07 -07002413__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002420 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * @param name Name of the stack.
2423 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002424 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002426#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002427 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002428 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002429 struct k_stack name \
2430 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002431 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002432 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433
Anas Nashif166f5192018-02-25 08:02:36 -06002434/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002435
Allan Stephens6bba9b02016-11-16 14:56:54 -05002436struct k_work;
2437
Allan Stephensc98da842016-11-11 15:45:03 -05002438/**
2439 * @defgroup workqueue_apis Workqueue Thread APIs
2440 * @ingroup kernel_apis
2441 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442 */
2443
Allan Stephens6bba9b02016-11-16 14:56:54 -05002444/**
2445 * @typedef k_work_handler_t
2446 * @brief Work item handler function type.
2447 *
2448 * A work item's handler function is executed by a workqueue's thread
2449 * when the work item is processed by the workqueue.
2450 *
2451 * @param work Address of the work item.
2452 *
2453 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002454 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002455 */
2456typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457
2458/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002459 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002461
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002463 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002464 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465};
2466
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002468 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469};
2470
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002472 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473 k_work_handler_t handler;
2474 atomic_t flags[1];
2475};
2476
Allan Stephens6bba9b02016-11-16 14:56:54 -05002477struct k_delayed_work {
2478 struct k_work work;
2479 struct _timeout timeout;
2480 struct k_work_q *work_q;
2481};
2482
2483extern struct k_work_q k_sys_work_q;
2484
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002486 * INTERNAL_HIDDEN @endcond
2487 */
2488
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002489#define _K_WORK_INITIALIZER(work_handler) \
2490 { \
2491 ._reserved = NULL, \
2492 .handler = work_handler, \
2493 .flags = { 0 } \
2494 }
2495
2496#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2497
Allan Stephens6bba9b02016-11-16 14:56:54 -05002498/**
2499 * @brief Initialize a statically-defined work item.
2500 *
2501 * This macro can be used to initialize a statically-defined workqueue work
2502 * item, prior to its first use. For example,
2503 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002504 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002505 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002506 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002508 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002510#define K_WORK_DEFINE(work, work_handler) \
2511 struct k_work work \
2512 __in_section(_k_work, static, work) = \
2513 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514
2515/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002516 * @brief Initialize a work item.
2517 *
2518 * This routine initializes a workqueue work item, prior to its first use.
2519 *
2520 * @param work Address of work item.
2521 * @param handler Function to invoke each time work item is processed.
2522 *
2523 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002524 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525 */
2526static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2527{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002528 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002529 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530}
2531
2532/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002533 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002534 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002535 * This routine submits work item @a work to be processed by workqueue
2536 * @a work_q. If the work item is already pending in the workqueue's queue
2537 * as a result of an earlier submission, this routine has no effect on the
2538 * work item. If the work item has already been processed, or is currently
2539 * being processed, its work is considered complete and the work item can be
2540 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002541 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002542 * @warning
2543 * A submitted work item must not be modified until it has been processed
2544 * by the workqueue.
2545 *
2546 * @note Can be called by ISRs.
2547 *
2548 * @param work_q Address of workqueue.
2549 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002550 *
2551 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002552 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553 */
2554static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2555 struct k_work *work)
2556{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002557 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002558 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559 }
2560}
2561
2562/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002563 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002564 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002565 * This routine indicates if work item @a work is pending in a workqueue's
2566 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002567 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002568 * @note Can be called by ISRs.
2569 *
2570 * @param work Address of work item.
2571 *
2572 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002573 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002574 */
2575static inline int k_work_pending(struct k_work *work)
2576{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002577 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002578}
2579
2580/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002581 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002582 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002583 * This routine starts workqueue @a work_q. The workqueue spawns its work
2584 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002585 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002586 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002587 * @param stack Pointer to work queue thread's stack space, as defined by
2588 * K_THREAD_STACK_DEFINE()
2589 * @param stack_size Size of the work queue thread's stack (in bytes), which
2590 * should either be the same constant passed to
2591 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002592 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002593 *
2594 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002595 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596 */
Andrew Boie507852a2017-07-25 18:47:07 -07002597extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002598 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002599 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002604 * This routine initializes a workqueue delayed work item, prior to
2605 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002606 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002607 * @param work Address of delayed work item.
2608 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002609 *
2610 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002611 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002613extern void k_delayed_work_init(struct k_delayed_work *work,
2614 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002615
2616/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002618 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * This routine schedules work item @a work to be processed by workqueue
2620 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002621 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * Only when the countdown completes is the work item actually submitted to
2623 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002626 * counting down cancels the existing submission and restarts the
2627 * countdown using the new delay. Note that this behavior is
2628 * inherently subject to race conditions with the pre-existing
2629 * timeouts and work queue, so care must be taken to synchronize such
2630 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002632 * @warning
2633 * A delayed work item must not be modified until it has been processed
2634 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002636 * @note Can be called by ISRs.
2637 *
2638 * @param work_q Address of workqueue.
2639 * @param work Address of delayed work item.
2640 * @param delay Delay before submitting the work item (in milliseconds).
2641 *
2642 * @retval 0 Work item countdown started.
2643 * @retval -EINPROGRESS Work item is already pending.
2644 * @retval -EINVAL Work item is being processed or has completed its work.
2645 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002646 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002647 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002648extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2649 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002650 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651
2652/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002653 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002655 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002656 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002657 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002659 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661 * @param work Address of delayed work item.
2662 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002663 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664 * @retval -EINPROGRESS Work item is already pending.
2665 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002666 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002668extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671 * @brief Submit a work item to the system workqueue.
2672 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002673 * This routine submits work item @a work to be processed by the system
2674 * workqueue. If the work item is already pending in the workqueue's queue
2675 * as a result of an earlier submission, this routine has no effect on the
2676 * work item. If the work item has already been processed, or is currently
2677 * being processed, its work is considered complete and the work item can be
2678 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002680 * @warning
2681 * Work items submitted to the system workqueue should avoid using handlers
2682 * that block or yield since this may prevent the system workqueue from
2683 * processing other work items in a timely manner.
2684 *
2685 * @note Can be called by ISRs.
2686 *
2687 * @param work Address of work item.
2688 *
2689 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002690 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002691 */
2692static inline void k_work_submit(struct k_work *work)
2693{
2694 k_work_submit_to_queue(&k_sys_work_q, work);
2695}
2696
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002697/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698 * @brief Submit a delayed work item to the system workqueue.
2699 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002700 * This routine schedules work item @a work to be processed by the system
2701 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002702 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002703 * Only when the countdown completes is the work item actually submitted to
2704 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002706 * Submitting a previously submitted delayed work item that is still
2707 * counting down cancels the existing submission and restarts the countdown
2708 * using the new delay. If the work item is currently pending on the
2709 * workqueue's queue because the countdown has completed it is too late to
2710 * resubmit the item, and resubmission fails without impacting the work item.
2711 * If the work item has already been processed, or is currently being processed,
2712 * its work is considered complete and the work item can be resubmitted.
2713 *
2714 * @warning
2715 * Work items submitted to the system workqueue should avoid using handlers
2716 * that block or yield since this may prevent the system workqueue from
2717 * processing other work items in a timely manner.
2718 *
2719 * @note Can be called by ISRs.
2720 *
2721 * @param work Address of delayed work item.
2722 * @param delay Delay before submitting the work item (in milliseconds).
2723 *
2724 * @retval 0 Work item countdown started.
2725 * @retval -EINPROGRESS Work item is already pending.
2726 * @retval -EINVAL Work item is being processed or has completed its work.
2727 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002728 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 */
2730static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002731 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002733 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734}
2735
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002737 * @brief Get time remaining before a delayed work gets scheduled.
2738 *
2739 * This routine computes the (approximate) time remaining before a
2740 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002741 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002742 *
2743 * @param work Delayed work item.
2744 *
2745 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002746 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002747 */
Kumar Galacc334c72017-04-21 10:55:34 -05002748static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002749{
Andy Ross52e444b2018-09-28 09:06:37 -07002750 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002751}
2752
Anas Nashif166f5192018-02-25 08:02:36 -06002753/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002754/**
Anas Nashifce78d162018-05-24 12:43:11 -05002755 * @defgroup mutex_apis Mutex APIs
2756 * @ingroup kernel_apis
2757 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002758 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759
Anas Nashifce78d162018-05-24 12:43:11 -05002760/**
2761 * Mutex Structure
2762 * @ingroup mutex_apis
2763 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764struct k_mutex {
2765 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002766 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002767 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002768 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770
Anas Nashif2f203c22016-12-18 06:57:45 -05002771 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772};
2773
Anas Nashifce78d162018-05-24 12:43:11 -05002774/**
2775 * @cond INTERNAL_HIDDEN
2776 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002777#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002779 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780 .owner = NULL, \
2781 .lock_count = 0, \
2782 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002783 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002784 }
2785
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002786#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2787
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002788/**
Allan Stephensc98da842016-11-11 15:45:03 -05002789 * INTERNAL_HIDDEN @endcond
2790 */
2791
2792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002796 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002797 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002799 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002800 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002801 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002803 struct k_mutex name \
2804 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002805 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002807/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002808 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002810 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * Upon completion, the mutex is available and does not have an owner.
2813 *
2814 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815 *
2816 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002817 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002819__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002820
2821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002822 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * This routine locks @a mutex. If the mutex is locked by another thread,
2825 * the calling thread waits until the mutex becomes available or until
2826 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * A thread is permitted to lock a mutex it has already locked. The operation
2829 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002831 * @param mutex Address of the mutex.
2832 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002833 * or one of the special values K_NO_WAIT and K_FOREVER.
2834 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002835 * @retval 0 Mutex locked.
2836 * @retval -EBUSY Returned without waiting.
2837 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002838 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002839 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002840__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002841
2842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002843 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * This routine unlocks @a mutex. The mutex must already be locked by the
2846 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847 *
2848 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 * thread.
2851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853 *
2854 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002855 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002856 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002857__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002858
Allan Stephensc98da842016-11-11 15:45:03 -05002859/**
Anas Nashif166f5192018-02-25 08:02:36 -06002860 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002861 */
2862
2863/**
2864 * @cond INTERNAL_HIDDEN
2865 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866
2867struct k_sem {
2868 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05302869 u32_t count;
2870 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002871 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872
Anas Nashif2f203c22016-12-18 06:57:45 -05002873 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874};
2875
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002876#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002877 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002878 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002879 .count = initial_count, \
2880 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002881 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002882 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002883 }
2884
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002885#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2886
Allan Stephensc98da842016-11-11 15:45:03 -05002887/**
2888 * INTERNAL_HIDDEN @endcond
2889 */
2890
2891/**
2892 * @defgroup semaphore_apis Semaphore APIs
2893 * @ingroup kernel_apis
2894 * @{
2895 */
2896
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002897/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002898 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @param sem Address of the semaphore.
2903 * @param initial_count Initial semaphore count.
2904 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002905 *
2906 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002907 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002908 */
Andrew Boie99280232017-09-29 14:17:47 -07002909__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2910 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002911
2912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2918 *
2919 * @param sem Address of the semaphore.
2920 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002921 * or one of the special values K_NO_WAIT and K_FOREVER.
2922 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002923 * @note When porting code from the nanokernel legacy API to the new API, be
2924 * careful with the return value of this function. The return value is the
2925 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2926 * non-zero means failure, while the nano_sem_take family returns 1 for success
2927 * and 0 for failure.
2928 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002929 * @retval 0 Semaphore taken.
2930 * @retval -EBUSY Returned without waiting.
2931 * @retval -EAGAIN Waiting period timed out.
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 int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002935
2936/**
2937 * @brief Give a semaphore.
2938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * This routine gives @a sem, unless the semaphore is already at its maximum
2940 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002945 *
2946 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002947 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002948 */
Andrew Boie99280232017-09-29 14:17:47 -07002949__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002951/**
2952 * @brief Reset a semaphore's count to zero.
2953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002957 *
2958 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002959 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 */
Andrew Boie990bf162017-10-03 12:36:49 -07002961__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002962
Anas Nashif954d5502018-02-25 08:37:28 -06002963/**
2964 * @internal
2965 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002966static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002967{
2968 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969}
2970
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002971/**
2972 * @brief Get a semaphore's count.
2973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002976 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002979 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002980 */
Andrew Boie990bf162017-10-03 12:36:49 -07002981__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002982
Anas Nashif954d5502018-02-25 08:37:28 -06002983/**
2984 * @internal
2985 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002986static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002987{
2988 return sem->count;
2989}
2990
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002995 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002996 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @param name Name of the semaphore.
2999 * @param initial_count Initial semaphore count.
3000 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003001 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003003#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003004 struct k_sem name \
3005 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003006 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303007 BUILD_ASSERT(((count_limit) != 0) && \
3008 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009
Anas Nashif166f5192018-02-25 08:02:36 -06003010/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003011
3012/**
3013 * @defgroup alert_apis Alert APIs
3014 * @ingroup kernel_apis
3015 * @{
3016 */
3017
Allan Stephens5eceb852016-11-16 10:16:30 -05003018/**
3019 * @typedef k_alert_handler_t
3020 * @brief Alert handler function type.
3021 *
3022 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003023 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003024 * and is only invoked if the alert has been initialized with one.
3025 *
3026 * @param alert Address of the alert.
3027 *
3028 * @return 0 if alert has been consumed; non-zero if alert should pend.
3029 */
3030typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003031
Anas Nashif166f5192018-02-25 08:02:36 -06003032/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003033
3034/**
3035 * @cond INTERNAL_HIDDEN
3036 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003038#define K_ALERT_DEFAULT NULL
Adithya Baglodyd5915882018-10-05 14:46:04 +05303039#define K_ALERT_IGNORE ((k_alert_handler_t)0xFFFFFFFF)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003041struct k_alert {
3042 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003043 atomic_t send_count;
3044 struct k_work work_item;
3045 struct k_sem sem;
3046
Anas Nashif2f203c22016-12-18 06:57:45 -05003047 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048};
3049
Anas Nashif954d5502018-02-25 08:37:28 -06003050/**
3051 * @internal
3052 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003053extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003055#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003056 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003057 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003059 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3060 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003061 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003062 }
3063
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003064#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3065
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003066/**
Allan Stephensc98da842016-11-11 15:45:03 -05003067 * INTERNAL_HIDDEN @endcond
3068 */
3069
3070/**
3071 * @addtogroup alert_apis
3072 * @{
3073 */
3074
3075/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003076 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003078 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003079 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003080 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003081 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003082 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003084 * @param name Name of the alert.
3085 * @param alert_handler Action to take when alert is sent. Specify either
3086 * the address of a function to be invoked by the system workqueue
3087 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3088 * K_ALERT_DEFAULT (which causes the alert to pend).
3089 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003090 *
3091 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003092 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003093#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003094 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003095 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003096 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003097 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003104 * @param alert Address of the alert.
3105 * @param handler Action to take when alert is sent. Specify either the address
3106 * of a function to be invoked by the system workqueue thread,
3107 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3108 * K_ALERT_DEFAULT (which causes the alert to pend).
3109 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003110 *
3111 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003112 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003113 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003114extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3115 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003116
3117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003120 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003122 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3123 *
3124 * @param alert Address of the alert.
3125 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003126 * or one of the special values K_NO_WAIT and K_FOREVER.
3127 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003128 * @retval 0 Alert received.
3129 * @retval -EBUSY Returned without waiting.
3130 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003131 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003132 */
Andrew Boie310e9872017-09-29 04:41:15 -07003133__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003134
3135/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003136 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003138 * This routine signals @a alert. The action specified for @a alert will
3139 * be taken, which may trigger the execution of an alert handler function
3140 * and/or cause the alert to pend (assuming the alert has not reached its
3141 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * @note Can be called by ISRs.
3144 *
3145 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003146 *
3147 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003148 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149 */
Andrew Boie310e9872017-09-29 04:41:15 -07003150__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003151
3152/**
Anas Nashif166f5192018-02-25 08:02:36 -06003153 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154 */
3155
Allan Stephensc98da842016-11-11 15:45:03 -05003156/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003157 * @defgroup msgq_apis Message Queue APIs
3158 * @ingroup kernel_apis
3159 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003160 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003161
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003162/**
3163 * @brief Message Queue Structure
3164 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165struct k_msgq {
3166 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003167 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003168 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003169 char *buffer_start;
3170 char *buffer_end;
3171 char *read_ptr;
3172 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003173 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003174
Anas Nashif2f203c22016-12-18 06:57:45 -05003175 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003176 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003178/**
3179 * @cond INTERNAL_HIDDEN
3180 */
3181
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003183#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003185 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003186 .max_msgs = q_max_msgs, \
3187 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003189 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003190 .read_ptr = q_buffer, \
3191 .write_ptr = q_buffer, \
3192 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003193 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003195#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003196/**
3197 * INTERNAL_HIDDEN @endcond
3198 */
3199
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003200
Andrew Boie0fe789f2018-04-12 18:35:56 -07003201#define K_MSGQ_FLAG_ALLOC BIT(0)
3202
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003203/**
3204 * @brief Message Queue Attributes
3205 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303206struct k_msgq_attrs {
3207 size_t msg_size;
3208 u32_t max_msgs;
3209 u32_t used_msgs;
3210};
3211
Allan Stephensc98da842016-11-11 15:45:03 -05003212
3213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3217 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003218 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3219 * message is similarly aligned to this boundary, @a q_msg_size must also be
3220 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003222 * The message queue can be accessed outside the module where it is defined
3223 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003224 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003225 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * @param q_name Name of the message queue.
3228 * @param q_msg_size Message size (in bytes).
3229 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003230 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003231 *
3232 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003233 */
3234#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003235 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003236 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003237 struct k_msgq q_name \
3238 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003239 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003240 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241
Peter Mitsisd7a37502016-10-13 11:37:40 -04003242/**
3243 * @brief Initialize a message queue.
3244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003245 * This routine initializes a message queue object, prior to its first use.
3246 *
Allan Stephensda827222016-11-09 14:23:58 -06003247 * The message queue's ring buffer must contain space for @a max_msgs messages,
3248 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3249 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3250 * that each message is similarly aligned to this boundary, @a q_msg_size
3251 * must also be a multiple of N.
3252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @param q Address of the message queue.
3254 * @param buffer Pointer to ring buffer that holds queued messages.
3255 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003256 * @param max_msgs Maximum number of messages that can be queued.
3257 *
3258 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003259 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003260 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003261void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3262 u32_t max_msgs);
3263
3264/**
3265 * @brief Initialize a message queue.
3266 *
3267 * This routine initializes a message queue object, prior to its first use,
3268 * allocating its internal ring buffer from the calling thread's resource
3269 * pool.
3270 *
3271 * Memory allocated for the ring buffer can be released by calling
3272 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3273 * all of its references.
3274 *
3275 * @param q Address of the message queue.
3276 * @param msg_size Message size (in bytes).
3277 * @param max_msgs Maximum number of messages that can be queued.
3278 *
3279 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3280 * thread's resource pool, or -EINVAL if the size parameters cause
3281 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003282 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003283 */
3284__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3285 u32_t max_msgs);
3286
3287
3288void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003289
3290/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003294 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003295 * @note Can be called by ISRs.
3296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @param q Address of the message queue.
3298 * @param data Pointer to the message.
3299 * @param timeout Waiting period to add the message (in milliseconds),
3300 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003301 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003302 * @retval 0 Message sent.
3303 * @retval -ENOMSG Returned without waiting or queue purged.
3304 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003305 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003306 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003307__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003308
3309/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * This routine receives a message from message queue @a q in a "first in,
3313 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003314 *
Allan Stephensc98da842016-11-11 15:45:03 -05003315 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003317 * @param q Address of the message queue.
3318 * @param data Address of area to hold the received message.
3319 * @param timeout Waiting period to receive the message (in milliseconds),
3320 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003321 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003322 * @retval 0 Message received.
3323 * @retval -ENOMSG Returned without waiting.
3324 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003325 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003326 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003327__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003328
3329/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * This routine discards all unreceived messages in a message queue's ring
3333 * buffer. Any threads that are blocked waiting to send a message to the
3334 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003337 *
3338 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003339 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003340 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003341__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342
Peter Mitsis67be2492016-10-07 11:44:34 -04003343/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * This routine returns the number of unused entries in a message queue's
3347 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @param q Address of the message queue.
3350 *
3351 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003352 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003353 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003354__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3355
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303356/**
3357 * @brief Get basic attributes of a message queue.
3358 *
3359 * This routine fetches basic attributes of message queue into attr argument.
3360 *
3361 * @param q Address of the message queue.
3362 * @param attrs pointer to message queue attribute structure.
3363 *
3364 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003365 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303366 */
3367__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3368
3369
Andrew Boie82edb6e2017-10-02 10:53:06 -07003370static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003371{
3372 return q->max_msgs - q->used_msgs;
3373}
3374
Peter Mitsisd7a37502016-10-13 11:37:40 -04003375/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * @param q Address of the message queue.
3381 *
3382 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003383 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003384 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003385__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3386
3387static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003388{
3389 return q->used_msgs;
3390}
3391
Anas Nashif166f5192018-02-25 08:02:36 -06003392/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003393
3394/**
3395 * @defgroup mem_pool_apis Memory Pool APIs
3396 * @ingroup kernel_apis
3397 * @{
3398 */
3399
Andy Ross73cb9582017-05-09 10:42:39 -07003400/* Note on sizing: the use of a 20 bit field for block means that,
3401 * assuming a reasonable minimum block size of 16 bytes, we're limited
3402 * to 16M of memory managed by a single pool. Long term it would be
3403 * good to move to a variable bit size based on configuration.
3404 */
3405struct k_mem_block_id {
3406 u32_t pool : 8;
3407 u32_t level : 4;
3408 u32_t block : 20;
3409};
3410
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003412 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003413 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414};
3415
Anas Nashif166f5192018-02-25 08:02:36 -06003416/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003417
3418/**
3419 * @defgroup mailbox_apis Mailbox APIs
3420 * @ingroup kernel_apis
3421 * @{
3422 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003423
3424struct k_mbox_msg {
3425 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003426 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003427 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003428 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003429 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003430 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431 /** sender's message data buffer */
3432 void *tx_data;
3433 /** internal use only - needed for legacy API support */
3434 void *_rx_data;
3435 /** message data block descriptor */
3436 struct k_mem_block tx_block;
3437 /** source thread id */
3438 k_tid_t rx_source_thread;
3439 /** target thread id */
3440 k_tid_t tx_target_thread;
3441 /** internal use only - thread waiting on send (may be a dummy) */
3442 k_tid_t _syncing_thread;
3443#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3444 /** internal use only - semaphore used during asynchronous send */
3445 struct k_sem *_async_sem;
3446#endif
3447};
3448
3449struct k_mbox {
3450 _wait_q_t tx_msg_queue;
3451 _wait_q_t rx_msg_queue;
3452
Anas Nashif2f203c22016-12-18 06:57:45 -05003453 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003455/**
3456 * @cond INTERNAL_HIDDEN
3457 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003458
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003459#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003460 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003461 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3462 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003463 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003464 }
3465
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003466#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3467
Peter Mitsis12092702016-10-14 12:57:23 -04003468/**
Allan Stephensc98da842016-11-11 15:45:03 -05003469 * INTERNAL_HIDDEN @endcond
3470 */
3471
3472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003473 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003476 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003477 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003480 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003483 struct k_mbox name \
3484 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003485 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486
Peter Mitsis12092702016-10-14 12:57:23 -04003487/**
3488 * @brief Initialize a mailbox.
3489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * This routine initializes a mailbox object, prior to its first use.
3491 *
3492 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003493 *
3494 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003495 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003496 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003497extern void k_mbox_init(struct k_mbox *mbox);
3498
Peter Mitsis12092702016-10-14 12:57:23 -04003499/**
3500 * @brief Send a mailbox message in a synchronous manner.
3501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * This routine sends a message to @a mbox and waits for a receiver to both
3503 * receive and process it. The message data may be in a buffer, in a memory
3504 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * @param mbox Address of the mailbox.
3507 * @param tx_msg Address of the transmit message descriptor.
3508 * @param timeout Waiting period for the message to be received (in
3509 * milliseconds), or one of the special values K_NO_WAIT
3510 * and K_FOREVER. Once the message has been received,
3511 * this routine waits as long as necessary for the message
3512 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003513 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003514 * @retval 0 Message sent.
3515 * @retval -ENOMSG Returned without waiting.
3516 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003517 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003518 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003519extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003520 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003521
Peter Mitsis12092702016-10-14 12:57:23 -04003522/**
3523 * @brief Send a mailbox message in an asynchronous manner.
3524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * This routine sends a message to @a mbox without waiting for a receiver
3526 * to process it. The message data may be in a buffer, in a memory pool block,
3527 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3528 * will be given when the message has been both received and completely
3529 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * @param mbox Address of the mailbox.
3532 * @param tx_msg Address of the transmit message descriptor.
3533 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003534 *
3535 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003536 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003537 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003538extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003539 struct k_sem *sem);
3540
Peter Mitsis12092702016-10-14 12:57:23 -04003541/**
3542 * @brief Receive a mailbox message.
3543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * This routine receives a message from @a mbox, then optionally retrieves
3545 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003547 * @param mbox Address of the mailbox.
3548 * @param rx_msg Address of the receive message descriptor.
3549 * @param buffer Address of the buffer to receive data, or NULL to defer data
3550 * retrieval and message disposal until later.
3551 * @param timeout Waiting period for a message to be received (in
3552 * milliseconds), or one of the special values K_NO_WAIT
3553 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003554 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003555 * @retval 0 Message received.
3556 * @retval -ENOMSG Returned without waiting.
3557 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003558 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003559 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003560extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003561 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003562
3563/**
3564 * @brief Retrieve mailbox message data into a buffer.
3565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003566 * This routine completes the processing of a received message by retrieving
3567 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003568 *
3569 * Alternatively, this routine can be used to dispose of a received message
3570 * without retrieving its data.
3571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * @param rx_msg Address of the receive message descriptor.
3573 * @param buffer Address of the buffer to receive data, or NULL to discard
3574 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003575 *
3576 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003577 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003578 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003579extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003580
3581/**
3582 * @brief Retrieve mailbox message data into a memory pool block.
3583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003584 * This routine completes the processing of a received message by retrieving
3585 * its data into a memory pool block, then disposing of the message.
3586 * The memory pool block that results from successful retrieval must be
3587 * returned to the pool once the data has been processed, even in cases
3588 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003589 *
3590 * Alternatively, this routine can be used to dispose of a received message
3591 * without retrieving its data. In this case there is no need to return a
3592 * memory pool block to the pool.
3593 *
3594 * This routine allocates a new memory pool block for the data only if the
3595 * data is not already in one. If a new block cannot be allocated, the routine
3596 * returns a failure code and the received message is left unchanged. This
3597 * permits the caller to reattempt data retrieval at a later time or to dispose
3598 * of the received message without retrieving its data.
3599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003600 * @param rx_msg Address of a receive message descriptor.
3601 * @param pool Address of memory pool, or NULL to discard data.
3602 * @param block Address of the area to hold memory pool block info.
3603 * @param timeout Waiting period to wait for a memory pool block (in
3604 * milliseconds), or one of the special values K_NO_WAIT
3605 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003606 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003607 * @retval 0 Data retrieved.
3608 * @retval -ENOMEM Returned without waiting.
3609 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003610 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003611 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003612extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003613 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003614 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615
Anas Nashif166f5192018-02-25 08:02:36 -06003616/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003617
3618/**
Anas Nashifce78d162018-05-24 12:43:11 -05003619 * @defgroup pipe_apis Pipe APIs
3620 * @ingroup kernel_apis
3621 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003622 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003623
Anas Nashifce78d162018-05-24 12:43:11 -05003624/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003625struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003626 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3627 size_t size; /**< Buffer size */
3628 size_t bytes_used; /**< # bytes used in buffer */
3629 size_t read_index; /**< Where in buffer to read from */
3630 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003631
3632 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003633 _wait_q_t readers; /**< Reader wait queue */
3634 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003635 } wait_q;
3636
Anas Nashif2f203c22016-12-18 06:57:45 -05003637 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003638 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003639};
3640
Anas Nashifce78d162018-05-24 12:43:11 -05003641/**
3642 * @cond INTERNAL_HIDDEN
3643 */
3644#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3645
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003646#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003647 { \
3648 .buffer = pipe_buffer, \
3649 .size = pipe_buffer_size, \
3650 .bytes_used = 0, \
3651 .read_index = 0, \
3652 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003653 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3654 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003655 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003656 }
3657
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003658#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3659
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003660/**
Allan Stephensc98da842016-11-11 15:45:03 -05003661 * INTERNAL_HIDDEN @endcond
3662 */
3663
3664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003665 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003667 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003668 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003669 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003671 * @param name Name of the pipe.
3672 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3673 * or zero if no ring buffer is used.
3674 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003675 *
3676 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003677 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003678#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3679 static unsigned char __kernel_noinit __aligned(pipe_align) \
3680 _k_pipe_buf_##name[pipe_buffer_size]; \
3681 struct k_pipe name \
3682 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003683 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003684
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * This routine initializes a pipe object, prior to its first use.
3689 *
3690 * @param pipe Address of the pipe.
3691 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3692 * is used.
3693 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3694 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003695 *
3696 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003697 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003698 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003699void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3700
3701/**
3702 * @brief Release a pipe's allocated buffer
3703 *
3704 * If a pipe object was given a dynamically allocated buffer via
3705 * k_pipe_alloc_init(), this will free it. This function does nothing
3706 * if the buffer wasn't dynamically allocated.
3707 *
3708 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003709 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003710 */
3711void k_pipe_cleanup(struct k_pipe *pipe);
3712
3713/**
3714 * @brief Initialize a pipe and allocate a buffer for it
3715 *
3716 * Storage for the buffer region will be allocated from the calling thread's
3717 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3718 * or userspace is enabled and the pipe object loses all references to it.
3719 *
3720 * This function should only be called on uninitialized pipe objects.
3721 *
3722 * @param pipe Address of the pipe.
3723 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3724 * buffer is used.
3725 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003726 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003727 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003728 */
3729__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003730
3731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003732 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003734 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003736 * @param pipe Address of the pipe.
3737 * @param data Address of data to write.
3738 * @param bytes_to_write Size of data (in bytes).
3739 * @param bytes_written Address of area to hold the number of bytes written.
3740 * @param min_xfer Minimum number of bytes to write.
3741 * @param timeout Waiting period to wait for the data to be written (in
3742 * milliseconds), or one of the special values K_NO_WAIT
3743 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003744 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003745 * @retval 0 At least @a min_xfer bytes of data were written.
3746 * @retval -EIO Returned without waiting; zero data bytes were written.
3747 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003749 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003751__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3752 size_t bytes_to_write, size_t *bytes_written,
3753 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003754
3755/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003756 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003758 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003760 * @param pipe Address of the pipe.
3761 * @param data Address to place the data read from pipe.
3762 * @param bytes_to_read Maximum number of data bytes to read.
3763 * @param bytes_read Address of area to hold the number of bytes read.
3764 * @param min_xfer Minimum number of data bytes to read.
3765 * @param timeout Waiting period to wait for the data to be read (in
3766 * milliseconds), or one of the special values K_NO_WAIT
3767 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003768 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003769 * @retval 0 At least @a min_xfer bytes of data were read.
3770 * @retval -EIO Returned without waiting; zero data bytes were read.
3771 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003772 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003773 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003774 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003775__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3776 size_t bytes_to_read, size_t *bytes_read,
3777 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003778
3779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003780 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003782 * This routine writes the data contained in a memory block to @a pipe.
3783 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003784 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003786 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787 * @param block Memory block containing data to send
3788 * @param size Number of data bytes in memory block to send
3789 * @param sem Semaphore to signal upon completion (else NULL)
3790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003792 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003793 */
3794extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3795 size_t size, struct k_sem *sem);
3796
Anas Nashif166f5192018-02-25 08:02:36 -06003797/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798
Allan Stephensc98da842016-11-11 15:45:03 -05003799/**
3800 * @cond INTERNAL_HIDDEN
3801 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003802
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003803struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003805 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003806 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807 char *buffer;
3808 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003809 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810
Anas Nashif2f203c22016-12-18 06:57:45 -05003811 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812};
3813
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003814#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003815 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003817 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003818 .num_blocks = slab_num_blocks, \
3819 .block_size = slab_block_size, \
3820 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 .free_list = NULL, \
3822 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003823 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824 }
3825
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003826#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3827
3828
Peter Mitsis578f9112016-10-07 13:50:31 -04003829/**
Allan Stephensc98da842016-11-11 15:45:03 -05003830 * INTERNAL_HIDDEN @endcond
3831 */
3832
3833/**
3834 * @defgroup mem_slab_apis Memory Slab APIs
3835 * @ingroup kernel_apis
3836 * @{
3837 */
3838
3839/**
Allan Stephensda827222016-11-09 14:23:58 -06003840 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003841 *
Allan Stephensda827222016-11-09 14:23:58 -06003842 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003843 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003844 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3845 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003846 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003847 *
Allan Stephensda827222016-11-09 14:23:58 -06003848 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003849 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003850 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003851 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003853 * @param name Name of the memory slab.
3854 * @param slab_block_size Size of each memory block (in bytes).
3855 * @param slab_num_blocks Number memory blocks.
3856 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003857 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003858 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003859#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3860 char __noinit __aligned(slab_align) \
3861 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3862 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003863 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003864 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003865 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003867/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003868 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003870 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003871 *
Allan Stephensda827222016-11-09 14:23:58 -06003872 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3873 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3874 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3875 * To ensure that each memory block is similarly aligned to this boundary,
3876 * @a slab_block_size must also be a multiple of N.
3877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003878 * @param slab Address of the memory slab.
3879 * @param buffer Pointer to buffer used for the memory blocks.
3880 * @param block_size Size of each memory block (in bytes).
3881 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003882 *
3883 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003884 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003885 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003886extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003887 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003888
3889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003890 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003892 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003894 * @param slab Address of the memory slab.
3895 * @param mem Pointer to block address area.
3896 * @param timeout Maximum time to wait for operation to complete
3897 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3898 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003899 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003900 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003901 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003902 * @retval -ENOMEM Returned without waiting.
3903 * @retval -EAGAIN Waiting period timed out.
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 int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003907 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003908
3909/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003910 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * This routine releases a previously allocated memory block back to its
3913 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003915 * @param slab Address of the memory slab.
3916 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003917 *
3918 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003919 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003920 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003921extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003922
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003926 * This routine gets the number of memory blocks that are currently
3927 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003929 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003932 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003933 */
Kumar Galacc334c72017-04-21 10:55:34 -05003934static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003935{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003936 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003937}
3938
Peter Mitsisc001aa82016-10-13 13:53:37 -04003939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003940 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003942 * This routine gets the number of memory blocks that are currently
3943 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003945 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003948 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003949 */
Kumar Galacc334c72017-04-21 10:55:34 -05003950static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003951{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003952 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003953}
3954
Anas Nashif166f5192018-02-25 08:02:36 -06003955/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003956
3957/**
3958 * @cond INTERNAL_HIDDEN
3959 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003960
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003961struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003962 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003963 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964};
3965
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003966/**
Allan Stephensc98da842016-11-11 15:45:03 -05003967 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003968 */
3969
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003970/**
Allan Stephensc98da842016-11-11 15:45:03 -05003971 * @addtogroup mem_pool_apis
3972 * @{
3973 */
3974
3975/**
3976 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3979 * long. The memory pool allows blocks to be repeatedly partitioned into
3980 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003981 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003982 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003983 * If the pool is to be accessed outside the module where it is defined, it
3984 * can be declared via
3985 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003986 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003988 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003989 * @param minsz Size of the smallest blocks in the pool (in bytes).
3990 * @param maxsz Size of the largest blocks in the pool (in bytes).
3991 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003992 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003993 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003994 */
Andy Ross73cb9582017-05-09 10:42:39 -07003995#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3996 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3997 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003998 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003999 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004000 .base = { \
4001 .buf = _mpool_buf_##name, \
4002 .max_sz = maxsz, \
4003 .n_max = nmax, \
4004 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4005 .levels = _mpool_lvls_##name, \
4006 .flags = SYS_MEM_POOL_KERNEL \
4007 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004008 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004009
Peter Mitsis937042c2016-10-13 13:18:26 -04004010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004011 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004013 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004015 * @param pool Address of the memory pool.
4016 * @param block Pointer to block descriptor for the allocated memory.
4017 * @param size Amount of memory to allocate (in bytes).
4018 * @param timeout Maximum time to wait for operation to complete
4019 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4020 * or K_FOREVER to wait as long as necessary.
4021 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004022 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004024 * @retval -ENOMEM Returned without waiting.
4025 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004026 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004027 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004028extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004029 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004030
4031/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004032 * @brief Allocate memory from a memory pool with malloc() semantics
4033 *
4034 * Such memory must be released using k_free().
4035 *
4036 * @param pool Address of the memory pool.
4037 * @param size Amount of memory to allocate (in bytes).
4038 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004039 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004040 */
4041extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4042
4043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004044 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004046 * This routine releases a previously allocated memory block back to its
4047 * memory pool.
4048 *
4049 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004050 *
4051 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004052 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004053 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004054extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004055
4056/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004057 * @brief Free memory allocated from a memory pool.
4058 *
4059 * This routine releases a previously allocated memory block back to its
4060 * memory pool
4061 *
4062 * @param id Memory block identifier.
4063 *
4064 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004065 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004066 */
4067extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4068
4069/**
Anas Nashif166f5192018-02-25 08:02:36 -06004070 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004071 */
4072
4073/**
4074 * @defgroup heap_apis Heap Memory Pool APIs
4075 * @ingroup kernel_apis
4076 * @{
4077 */
4078
4079/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004080 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004082 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004083 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004085 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004087 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004088 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004089 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004090extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004091
4092/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004093 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004094 *
4095 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004096 * returned must have been allocated from the heap memory pool or
4097 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004098 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004099 * If @a ptr is NULL, no operation is performed.
4100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004101 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004102 *
4103 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004104 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004105 */
4106extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004107
Allan Stephensc98da842016-11-11 15:45:03 -05004108/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004109 * @brief Allocate memory from heap, array style
4110 *
4111 * This routine provides traditional calloc() semantics. Memory is
4112 * allocated from the heap memory pool and zeroed.
4113 *
4114 * @param nmemb Number of elements in the requested array
4115 * @param size Size of each array element (in bytes).
4116 *
4117 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004118 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004119 */
4120extern void *k_calloc(size_t nmemb, size_t size);
4121
Anas Nashif166f5192018-02-25 08:02:36 -06004122/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004123
Benjamin Walshacc68c12017-01-29 18:57:45 -05004124/* polling API - PRIVATE */
4125
Benjamin Walshb0179862017-02-02 16:39:57 -05004126#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004127#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004128#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004129#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004130#endif
4131
Benjamin Walshacc68c12017-01-29 18:57:45 -05004132/* private - implementation data created as needed, per-type */
4133struct _poller {
4134 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004135 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004136};
4137
4138/* private - types bit positions */
4139enum _poll_types_bits {
4140 /* can be used to ignore an event */
4141 _POLL_TYPE_IGNORE,
4142
4143 /* to be signaled by k_poll_signal() */
4144 _POLL_TYPE_SIGNAL,
4145
4146 /* semaphore availability */
4147 _POLL_TYPE_SEM_AVAILABLE,
4148
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004149 /* queue/fifo/lifo data availability */
4150 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004151
4152 _POLL_NUM_TYPES
4153};
4154
4155#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4156
4157/* private - states bit positions */
4158enum _poll_states_bits {
4159 /* default state when creating event */
4160 _POLL_STATE_NOT_READY,
4161
Benjamin Walshacc68c12017-01-29 18:57:45 -05004162 /* signaled by k_poll_signal() */
4163 _POLL_STATE_SIGNALED,
4164
4165 /* semaphore is available */
4166 _POLL_STATE_SEM_AVAILABLE,
4167
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004168 /* data is available to read on queue/fifo/lifo */
4169 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004170
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004171 /* queue/fifo/lifo wait was cancelled */
4172 _POLL_STATE_CANCELLED,
4173
Benjamin Walshacc68c12017-01-29 18:57:45 -05004174 _POLL_NUM_STATES
4175};
4176
4177#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4178
4179#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004180 (32 - (0 \
4181 + 8 /* tag */ \
4182 + _POLL_NUM_TYPES \
4183 + _POLL_NUM_STATES \
4184 + 1 /* modes */ \
4185 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004186
Benjamin Walshacc68c12017-01-29 18:57:45 -05004187/* end of polling API - PRIVATE */
4188
4189
4190/**
4191 * @defgroup poll_apis Async polling APIs
4192 * @ingroup kernel_apis
4193 * @{
4194 */
4195
4196/* Public polling API */
4197
4198/* public - values for k_poll_event.type bitfield */
4199#define K_POLL_TYPE_IGNORE 0
4200#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4201#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004202#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4203#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004204
4205/* public - polling modes */
4206enum k_poll_modes {
4207 /* polling thread does not take ownership of objects when available */
4208 K_POLL_MODE_NOTIFY_ONLY = 0,
4209
4210 K_POLL_NUM_MODES
4211};
4212
4213/* public - values for k_poll_event.state bitfield */
4214#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004215#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4216#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004217#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4218#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004219#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004220
4221/* public - poll signal object */
4222struct k_poll_signal {
4223 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004224 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004225
4226 /*
4227 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4228 * user resets it to 0.
4229 */
4230 unsigned int signaled;
4231
4232 /* custom result value passed to k_poll_signal() if needed */
4233 int result;
4234};
4235
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004236#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004237 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004238 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004239 .signaled = 0, \
4240 .result = 0, \
4241 }
4242
4243struct k_poll_event {
4244 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004245 sys_dnode_t _node;
4246
4247 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004248 struct _poller *poller;
4249
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004250 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004251 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004252
Benjamin Walshacc68c12017-01-29 18:57:45 -05004253 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004254 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255
4256 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004257 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004258
4259 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004260 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261
4262 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004263 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004264
4265 /* per-type data */
4266 union {
4267 void *obj;
4268 struct k_poll_signal *signal;
4269 struct k_sem *sem;
4270 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004271 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004272 };
4273};
4274
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004275#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004276 { \
4277 .poller = NULL, \
4278 .type = event_type, \
4279 .state = K_POLL_STATE_NOT_READY, \
4280 .mode = event_mode, \
4281 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004282 { .obj = event_obj }, \
4283 }
4284
4285#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4286 event_tag) \
4287 { \
4288 .type = event_type, \
4289 .tag = event_tag, \
4290 .state = K_POLL_STATE_NOT_READY, \
4291 .mode = event_mode, \
4292 .unused = 0, \
4293 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004294 }
4295
4296/**
4297 * @brief Initialize one struct k_poll_event instance
4298 *
4299 * After this routine is called on a poll event, the event it ready to be
4300 * placed in an event array to be passed to k_poll().
4301 *
4302 * @param event The event to initialize.
4303 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4304 * values. Only values that apply to the same object being polled
4305 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4306 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004307 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004308 * @param obj Kernel object or poll signal.
4309 *
4310 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004311 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312 */
4313
Kumar Galacc334c72017-04-21 10:55:34 -05004314extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004315 int mode, void *obj);
4316
4317/**
4318 * @brief Wait for one or many of multiple poll events to occur
4319 *
4320 * This routine allows a thread to wait concurrently for one or many of
4321 * multiple poll events to have occurred. Such events can be a kernel object
4322 * being available, like a semaphore, or a poll signal event.
4323 *
4324 * When an event notifies that a kernel object is available, the kernel object
4325 * is not "given" to the thread calling k_poll(): it merely signals the fact
4326 * that the object was available when the k_poll() call was in effect. Also,
4327 * all threads trying to acquire an object the regular way, i.e. by pending on
4328 * the object, have precedence over the thread polling on the object. This
4329 * means that the polling thread will never get the poll event on an object
4330 * until the object becomes available and its pend queue is empty. For this
4331 * reason, the k_poll() call is more effective when the objects being polled
4332 * only have one thread, the polling thread, trying to acquire them.
4333 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004334 * When k_poll() returns 0, the caller should loop on all the events that were
4335 * passed to k_poll() and check the state field for the values that were
4336 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004337 *
4338 * Before being reused for another call to k_poll(), the user has to reset the
4339 * state field to K_POLL_STATE_NOT_READY.
4340 *
Andrew Boie3772f772018-05-07 16:52:57 -07004341 * When called from user mode, a temporary memory allocation is required from
4342 * the caller's resource pool.
4343 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004344 * @param events An array of pointers to events to be polled for.
4345 * @param num_events The number of events in the array.
4346 * @param timeout Waiting period for an event to be ready (in milliseconds),
4347 * or one of the special values K_NO_WAIT and K_FOREVER.
4348 *
4349 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004350 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004351 * @retval -EINTR Polling has been interrupted, e.g. with
4352 * k_queue_cancel_wait(). All output events are still set and valid,
4353 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4354 * words, -EINTR status means that at least one of output events is
4355 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004356 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4357 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004358 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004359 */
4360
Andrew Boie3772f772018-05-07 16:52:57 -07004361__syscall int k_poll(struct k_poll_event *events, int num_events,
4362 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004363
4364/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004365 * @brief Initialize a poll signal object.
4366 *
4367 * Ready a poll signal object to be signaled via k_poll_signal().
4368 *
4369 * @param signal A poll signal.
4370 *
4371 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004372 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004373 */
4374
Andrew Boie3772f772018-05-07 16:52:57 -07004375__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4376
4377/*
4378 * @brief Reset a poll signal object's state to unsignaled.
4379 *
4380 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004381 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004382 */
4383__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4384
4385static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4386{
4387 signal->signaled = 0;
4388}
4389
4390/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004391 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004392 *
4393 * @param signal A poll signal object
4394 * @param signaled An integer buffer which will be written nonzero if the
4395 * object was signaled
4396 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004397 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004398 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004399 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004400 */
4401__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4402 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004403
4404/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004405 * @brief Signal a poll signal object.
4406 *
4407 * This routine makes ready a poll signal, which is basically a poll event of
4408 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4409 * made ready to run. A @a result value can be specified.
4410 *
4411 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004412 * k_poll_signal(), stays set until the user sets it back to 0 with
4413 * k_poll_signal_reset(). It thus has to be reset by the user before being
4414 * passed again to k_poll() or k_poll() will consider it being signaled, and
4415 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004416 *
4417 * @param signal A poll signal.
4418 * @param result The value to store in the result field of the signal.
4419 *
4420 * @retval 0 The signal was delivered successfully.
4421 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004422 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004423 */
4424
Andrew Boie3772f772018-05-07 16:52:57 -07004425__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004426
Anas Nashif954d5502018-02-25 08:37:28 -06004427/**
4428 * @internal
4429 */
Andy Ross8606fab2018-03-26 10:54:40 -07004430extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004431
Anas Nashif166f5192018-02-25 08:02:36 -06004432/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004433
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004434/**
4435 * @brief Make the CPU idle.
4436 *
4437 * This function makes the CPU idle until an event wakes it up.
4438 *
4439 * In a regular system, the idle thread should be the only thread responsible
4440 * for making the CPU idle and triggering any type of power management.
4441 * However, in some more constrained systems, such as a single-threaded system,
4442 * the only thread would be responsible for this if needed.
4443 *
4444 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004445 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004446 */
4447extern void k_cpu_idle(void);
4448
4449/**
4450 * @brief Make the CPU idle in an atomic fashion.
4451 *
4452 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4453 * must be done atomically before making the CPU idle.
4454 *
4455 * @param key Interrupt locking key obtained from irq_lock().
4456 *
4457 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004458 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004459 */
4460extern void k_cpu_atomic_idle(unsigned int key);
4461
Anas Nashif954d5502018-02-25 08:37:28 -06004462
4463/**
4464 * @internal
4465 */
Kumar Galacc334c72017-04-21 10:55:34 -05004466extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004467
Andrew Boiecdb94d62017-04-18 15:22:05 -07004468#ifdef _ARCH_EXCEPT
4469/* This archtecture has direct support for triggering a CPU exception */
4470#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4471#else
4472
Andrew Boiecdb94d62017-04-18 15:22:05 -07004473/* NOTE: This is the implementation for arches that do not implement
4474 * _ARCH_EXCEPT() to generate a real CPU exception.
4475 *
4476 * We won't have a real exception frame to determine the PC value when
4477 * the oops occurred, so print file and line number before we jump into
4478 * the fatal error handler.
4479 */
4480#define _k_except_reason(reason) do { \
4481 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4482 _NanoFatalErrorHandler(reason, &_default_esf); \
4483 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004484 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004485
4486#endif /* _ARCH__EXCEPT */
4487
4488/**
4489 * @brief Fatally terminate a thread
4490 *
4491 * This should be called when a thread has encountered an unrecoverable
4492 * runtime condition and needs to terminate. What this ultimately
4493 * means is determined by the _fatal_error_handler() implementation, which
4494 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4495 *
4496 * If this is called from ISR context, the default system fatal error handler
4497 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004498 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004499 */
4500#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4501
4502/**
4503 * @brief Fatally terminate the system
4504 *
4505 * This should be called when the Zephyr kernel has encountered an
4506 * unrecoverable runtime condition and needs to terminate. What this ultimately
4507 * means is determined by the _fatal_error_handler() implementation, which
4508 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004509 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004510 */
4511#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4512
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004513/*
4514 * private APIs that are utilized by one or more public APIs
4515 */
4516
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004517#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004518/**
4519 * @internal
4520 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004521extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004522#else
Anas Nashif954d5502018-02-25 08:37:28 -06004523/**
4524 * @internal
4525 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004526#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004527#endif
4528
Anas Nashif954d5502018-02-25 08:37:28 -06004529/**
4530 * @internal
4531 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004532extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004533/**
4534 * @internal
4535 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004536extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004537
Andrew Boiedc5d9352017-06-02 12:56:47 -07004538/* arch/cpu.h may declare an architecture or platform-specific macro
4539 * for properly declaring stacks, compatible with MMU/MPU constraints if
4540 * enabled
4541 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004542
4543/**
4544 * @brief Obtain an extern reference to a stack
4545 *
4546 * This macro properly brings the symbol of a thread stack declared
4547 * elsewhere into scope.
4548 *
4549 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004550 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004551 */
4552#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4553
Andrew Boiedc5d9352017-06-02 12:56:47 -07004554#ifdef _ARCH_THREAD_STACK_DEFINE
4555#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4556#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4557 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304558#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004559#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4560#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004561static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004562{
4563 return _ARCH_THREAD_STACK_BUFFER(sym);
4564}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004565#else
4566/**
4567 * @brief Declare a toplevel thread stack memory region
4568 *
4569 * This declares a region of memory suitable for use as a thread's stack.
4570 *
4571 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4572 * 'noinit' section so that it isn't zeroed at boot
4573 *
Andrew Boie507852a2017-07-25 18:47:07 -07004574 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004575 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004576 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004577 *
4578 * It is legal to precede this definition with the 'static' keyword.
4579 *
4580 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4581 * parameter of k_thread_create(), it may not be the same as the
4582 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4583 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004584 * Some arches may round the size of the usable stack region up to satisfy
4585 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4586 * size.
4587 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004588 * @param sym Thread stack symbol name
4589 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004590 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004591 */
4592#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004593 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004594
4595/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304596 * @brief Calculate size of stacks to be allocated in a stack array
4597 *
4598 * This macro calculates the size to be allocated for the stacks
4599 * inside a stack array. It accepts the indicated "size" as a parameter
4600 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4601 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4602 *
4603 * @param size Size of the stack memory region
4604 * @req K-TSTACK-001
4605 */
4606#define K_THREAD_STACK_LEN(size) (size)
4607
4608/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004609 * @brief Declare a toplevel array of thread stack memory regions
4610 *
4611 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4612 * definition for additional details and constraints.
4613 *
4614 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4615 * 'noinit' section so that it isn't zeroed at boot
4616 *
4617 * @param sym Thread stack symbol name
4618 * @param nmemb Number of stacks to declare
4619 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004620 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004621 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004622#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004623 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304624 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004625
4626/**
4627 * @brief Declare an embedded stack memory region
4628 *
4629 * Used for stacks embedded within other data structures. Use is highly
4630 * discouraged but in some cases necessary. For memory protection scenarios,
4631 * it is very important that any RAM preceding this member not be writable
4632 * by threads else a stack overflow will lead to silent corruption. In other
4633 * words, the containing data structure should live in RAM owned by the kernel.
4634 *
4635 * @param sym Thread stack symbol name
4636 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004637 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004638 */
4639#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004640 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004641
4642/**
4643 * @brief Return the size in bytes of a stack memory region
4644 *
4645 * Convenience macro for passing the desired stack size to k_thread_create()
4646 * since the underlying implementation may actually create something larger
4647 * (for instance a guard area).
4648 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004649 * The value returned here is not guaranteed to match the 'size' parameter
4650 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004651 *
4652 * @param sym Stack memory symbol
4653 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004654 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004655 */
4656#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4657
4658/**
4659 * @brief Get a pointer to the physical stack buffer
4660 *
4661 * Convenience macro to get at the real underlying stack buffer used by
4662 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4663 * This is really only intended for diagnostic tools which want to examine
4664 * stack memory contents.
4665 *
4666 * @param sym Declared stack symbol name
4667 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004668 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004669 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004670static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004671{
4672 return (char *)sym;
4673}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004674
4675#endif /* _ARCH_DECLARE_STACK */
4676
Chunlin Hane9c97022017-07-07 20:29:30 +08004677/**
4678 * @defgroup mem_domain_apis Memory domain APIs
4679 * @ingroup kernel_apis
4680 * @{
4681 */
4682
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004683/**
4684 * @def MEM_PARTITION_ENTRY
4685 * @brief Used to declare a memory partition entry
4686 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004687 */
4688#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4689 {\
4690 .start = _start, \
4691 .size = _size, \
4692 .attr = _attr, \
4693 }
4694
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004695/**
4696 * @def K_MEM_PARTITION_DEFINE
4697 * @brief Used to declare a memory partition
4698 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004699 */
4700#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4701#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4702 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304703 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004704 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4705#else
4706#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304707 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004708 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4709#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4710
Chunlin Hane9c97022017-07-07 20:29:30 +08004711/* memory partition */
4712struct k_mem_partition {
4713 /* start address of memory partition */
4714 u32_t start;
4715 /* size of memory partition */
4716 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304717#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004718 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304719 k_mem_partition_attr_t attr;
4720#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004721};
4722
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004723/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304724 * Note: Always declare this structure with __kernel prefix
4725 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004726struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304727#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004728 /* partitions in the domain */
4729 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304730#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004731 /* domain q */
4732 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004733 /* number of partitions in the domain */
4734 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004735};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304736
Chunlin Hane9c97022017-07-07 20:29:30 +08004737
4738/**
4739 * @brief Initialize a memory domain.
4740 *
4741 * Initialize a memory domain with given name and memory partitions.
4742 *
4743 * @param domain The memory domain to be initialized.
4744 * @param num_parts The number of array items of "parts" parameter.
4745 * @param parts An array of pointers to the memory partitions. Can be NULL
4746 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004747 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004748 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004749extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004750 struct k_mem_partition *parts[]);
4751/**
4752 * @brief Destroy a memory domain.
4753 *
4754 * Destroy a memory domain.
4755 *
4756 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004757 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004758 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004759extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4760
4761/**
4762 * @brief Add a memory partition into a memory domain.
4763 *
4764 * Add a memory partition into a memory domain.
4765 *
4766 * @param domain The memory domain to be added a memory partition.
4767 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004768 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004769 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004770extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4771 struct k_mem_partition *part);
4772
4773/**
4774 * @brief Remove a memory partition from a memory domain.
4775 *
4776 * Remove a memory partition from a memory domain.
4777 *
4778 * @param domain The memory domain to be removed a memory partition.
4779 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004780 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004781 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004782extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4783 struct k_mem_partition *part);
4784
4785/**
4786 * @brief Add a thread into a memory domain.
4787 *
4788 * Add a thread into a memory domain.
4789 *
4790 * @param domain The memory domain that the thread is going to be added into.
4791 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004792 *
4793 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004794 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004795extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4796 k_tid_t thread);
4797
4798/**
4799 * @brief Remove a thread from its memory domain.
4800 *
4801 * Remove a thread from its memory domain.
4802 *
4803 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004804 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004805 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004806extern void k_mem_domain_remove_thread(k_tid_t thread);
4807
Anas Nashif166f5192018-02-25 08:02:36 -06004808/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004809
Andrew Boie756f9072017-10-10 16:01:49 -07004810/**
4811 * @brief Emit a character buffer to the console device
4812 *
4813 * @param c String of characters to print
4814 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004815 *
4816 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004817 */
4818__syscall void k_str_out(char *c, size_t n);
4819
Andy Rosse7172672018-01-24 15:48:32 -08004820/**
4821 * @brief Start a numbered CPU on a MP-capable system
4822
4823 * This starts and initializes a specific CPU. The main thread on
4824 * startup is running on CPU zero, other processors are numbered
4825 * sequentially. On return from this function, the CPU is known to
4826 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004827 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004828 * with the provided key will work to enable them.
4829 *
4830 * Normally, in SMP mode this function will be called by the kernel
4831 * initialization and should not be used as a user API. But it is
4832 * defined here for special-purpose apps which want Zephyr running on
4833 * one core and to use others for design-specific processing.
4834 *
4835 * @param cpu_num Integer number of the CPU
4836 * @param stack Stack memory for the CPU
4837 * @param sz Stack buffer size, in bytes
4838 * @param fn Function to begin running on the CPU. First argument is
4839 * an irq_unlock() key.
4840 * @param arg Untyped argument to be passed to "fn"
4841 */
4842extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4843 void (*fn)(int, void *), void *arg);
4844
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004845#ifdef __cplusplus
4846}
4847#endif
4848
Andrew Boiee004dec2016-11-07 09:01:19 -08004849#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4850/*
4851 * Define new and delete operators.
4852 * At this moment, the operators do nothing since objects are supposed
4853 * to be statically allocated.
4854 */
4855inline void operator delete(void *ptr)
4856{
4857 (void)ptr;
4858}
4859
4860inline void operator delete[](void *ptr)
4861{
4862 (void)ptr;
4863}
4864
4865inline void *operator new(size_t size)
4866{
4867 (void)size;
4868 return NULL;
4869}
4870
4871inline void *operator new[](size_t size)
4872{
4873 (void)size;
4874 return NULL;
4875}
4876
4877/* Placement versions of operator new and delete */
4878inline void operator delete(void *ptr1, void *ptr2)
4879{
4880 (void)ptr1;
4881 (void)ptr2;
4882}
4883
4884inline void operator delete[](void *ptr1, void *ptr2)
4885{
4886 (void)ptr1;
4887 (void)ptr2;
4888}
4889
4890inline void *operator new(size_t size, void *ptr)
4891{
4892 (void)size;
4893 return ptr;
4894}
4895
4896inline void *operator new[](size_t size, void *ptr)
4897{
4898 (void)size;
4899 return ptr;
4900}
4901
4902#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4903
Anas Nashifb6304e62018-07-04 08:03:03 -05004904#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004905#include <syscalls/kernel.h>
4906
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004907#endif /* !_ASMLANGUAGE */
4908
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004909#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */