blob: faa03f6a3cfd515fd76e483c53b6ab5ce390ab02 [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
Flavio Ceolind1ed3362018-12-07 11:39:13 -080099#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#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
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800103#define _OBJECT_TRACING_NEXT_PTR(type)
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 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500202 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700203 */
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;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400580enum execution_context_types {
581 K_ISR = 0,
582 K_COOP_THREAD,
583 K_PREEMPT_THREAD,
584};
585
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400586/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100587 * @defgroup profiling_apis Profiling APIs
588 * @ingroup kernel_apis
589 * @{
590 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530591typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
592 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100593
594/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530595 * @brief Iterate over all the threads in the system.
596 *
597 * This routine iterates over all the threads in the system and
598 * calls the user_cb function for each thread.
599 *
600 * @param user_cb Pointer to the user callback function.
601 * @param user_data Pointer to user data.
602 *
603 * @note CONFIG_THREAD_MONITOR must be set for this function
604 * to be effective. Also this API uses irq_lock to protect the
605 * _kernel.threads list which means creation of new threads and
606 * terminations of existing threads are blocked until this
607 * API returns.
608 *
609 * @return N/A
610 */
611extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
612
Anas Nashif166f5192018-02-25 08:02:36 -0600613/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100614
615/**
Allan Stephensc98da842016-11-11 15:45:03 -0500616 * @defgroup thread_apis Thread APIs
617 * @ingroup kernel_apis
618 * @{
619 */
620
Benjamin Walshed240f22017-01-22 13:05:08 -0500621#endif /* !_ASMLANGUAGE */
622
623
624/*
625 * Thread user options. May be needed by assembly code. Common part uses low
626 * bits, arch-specific use high bits.
627 */
628
Anas Nashifa541e932018-05-24 11:19:16 -0500629/**
630 * @brief system thread that must not abort
631 * @req K-THREAD-000
632 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700633#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500634
635#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500636/**
637 * @brief thread uses floating point registers
638 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700639#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500640#endif
641
Anas Nashifa541e932018-05-24 11:19:16 -0500642/**
643 * @brief user mode thread
644 *
645 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700646 * has additional restrictions
647 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700648#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700649
Anas Nashifa541e932018-05-24 11:19:16 -0500650/**
651 * @brief Inherit Permissions
652 *
653 * @details
654 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700655 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
656 * is not enabled.
657 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700658#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700659
Benjamin Walshed240f22017-01-22 13:05:08 -0500660#ifdef CONFIG_X86
661/* x86 Bitmask definitions for threads user options */
662
663#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
664/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700665#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500666#endif
667#endif
668
669/* end - thread options */
670
671#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400672/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700673 * @brief Create a thread.
674 *
675 * This routine initializes a thread, then schedules it for execution.
676 *
677 * The new thread may be scheduled for immediate execution or a delayed start.
678 * If the newly spawned thread does not have a delayed start the kernel
679 * scheduler may preempt the current thread to allow the new thread to
680 * execute.
681 *
682 * Thread options are architecture-specific, and can include K_ESSENTIAL,
683 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
684 * them using "|" (the logical OR operator).
685 *
686 * Historically, users often would use the beginning of the stack memory region
687 * to store the struct k_thread data, although corruption will occur if the
688 * stack overflows this region and stack protection features may not detect this
689 * situation.
690 *
691 * @param new_thread Pointer to uninitialized struct k_thread
692 * @param stack Pointer to the stack space.
693 * @param stack_size Stack size in bytes.
694 * @param entry Thread entry function.
695 * @param p1 1st entry point parameter.
696 * @param p2 2nd entry point parameter.
697 * @param p3 3rd entry point parameter.
698 * @param prio Thread priority.
699 * @param options Thread options.
700 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
701 *
702 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400703 *
704 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700705 */
Andrew Boie662c3452017-10-02 10:51:18 -0700706__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700707 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700708 size_t stack_size,
709 k_thread_entry_t entry,
710 void *p1, void *p2, void *p3,
711 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700712
Andrew Boie3f091b52017-08-30 14:34:14 -0700713/**
714 * @brief Drop a thread's privileges permanently to user mode
715 *
716 * @param entry Function to start executing from
717 * @param p1 1st entry point parameter
718 * @param p2 2nd entry point parameter
719 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400720 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700721 */
722extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
723 void *p1, void *p2,
724 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700725
Andrew Boied26cf2d2017-03-30 13:07:02 -0700726/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530727 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700728 *
729 * This is a convenience function. For the provided thread, grant access to
730 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700731 *
732 * The thread object must be initialized (i.e. running). The objects don't
733 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530734 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700735 *
736 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530737 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400738 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700739 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530740#define k_thread_access_grant(thread, ...) \
741 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700742
743/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700744 * @brief Assign a resource memory pool to a thread
745 *
746 * By default, threads have no resource pool assigned unless their parent
747 * thread has a resource pool, in which case it is inherited. Multiple
748 * threads may be assigned to the same memory pool.
749 *
750 * Changing a thread's resource pool will not migrate allocations from the
751 * previous pool.
752 *
753 * @param thread Target thread to assign a memory pool for resource requests,
754 * or NULL if the thread should no longer have a memory pool.
755 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400756 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700757 */
758static inline void k_thread_resource_pool_assign(struct k_thread *thread,
759 struct k_mem_pool *pool)
760{
761 thread->resource_pool = pool;
762}
763
764#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
765/**
766 * @brief Assign the system heap as a thread's resource pool
767 *
768 * Similar to k_thread_resource_pool_assign(), but the thread will use
769 * the kernel heap to draw memory.
770 *
771 * Use with caution, as a malicious thread could perform DoS attacks on the
772 * kernel heap.
773 *
774 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400775 *
776 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700777 */
778void k_thread_system_pool_assign(struct k_thread *thread);
779#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
780
781/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 *
Allan Stephensc98da842016-11-11 15:45:03 -0500784 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500785 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200789 * @return Zero if the requested time has elapsed or the number of milliseconds
790 * left to sleep, if thread was woken up by \ref k_wakeup call.
791 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 */
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200793__syscall s32_t k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794
795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
798 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 * @return N/A
802 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800803__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804
805/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500808 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 *
812 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400813 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 */
Andrew Boie468190a2017-09-29 14:00:48 -0700815__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816
817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * If @a thread is not currently sleeping, the routine has no effect.
823 *
824 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
826 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400827 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 */
Andrew Boie468190a2017-09-29 14:00:48 -0700829__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830
831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400835 *
836 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700838__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839
840/**
Allan Stephensc98da842016-11-11 15:45:03 -0500841 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * This routine permanently stops execution of @a thread. The thread is taken
844 * off all kernel queues it is part of (i.e. the ready queue, the timeout
845 * queue, or a kernel object wait queue). However, any kernel resources the
846 * thread might currently own (such as mutexes or memory blocks) are not
847 * released. It is the responsibility of the caller of this routine to ensure
848 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
852 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400853 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 */
Andrew Boie468190a2017-09-29 14:00:48 -0700855__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400856
Andrew Boie7d627c52017-08-30 11:01:56 -0700857
858/**
859 * @brief Start an inactive thread
860 *
861 * If a thread was created with K_FOREVER in the delay parameter, it will
862 * not be added to the scheduling queue until this function is called
863 * on it.
864 *
865 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400866 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700867 */
Andrew Boie468190a2017-09-29 14:00:48 -0700868__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700869
Allan Stephensc98da842016-11-11 15:45:03 -0500870/**
871 * @cond INTERNAL_HIDDEN
872 */
873
Benjamin Walshd211a522016-12-06 11:44:01 -0500874/* timeout has timed out and is not on _timeout_q anymore */
875#define _EXPIRED (-2)
876
877/* timeout is not in use */
878#define _INACTIVE (-1)
879
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400880struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700881 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700882 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400883 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700884 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500885 void *init_p1;
886 void *init_p2;
887 void *init_p3;
888 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500889 u32_t init_options;
890 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500891 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600892 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400893};
894
Andrew Boied26cf2d2017-03-30 13:07:02 -0700895#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400896 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600897 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500898 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700899 .init_thread = (thread), \
900 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500901 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700902 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400903 .init_p1 = (void *)p1, \
904 .init_p2 = (void *)p2, \
905 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500906 .init_prio = (prio), \
907 .init_options = (options), \
908 .init_delay = (delay), \
909 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600910 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400911 }
912
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400913/**
Allan Stephensc98da842016-11-11 15:45:03 -0500914 * INTERNAL_HIDDEN @endcond
915 */
916
917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @brief Statically define and initialize a thread.
919 *
920 * The thread may be scheduled for immediate execution or a delayed start.
921 *
922 * Thread options are architecture-specific, and can include K_ESSENTIAL,
923 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
924 * them using "|" (the logical OR operator).
925 *
926 * The ID of the thread can be accessed using:
927 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500928 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500929 *
930 * @param name Name of the thread.
931 * @param stack_size Stack size in bytes.
932 * @param entry Thread entry function.
933 * @param p1 1st entry point parameter.
934 * @param p2 2nd entry point parameter.
935 * @param p3 3rd entry point parameter.
936 * @param prio Thread priority.
937 * @param options Thread options.
938 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400939 *
Anas Nashif47420d02018-05-24 14:20:56 -0400940 * @req K-THREAD-010
941 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400942 * @internal It has been observed that the x86 compiler by default aligns
943 * these _static_thread_data structures to 32-byte boundaries, thereby
944 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400945 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400946 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500947#define K_THREAD_DEFINE(name, stack_size, \
948 entry, p1, p2, p3, \
949 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700950 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700951 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500952 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500953 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700954 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
955 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500956 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600957 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700958 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400959
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500961 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500963 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * @param thread ID of thread whose priority is needed.
966 *
967 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400968 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700970__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971
972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500973 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 *
977 * Rescheduling can occur immediately depending on the priority @a thread is
978 * set to:
979 *
980 * - If its priority is raised above the priority of the caller of this
981 * function, and the caller is preemptible, @a thread will be scheduled in.
982 *
983 * - If the caller operates on itself, it lowers its priority below that of
984 * other threads in the system, and the caller is preemptible, the thread of
985 * highest priority will be scheduled in.
986 *
987 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
988 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
989 * highest priority.
990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 * @param prio New priority.
993 *
994 * @warning Changing the priority of a thread currently involved in mutex
995 * priority inheritance may result in undefined behavior.
996 *
997 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400998 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 */
Andrew Boie468190a2017-09-29 14:00:48 -07001000__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001001
Andy Ross4a2e50f2018-05-15 11:06:25 -07001002
1003#ifdef CONFIG_SCHED_DEADLINE
1004/**
1005 * @brief Set deadline expiration time for scheduler
1006 *
1007 * This sets the "deadline" expiration as a time delta from the
1008 * current time, in the same units used by k_cycle_get_32(). The
1009 * scheduler (when deadline scheduling is enabled) will choose the
1010 * next expiring thread when selecting between threads at the same
1011 * static priority. Threads at different priorities will be scheduled
1012 * according to their static priority.
1013 *
1014 * @note Deadlines that are negative (i.e. in the past) are still seen
1015 * as higher priority than others, even if the thread has "finished"
1016 * its work. If you don't want it scheduled anymore, you have to
1017 * reset the deadline into the future, block/pend the thread, or
1018 * modify its priority with k_thread_priority_set().
1019 *
1020 * @note Despite the API naming, the scheduler makes no guarantees the
1021 * the thread WILL be scheduled within that deadline, nor does it take
1022 * extra metadata (like e.g. the "runtime" and "period" parameters in
1023 * Linux sched_setattr()) that allows the kernel to validate the
1024 * scheduling for achievability. Such features could be implemented
1025 * above this call, which is simply input to the priority selection
1026 * logic.
1027 *
1028 * @param thread A thread on which to set the deadline
1029 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001030 *
1031 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001032 */
1033__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1034#endif
1035
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001037 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * This routine prevents the kernel scheduler from making @a thread the
1040 * current thread. All other internal operations on @a thread are still
1041 * performed; for example, any timeout it is waiting on keeps ticking,
1042 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * If @a thread is already suspended, the routine has no effect.
1045 *
1046 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
1048 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001049 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 */
Andrew Boie468190a2017-09-29 14:00:48 -07001051__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052
1053/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * This routine allows the kernel scheduler to make @a thread the current
1057 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001059 * If @a thread is not currently suspended, the routine has no effect.
1060 *
1061 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001062 *
1063 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001064 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001065 */
Andrew Boie468190a2017-09-29 14:00:48 -07001066__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001071 * This routine specifies how the scheduler will perform time slicing of
1072 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * To enable time slicing, @a slice must be non-zero. The scheduler
1075 * ensures that no thread runs for more than the specified time limit
1076 * before other threads of that priority are given a chance to execute.
1077 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001078 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001080 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 * execute. Once the scheduler selects a thread for execution, there is no
1082 * minimum guaranteed time the thread will execute before threads of greater or
1083 * equal priority are scheduled.
1084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001085 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086 * for execution, this routine has no effect; the thread is immediately
1087 * rescheduled after the slice period expires.
1088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * To disable timeslicing, set both @a slice and @a prio to zero.
1090 *
1091 * @param slice Maximum time slice length (in milliseconds).
1092 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
1094 * @return N/A
1095 */
Kumar Galacc334c72017-04-21 10:55:34 -05001096extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097
Anas Nashif166f5192018-02-25 08:02:36 -06001098/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001099
1100/**
1101 * @addtogroup isr_apis
1102 * @{
1103 */
1104
1105/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001106 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107 *
Allan Stephensc98da842016-11-11 15:45:03 -05001108 * This routine allows the caller to customize its actions, depending on
1109 * whether it is a thread or an ISR.
1110 *
1111 * @note Can be called by ISRs.
1112 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001113 * @return false if invoked by a thread.
1114 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001116extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117
Benjamin Walsh445830d2016-11-10 15:54:27 -05001118/**
1119 * @brief Determine if code is running in a preemptible thread.
1120 *
Allan Stephensc98da842016-11-11 15:45:03 -05001121 * This routine allows the caller to customize its actions, depending on
1122 * whether it can be preempted by another thread. The routine returns a 'true'
1123 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001124 *
Allan Stephensc98da842016-11-11 15:45:03 -05001125 * - The code is running in a thread, not at ISR.
1126 * - The thread's priority is in the preemptible range.
1127 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001128 *
Allan Stephensc98da842016-11-11 15:45:03 -05001129 * @note Can be called by ISRs.
1130 *
1131 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001132 * @return Non-zero if invoked by a preemptible thread.
1133 */
Andrew Boie468190a2017-09-29 14:00:48 -07001134__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001135
Allan Stephensc98da842016-11-11 15:45:03 -05001136/**
Anas Nashif166f5192018-02-25 08:02:36 -06001137 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001138 */
1139
1140/**
1141 * @addtogroup thread_apis
1142 * @{
1143 */
1144
1145/**
1146 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001147 *
Allan Stephensc98da842016-11-11 15:45:03 -05001148 * This routine prevents the current thread from being preempted by another
1149 * thread by instructing the scheduler to treat it as a cooperative thread.
1150 * If the thread subsequently performs an operation that makes it unready,
1151 * it will be context switched out in the normal manner. When the thread
1152 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001153 *
Allan Stephensc98da842016-11-11 15:45:03 -05001154 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001155 *
Allan Stephensc98da842016-11-11 15:45:03 -05001156 * @note k_sched_lock() and k_sched_unlock() should normally be used
1157 * when the operation being performed can be safely interrupted by ISRs.
1158 * However, if the amount of processing involved is very small, better
1159 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001160 *
1161 * @return N/A
1162 */
1163extern void k_sched_lock(void);
1164
Allan Stephensc98da842016-11-11 15:45:03 -05001165/**
1166 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001167 *
Allan Stephensc98da842016-11-11 15:45:03 -05001168 * This routine reverses the effect of a previous call to k_sched_lock().
1169 * A thread must call the routine once for each time it called k_sched_lock()
1170 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001171 *
1172 * @return N/A
1173 */
1174extern void k_sched_unlock(void);
1175
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001177 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * Custom data is not used by the kernel itself, and is freely available
1182 * for a thread to use as it sees fit. It can be used as a framework
1183 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 *
1187 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001188 *
1189 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001190 */
Andrew Boie468190a2017-09-29 14:00:48 -07001191__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001192
1193/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001194 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001196 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001198 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001199 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 */
Andrew Boie468190a2017-09-29 14:00:48 -07001201__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001202
1203/**
Anas Nashif57554052018-03-03 02:31:05 -06001204 * @brief Set current thread name
1205 *
1206 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1207 * tracing and debugging.
1208 *
1209 */
1210__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1211
1212/**
1213 * @brief Get thread name
1214 *
1215 * Get the name of a thread
1216 *
1217 * @param thread_id Thread ID
1218 *
1219 */
1220__syscall const char *k_thread_name_get(k_tid_t thread_id);
1221
1222/**
Andy Rosscfe62032018-09-29 07:34:55 -07001223 * @}
1224 */
1225
1226/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001227 * @addtogroup clock_apis
1228 * @{
1229 */
1230
1231/**
1232 * @brief Generate null timeout delay.
1233 *
1234 * This macro generates a timeout delay that that instructs a kernel API
1235 * not to wait if the requested operation cannot be performed immediately.
1236 *
1237 * @return Timeout delay value.
1238 */
1239#define K_NO_WAIT 0
1240
1241/**
1242 * @brief Generate timeout delay from milliseconds.
1243 *
1244 * This macro generates a timeout delay that that instructs a kernel API
1245 * to wait up to @a ms milliseconds to perform the requested operation.
1246 *
1247 * @param ms Duration in milliseconds.
1248 *
1249 * @return Timeout delay value.
1250 */
Johan Hedberg14471692016-11-13 10:52:15 +02001251#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001252
1253/**
1254 * @brief Generate timeout delay from seconds.
1255 *
1256 * This macro generates a timeout delay that that instructs a kernel API
1257 * to wait up to @a s seconds to perform the requested operation.
1258 *
1259 * @param s Duration in seconds.
1260 *
1261 * @return Timeout delay value.
1262 */
Johan Hedberg14471692016-11-13 10:52:15 +02001263#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001264
1265/**
1266 * @brief Generate timeout delay from minutes.
1267 *
1268 * This macro generates a timeout delay that that instructs a kernel API
1269 * to wait up to @a m minutes to perform the requested operation.
1270 *
1271 * @param m Duration in minutes.
1272 *
1273 * @return Timeout delay value.
1274 */
Johan Hedberg14471692016-11-13 10:52:15 +02001275#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001276
1277/**
1278 * @brief Generate timeout delay from hours.
1279 *
1280 * This macro generates a timeout delay that that instructs a kernel API
1281 * to wait up to @a h hours to perform the requested operation.
1282 *
1283 * @param h Duration in hours.
1284 *
1285 * @return Timeout delay value.
1286 */
Johan Hedberg14471692016-11-13 10:52:15 +02001287#define K_HOURS(h) K_MINUTES((h) * 60)
1288
Allan Stephensc98da842016-11-11 15:45:03 -05001289/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001290 * @brief Generate infinite timeout delay.
1291 *
1292 * This macro generates a timeout delay that that instructs a kernel API
1293 * to wait as long as necessary to perform the requested operation.
1294 *
1295 * @return Timeout delay value.
1296 */
1297#define K_FOREVER (-1)
1298
1299/**
Anas Nashif166f5192018-02-25 08:02:36 -06001300 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001301 */
1302
1303/**
Allan Stephensc98da842016-11-11 15:45:03 -05001304 * @cond INTERNAL_HIDDEN
1305 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001306
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001307struct k_timer {
1308 /*
1309 * _timeout structure must be first here if we want to use
1310 * dynamic timer allocation. timeout.node is used in the double-linked
1311 * list of free timers
1312 */
1313 struct _timeout timeout;
1314
Allan Stephens45bfa372016-10-12 12:39:42 -05001315 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001316 _wait_q_t wait_q;
1317
1318 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001319 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001320
1321 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001322 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001323
1324 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001325 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001326
Allan Stephens45bfa372016-10-12 12:39:42 -05001327 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001328 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001329
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001330 /* user-specific data, also used to support legacy features */
1331 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001332
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001333 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001334};
1335
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001336#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001337 { \
Andy Ross987c0e52018-09-27 16:50:00 -07001338 .timeout.dticks = _INACTIVE, \
1339 .timeout.fn = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001340 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001341 .expiry_fn = expiry, \
1342 .stop_fn = stop, \
1343 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001344 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001345 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001346 }
1347
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001348#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1349
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001350/**
Allan Stephensc98da842016-11-11 15:45:03 -05001351 * INTERNAL_HIDDEN @endcond
1352 */
1353
1354/**
1355 * @defgroup timer_apis Timer APIs
1356 * @ingroup kernel_apis
1357 * @{
1358 */
1359
1360/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001361 * @typedef k_timer_expiry_t
1362 * @brief Timer expiry function type.
1363 *
1364 * A timer's expiry function is executed by the system clock interrupt handler
1365 * each time the timer expires. The expiry function is optional, and is only
1366 * invoked if the timer has been initialized with one.
1367 *
1368 * @param timer Address of timer.
1369 *
1370 * @return N/A
1371 */
1372typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1373
1374/**
1375 * @typedef k_timer_stop_t
1376 * @brief Timer stop function type.
1377 *
1378 * A timer's stop function is executed if the timer is stopped prematurely.
1379 * The function runs in the context of the thread that stops the timer.
1380 * The stop function is optional, and is only invoked if the timer has been
1381 * initialized with one.
1382 *
1383 * @param timer Address of timer.
1384 *
1385 * @return N/A
1386 */
1387typedef void (*k_timer_stop_t)(struct k_timer *timer);
1388
1389/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001390 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001392 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001394 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 *
1396 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001397 * @param expiry_fn Function to invoke each time the timer expires.
1398 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001399 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001400#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001401 struct k_timer name \
1402 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001403 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404
Allan Stephens45bfa372016-10-12 12:39:42 -05001405/**
1406 * @brief Initialize a timer.
1407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001408 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001409 *
1410 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001411 * @param expiry_fn Function to invoke each time the timer expires.
1412 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001413 *
1414 * @return N/A
1415 */
1416extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001417 k_timer_expiry_t expiry_fn,
1418 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001419
Allan Stephens45bfa372016-10-12 12:39:42 -05001420/**
1421 * @brief Start a timer.
1422 *
1423 * This routine starts a timer, and resets its status to zero. The timer
1424 * begins counting down using the specified duration and period values.
1425 *
1426 * Attempting to start a timer that is already running is permitted.
1427 * The timer's status is reset to zero and the timer begins counting down
1428 * using the new duration and period values.
1429 *
1430 * @param timer Address of timer.
1431 * @param duration Initial timer duration (in milliseconds).
1432 * @param period Timer period (in milliseconds).
1433 *
1434 * @return N/A
1435 */
Andrew Boiea354d492017-09-29 16:22:28 -07001436__syscall void k_timer_start(struct k_timer *timer,
1437 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001438
1439/**
1440 * @brief Stop a timer.
1441 *
1442 * This routine stops a running timer prematurely. The timer's stop function,
1443 * if one exists, is invoked by the caller.
1444 *
1445 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001446 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001447 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001448 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1449 * if @a k_timer_stop is to be called from ISRs.
1450 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001451 * @param timer Address of timer.
1452 *
1453 * @return N/A
1454 */
Andrew Boiea354d492017-09-29 16:22:28 -07001455__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001456
1457/**
1458 * @brief Read timer status.
1459 *
1460 * This routine reads the timer's status, which indicates the number of times
1461 * it has expired since its status was last read.
1462 *
1463 * Calling this routine resets the timer's status to zero.
1464 *
1465 * @param timer Address of timer.
1466 *
1467 * @return Timer status.
1468 */
Andrew Boiea354d492017-09-29 16:22:28 -07001469__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001470
1471/**
1472 * @brief Synchronize thread to timer expiration.
1473 *
1474 * This routine blocks the calling thread until the timer's status is non-zero
1475 * (indicating that it has expired at least once since it was last examined)
1476 * or the timer is stopped. If the timer status is already non-zero,
1477 * or the timer is already stopped, the caller continues without waiting.
1478 *
1479 * Calling this routine resets the timer's status to zero.
1480 *
1481 * This routine must not be used by interrupt handlers, since they are not
1482 * allowed to block.
1483 *
1484 * @param timer Address of timer.
1485 *
1486 * @return Timer status.
1487 */
Andrew Boiea354d492017-09-29 16:22:28 -07001488__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001489
Andy Ross52e444b2018-09-28 09:06:37 -07001490extern s32_t z_timeout_remaining(struct _timeout *timeout);
1491
Allan Stephens45bfa372016-10-12 12:39:42 -05001492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001493 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001494 *
1495 * This routine computes the (approximate) time remaining before a running
1496 * timer next expires. If the timer is not running, it returns zero.
1497 *
1498 * @param timer Address of timer.
1499 *
1500 * @return Remaining time (in milliseconds).
1501 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001502__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001503
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001504static inline u32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001505{
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001506 return (u32_t)__ticks_to_ms(z_timeout_remaining(&timer->timeout));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001507}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001508
Allan Stephensc98da842016-11-11 15:45:03 -05001509/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001510 * @brief Associate user-specific data with a timer.
1511 *
1512 * This routine records the @a user_data with the @a timer, to be retrieved
1513 * later.
1514 *
1515 * It can be used e.g. in a timer handler shared across multiple subsystems to
1516 * retrieve data specific to the subsystem this timer is associated with.
1517 *
1518 * @param timer Address of timer.
1519 * @param user_data User data to associate with the timer.
1520 *
1521 * @return N/A
1522 */
Andrew Boiea354d492017-09-29 16:22:28 -07001523__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1524
Anas Nashif954d5502018-02-25 08:37:28 -06001525/**
1526 * @internal
1527 */
Andrew Boiea354d492017-09-29 16:22:28 -07001528static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1529 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001530{
1531 timer->user_data = user_data;
1532}
1533
1534/**
1535 * @brief Retrieve the user-specific data from a timer.
1536 *
1537 * @param timer Address of timer.
1538 *
1539 * @return The user data.
1540 */
Andrew Boiea354d492017-09-29 16:22:28 -07001541__syscall void *k_timer_user_data_get(struct k_timer *timer);
1542
1543static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001544{
1545 return timer->user_data;
1546}
1547
Anas Nashif166f5192018-02-25 08:02:36 -06001548/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001549
Allan Stephensc98da842016-11-11 15:45:03 -05001550/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001551 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001552 * @{
1553 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001554
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001556 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001558 * This routine returns the elapsed time since the system booted,
1559 * in milliseconds.
1560 *
1561 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001562 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001563__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001564
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001565/**
1566 * @brief Enable clock always on in tickless kernel
1567 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001568 * This routine enables keeping the clock running (that is, it always
1569 * keeps an active timer interrupt scheduled) when there are no timer
1570 * events programmed in tickless kernel scheduling. This is necessary
1571 * if the clock is used to track passage of time (e.g. via
1572 * k_uptime_get_32()), otherwise the internal hardware counter may
1573 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001574 *
1575 * @retval prev_status Previous status of always on flag
1576 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001577int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001578
1579/**
1580 * @brief Disable clock always on in tickless kernel
1581 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001582 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001583 * there are no timer events programmed in tickless kernel
1584 * scheduling. To save power, this routine should be called
1585 * immediately when clock is not used to track time.
1586 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001587void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001588
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001591 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001592 * This routine returns the lower 32-bits of the elapsed time since the system
1593 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001595 * This routine can be more efficient than k_uptime_get(), as it reduces the
1596 * need for interrupt locking and 64-bit math. However, the 32-bit result
1597 * cannot hold a system uptime time larger than approximately 50 days, so the
1598 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001600 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001601 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001602__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001603
1604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * This routine computes the elapsed time between the current system uptime
1608 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001610 * @param reftime Pointer to a reference time, which is updated to the current
1611 * uptime upon return.
1612 *
1613 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001614 */
Andy Ross987c0e52018-09-27 16:50:00 -07001615static inline s64_t k_uptime_delta(s64_t *reftime)
1616{
1617 s64_t uptime, delta;
1618
1619 uptime = k_uptime_get();
1620 delta = uptime - *reftime;
1621 *reftime = uptime;
1622
1623 return delta;
1624}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001625
1626/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001627 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * This routine computes the elapsed time between the current system uptime
1630 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001632 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1633 * need for interrupt locking and 64-bit math. However, the 32-bit result
1634 * cannot hold an elapsed time larger than approximately 50 days, so the
1635 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001637 * @param reftime Pointer to a reference time, which is updated to the current
1638 * uptime upon return.
1639 *
1640 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001641 */
Andy Ross987c0e52018-09-27 16:50:00 -07001642static inline u32_t k_uptime_delta_32(s64_t *reftime)
1643{
1644 return (u32_t)k_uptime_delta(reftime);
1645}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001646
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001648 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * This routine returns the current time, as measured by the system's hardware
1651 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001655#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657/**
Anas Nashif166f5192018-02-25 08:02:36 -06001658 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001659 */
1660
Allan Stephensc98da842016-11-11 15:45:03 -05001661/**
1662 * @cond INTERNAL_HIDDEN
1663 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001664
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001665struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001666 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001667 union {
1668 _wait_q_t wait_q;
1669
1670 _POLL_EVENT;
1671 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001672
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001673 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001674};
1675
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001676#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001677 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001678 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001679 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001680 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001681 _OBJECT_TRACING_INIT \
1682 }
1683
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001684#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1685
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001686extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1687
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001688/**
1689 * INTERNAL_HIDDEN @endcond
1690 */
1691
1692/**
1693 * @defgroup queue_apis Queue APIs
1694 * @ingroup kernel_apis
1695 * @{
1696 */
1697
1698/**
1699 * @brief Initialize a queue.
1700 *
1701 * This routine initializes a queue object, prior to its first use.
1702 *
1703 * @param queue Address of the queue.
1704 *
1705 * @return N/A
1706 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001707__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001708
1709/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001710 * @brief Cancel waiting on a queue.
1711 *
1712 * This routine causes first thread pending on @a queue, if any, to
1713 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001714 * If the queue is being waited on by k_poll(), it will return with
1715 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1716 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001717 *
1718 * @note Can be called by ISRs.
1719 *
1720 * @param queue Address of the queue.
1721 *
1722 * @return N/A
1723 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001724__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001725
1726/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001727 * @brief Append an element to the end of a queue.
1728 *
1729 * This routine appends a data item to @a queue. A queue data item must be
1730 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1731 * reserved for the kernel's use.
1732 *
1733 * @note Can be called by ISRs.
1734 *
1735 * @param queue Address of the queue.
1736 * @param data Address of the data item.
1737 *
1738 * @return N/A
1739 */
1740extern void k_queue_append(struct k_queue *queue, void *data);
1741
1742/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001743 * @brief Append an element to a queue.
1744 *
1745 * This routine appends a data item to @a queue. There is an implicit
1746 * memory allocation from the calling thread's resource pool, which is
1747 * automatically freed when the item is removed from the queue.
1748 *
1749 * @note Can be called by ISRs.
1750 *
1751 * @param queue Address of the queue.
1752 * @param data Address of the data item.
1753 *
1754 * @retval 0 on success
1755 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1756 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301757__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001758
1759/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001760 * @brief Prepend an element to a queue.
1761 *
1762 * This routine prepends a data item to @a queue. A queue data item must be
1763 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1764 * reserved for the kernel's use.
1765 *
1766 * @note Can be called by ISRs.
1767 *
1768 * @param queue Address of the queue.
1769 * @param data Address of the data item.
1770 *
1771 * @return N/A
1772 */
1773extern void k_queue_prepend(struct k_queue *queue, void *data);
1774
1775/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001776 * @brief Prepend an element to a queue.
1777 *
1778 * This routine prepends a data item to @a queue. There is an implicit
1779 * memory allocation from the calling thread's resource pool, which is
1780 * automatically freed when the item is removed from the queue.
1781 *
1782 * @note Can be called by ISRs.
1783 *
1784 * @param queue Address of the queue.
1785 * @param data Address of the data item.
1786 *
1787 * @retval 0 on success
1788 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1789 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301790__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001791
1792/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001793 * @brief Inserts an element to a queue.
1794 *
1795 * This routine inserts a data item to @a queue after previous item. A queue
1796 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1797 * item are reserved for the kernel's use.
1798 *
1799 * @note Can be called by ISRs.
1800 *
1801 * @param queue Address of the queue.
1802 * @param prev Address of the previous data item.
1803 * @param data Address of the data item.
1804 *
1805 * @return N/A
1806 */
1807extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1808
1809/**
1810 * @brief Atomically append a list of elements to a queue.
1811 *
1812 * This routine adds a list of data items to @a queue in one operation.
1813 * The data items must be in a singly-linked list, with the first 32 bits
1814 * in each data item pointing to the next data item; the list must be
1815 * NULL-terminated.
1816 *
1817 * @note Can be called by ISRs.
1818 *
1819 * @param queue Address of the queue.
1820 * @param head Pointer to first node in singly-linked list.
1821 * @param tail Pointer to last node in singly-linked list.
1822 *
1823 * @return N/A
1824 */
1825extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1826
1827/**
1828 * @brief Atomically add a list of elements to a queue.
1829 *
1830 * This routine adds a list of data items to @a queue in one operation.
1831 * The data items must be in a singly-linked list implemented using a
1832 * sys_slist_t object. Upon completion, the original list is empty.
1833 *
1834 * @note Can be called by ISRs.
1835 *
1836 * @param queue Address of the queue.
1837 * @param list Pointer to sys_slist_t object.
1838 *
1839 * @return N/A
1840 */
1841extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1842
1843/**
1844 * @brief Get an element from a queue.
1845 *
1846 * This routine removes first data item from @a queue. The first 32 bits of the
1847 * data item are reserved for the kernel's use.
1848 *
1849 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1850 *
1851 * @param queue Address of the queue.
1852 * @param timeout Waiting period to obtain a data item (in milliseconds),
1853 * or one of the special values K_NO_WAIT and K_FOREVER.
1854 *
1855 * @return Address of the data item if successful; NULL if returned
1856 * without waiting, or waiting period timed out.
1857 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001858__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001859
1860/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001861 * @brief Remove an element from a queue.
1862 *
1863 * This routine removes data item from @a queue. The first 32 bits of the
1864 * data item are reserved for the kernel's use. Removing elements from k_queue
1865 * rely on sys_slist_find_and_remove which is not a constant time operation.
1866 *
1867 * @note Can be called by ISRs
1868 *
1869 * @param queue Address of the queue.
1870 * @param data Address of the data item.
1871 *
1872 * @return true if data item was removed
1873 */
1874static inline bool k_queue_remove(struct k_queue *queue, void *data)
1875{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001876 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001877}
1878
1879/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001880 * @brief Append an element to a queue only if it's not present already.
1881 *
1882 * This routine appends data item to @a queue. The first 32 bits of the
1883 * data item are reserved for the kernel's use. Appending elements to k_queue
1884 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1885 *
1886 * @note Can be called by ISRs
1887 *
1888 * @param queue Address of the queue.
1889 * @param data Address of the data item.
1890 *
1891 * @return true if data item was added, false if not
1892 */
1893static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1894{
1895 sys_sfnode_t *test;
1896
1897 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1898 if (test == (sys_sfnode_t *) data) {
1899 return false;
1900 }
1901 }
1902
1903 k_queue_append(queue, data);
1904 return true;
1905}
1906
1907/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001908 * @brief Query a queue to see if it has data available.
1909 *
1910 * Note that the data might be already gone by the time this function returns
1911 * if other threads are also trying to read from the queue.
1912 *
1913 * @note Can be called by ISRs.
1914 *
1915 * @param queue Address of the queue.
1916 *
1917 * @return Non-zero if the queue is empty.
1918 * @return 0 if data is available.
1919 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001920__syscall int k_queue_is_empty(struct k_queue *queue);
1921
1922static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001923{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001924 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001925}
1926
1927/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001928 * @brief Peek element at the head of queue.
1929 *
1930 * Return element from the head of queue without removing it.
1931 *
1932 * @param queue Address of the queue.
1933 *
1934 * @return Head element, or NULL if queue is empty.
1935 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001936__syscall void *k_queue_peek_head(struct k_queue *queue);
1937
1938static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001939{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001940 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001941}
1942
1943/**
1944 * @brief Peek element at the tail of queue.
1945 *
1946 * Return element from the tail of queue without removing it.
1947 *
1948 * @param queue Address of the queue.
1949 *
1950 * @return Tail element, or NULL if queue is empty.
1951 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001952__syscall void *k_queue_peek_tail(struct k_queue *queue);
1953
1954static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001955{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001956 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001957}
1958
1959/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001960 * @brief Statically define and initialize a queue.
1961 *
1962 * The queue can be accessed outside the module where it is defined using:
1963 *
1964 * @code extern struct k_queue <name>; @endcode
1965 *
1966 * @param name Name of the queue.
1967 */
1968#define K_QUEUE_DEFINE(name) \
1969 struct k_queue name \
1970 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001971 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001972
Anas Nashif166f5192018-02-25 08:02:36 -06001973/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001974
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001975struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001976 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977};
1978
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001979/**
1980 * @cond INTERNAL_HIDDEN
1981 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001982#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001983 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001984 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001985 }
1986
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001987#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1988
Allan Stephensc98da842016-11-11 15:45:03 -05001989/**
1990 * INTERNAL_HIDDEN @endcond
1991 */
1992
1993/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001994 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001995 * @ingroup kernel_apis
1996 * @{
1997 */
1998
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002000 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002002 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002004 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 *
2006 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002007 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002009#define k_fifo_init(fifo) \
2010 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011
2012/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002013 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002014 *
2015 * This routine causes first thread pending on @a fifo, if any, to
2016 * return from k_fifo_get() call with NULL value (as if timeout
2017 * expired).
2018 *
2019 * @note Can be called by ISRs.
2020 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002021 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002022 *
2023 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002024 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002025 */
2026#define k_fifo_cancel_wait(fifo) \
2027 k_queue_cancel_wait((struct k_queue *) fifo)
2028
2029/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002030 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002032 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002033 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2034 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * @note Can be called by ISRs.
2037 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002039 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
2041 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002042 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002044#define k_fifo_put(fifo, data) \
2045 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046
2047/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002048 * @brief Add an element to a FIFO queue.
2049 *
2050 * This routine adds a data item to @a fifo. There is an implicit
2051 * memory allocation from the calling thread's resource pool, which is
2052 * automatically freed when the item is removed.
2053 *
2054 * @note Can be called by ISRs.
2055 *
2056 * @param fifo Address of the FIFO.
2057 * @param data Address of the data item.
2058 *
2059 * @retval 0 on success
2060 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002061 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002062 */
2063#define k_fifo_alloc_put(fifo, data) \
2064 k_queue_alloc_append((struct k_queue *) fifo, data)
2065
2066/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002067 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * This routine adds a list of data items to @a fifo in one operation.
2070 * The data items must be in a singly-linked list, with the first 32 bits
2071 * each data item pointing to the next data item; the list must be
2072 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002076 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * @param head Pointer to first node in singly-linked list.
2078 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 *
2080 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002081 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002083#define k_fifo_put_list(fifo, head, tail) \
2084 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085
2086/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002087 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * This routine adds a list of data items to @a fifo in one operation.
2090 * The data items must be in a singly-linked list implemented using a
2091 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092 * and must be re-initialized via sys_slist_init().
2093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @note Can be called by ISRs.
2095 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002096 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002097 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 *
2099 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002100 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002101 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002102#define k_fifo_put_slist(fifo, list) \
2103 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104
2105/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002106 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002108 * This routine removes a data item from @a fifo in a "first in, first out"
2109 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002111 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2112 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002113 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115 * or one of the special values K_NO_WAIT and K_FOREVER.
2116 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002117 * @return Address of the data item if successful; NULL if returned
2118 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002119 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002120 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002121#define k_fifo_get(fifo, timeout) \
2122 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002123
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002125 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002126 *
2127 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002128 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002129 *
2130 * @note Can be called by ISRs.
2131 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002132 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002133 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002134 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002135 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002136 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002137 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002138#define k_fifo_is_empty(fifo) \
2139 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002140
2141/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002142 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002143 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002144 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302145 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002146 * on each iteration of processing, a head container will be peeked,
2147 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002149 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002150 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002151 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002152 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002153 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002154 */
2155#define k_fifo_peek_head(fifo) \
2156 k_queue_peek_head((struct k_queue *) fifo)
2157
2158/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002159 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002160 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002161 * Return element from the tail of FIFO queue (without removing it). A usecase
2162 * for this is if elements of the FIFO queue are themselves containers. Then
2163 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002164 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002166 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002167 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002168 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002169 */
2170#define k_fifo_peek_tail(fifo) \
2171 k_queue_peek_tail((struct k_queue *) fifo)
2172
2173/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002174 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002176 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002178 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002180 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002181 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002183#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002184 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002185 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002186 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002187
Anas Nashif166f5192018-02-25 08:02:36 -06002188/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002189
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002191 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192};
2193
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002194/**
2195 * @cond INTERNAL_HIDDEN
2196 */
2197
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002198#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002199 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002200 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002201 }
2202
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002203#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2204
Allan Stephensc98da842016-11-11 15:45:03 -05002205/**
2206 * INTERNAL_HIDDEN @endcond
2207 */
2208
2209/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002210 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002211 * @ingroup kernel_apis
2212 * @{
2213 */
2214
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002219 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002220 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221 *
2222 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002223 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002224 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002225#define k_lifo_init(lifo) \
2226 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002227
2228/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002229 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002232 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2233 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002235 * @note Can be called by ISRs.
2236 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002237 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002238 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002239 *
2240 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002241 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002242 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002243#define k_lifo_put(lifo, data) \
2244 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245
2246/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002247 * @brief Add an element to a LIFO queue.
2248 *
2249 * This routine adds a data item to @a lifo. There is an implicit
2250 * memory allocation from the calling thread's resource pool, which is
2251 * automatically freed when the item is removed.
2252 *
2253 * @note Can be called by ISRs.
2254 *
2255 * @param lifo Address of the LIFO.
2256 * @param data Address of the data item.
2257 *
2258 * @retval 0 on success
2259 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002260 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002261 */
2262#define k_lifo_alloc_put(lifo, data) \
2263 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2264
2265/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002266 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * This routine removes a data item from @a lifo in a "last in, first out"
2269 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002271 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2272 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002273 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002274 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 * or one of the special values K_NO_WAIT and K_FOREVER.
2276 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002277 * @return Address of the data item if successful; NULL if returned
2278 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002279 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002281#define k_lifo_get(lifo, timeout) \
2282 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002283
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002285 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002289 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002291 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002292 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002295 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002296 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002297 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002298
Anas Nashif166f5192018-02-25 08:02:36 -06002299/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002300
2301/**
2302 * @cond INTERNAL_HIDDEN
2303 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302304#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305
2306struct k_stack {
2307 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002308 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002310 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002311 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312};
2313
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002314#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002315 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002316 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002317 .base = stack_buffer, \
2318 .next = stack_buffer, \
2319 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002320 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002321 }
2322
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002323#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2324
Allan Stephensc98da842016-11-11 15:45:03 -05002325/**
2326 * INTERNAL_HIDDEN @endcond
2327 */
2328
2329/**
2330 * @defgroup stack_apis Stack APIs
2331 * @ingroup kernel_apis
2332 * @{
2333 */
2334
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335/**
2336 * @brief Initialize a stack.
2337 *
2338 * This routine initializes a stack object, prior to its first use.
2339 *
2340 * @param stack Address of the stack.
2341 * @param buffer Address of array used to hold stacked values.
2342 * @param num_entries Maximum number of values that can be stacked.
2343 *
2344 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002345 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002346 */
Andrew Boief3bee952018-05-02 17:44:39 -07002347void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302348 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002349
2350
2351/**
2352 * @brief Initialize a stack.
2353 *
2354 * This routine initializes a stack object, prior to its first use. Internal
2355 * buffers will be allocated from the calling thread's resource pool.
2356 * This memory will be released if k_stack_cleanup() is called, or
2357 * userspace is enabled and the stack object loses all references to it.
2358 *
2359 * @param stack Address of the stack.
2360 * @param num_entries Maximum number of values that can be stacked.
2361 *
2362 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002363 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002364 */
2365
Adithya Baglody28080d32018-10-15 11:48:51 +05302366__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2367 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002368
2369/**
2370 * @brief Release a stack's allocated buffer
2371 *
2372 * If a stack object was given a dynamically allocated buffer via
2373 * k_stack_alloc_init(), this will free it. This function does nothing
2374 * if the buffer wasn't dynamically allocated.
2375 *
2376 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002377 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002378 */
2379void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380
2381/**
2382 * @brief Push an element onto a stack.
2383 *
2384 * This routine adds a 32-bit value @a data to @a stack.
2385 *
2386 * @note Can be called by ISRs.
2387 *
2388 * @param stack Address of the stack.
2389 * @param data Value to push onto the stack.
2390 *
2391 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002392 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393 */
Andrew Boiee8734462017-09-29 16:42:07 -07002394__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395
2396/**
2397 * @brief Pop an element from a stack.
2398 *
2399 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2400 * manner and stores the value in @a data.
2401 *
2402 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2403 *
2404 * @param stack Address of the stack.
2405 * @param data Address of area to hold the value popped from the stack.
2406 * @param timeout Waiting period to obtain a value (in milliseconds),
2407 * or one of the special values K_NO_WAIT and K_FOREVER.
2408 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002409 * @retval 0 Element popped from stack.
2410 * @retval -EBUSY Returned without waiting.
2411 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002412 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002413 */
Andrew Boiee8734462017-09-29 16:42:07 -07002414__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002417 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002421 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 * @param name Name of the stack.
2424 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002425 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002426 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002427#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002428 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002429 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002430 struct k_stack name \
2431 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002432 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002433 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434
Anas Nashif166f5192018-02-25 08:02:36 -06002435/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002436
Allan Stephens6bba9b02016-11-16 14:56:54 -05002437struct k_work;
2438
Allan Stephensc98da842016-11-11 15:45:03 -05002439/**
2440 * @defgroup workqueue_apis Workqueue Thread APIs
2441 * @ingroup kernel_apis
2442 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443 */
2444
Allan Stephens6bba9b02016-11-16 14:56:54 -05002445/**
2446 * @typedef k_work_handler_t
2447 * @brief Work item handler function type.
2448 *
2449 * A work item's handler function is executed by a workqueue's thread
2450 * when the work item is processed by the workqueue.
2451 *
2452 * @param work Address of the work item.
2453 *
2454 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002455 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002456 */
2457typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458
2459/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002460 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002462
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002463struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002464 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002465 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466};
2467
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002469 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470};
2471
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002473 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474 k_work_handler_t handler;
2475 atomic_t flags[1];
2476};
2477
Allan Stephens6bba9b02016-11-16 14:56:54 -05002478struct k_delayed_work {
2479 struct k_work work;
2480 struct _timeout timeout;
2481 struct k_work_q *work_q;
2482};
2483
2484extern struct k_work_q k_sys_work_q;
2485
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002487 * INTERNAL_HIDDEN @endcond
2488 */
2489
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002490#define _K_WORK_INITIALIZER(work_handler) \
2491 { \
2492 ._reserved = NULL, \
2493 .handler = work_handler, \
2494 .flags = { 0 } \
2495 }
2496
2497#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2498
Allan Stephens6bba9b02016-11-16 14:56:54 -05002499/**
2500 * @brief Initialize a statically-defined work item.
2501 *
2502 * This macro can be used to initialize a statically-defined workqueue work
2503 * item, prior to its first use. For example,
2504 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002505 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002506 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002507 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002508 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002509 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002511#define K_WORK_DEFINE(work, work_handler) \
Andrew Boiec2e01df2018-11-12 15:16:54 -08002512 struct k_work work = _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513
2514/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002515 * @brief Initialize a work item.
2516 *
2517 * This routine initializes a workqueue work item, prior to its first use.
2518 *
2519 * @param work Address of work item.
2520 * @param handler Function to invoke each time work item is processed.
2521 *
2522 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002523 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524 */
2525static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2526{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002527 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528}
2529
2530/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002531 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002532 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002533 * This routine submits work item @a work to be processed by workqueue
2534 * @a work_q. If the work item is already pending in the workqueue's queue
2535 * as a result of an earlier submission, this routine has no effect on the
2536 * work item. If the work item has already been processed, or is currently
2537 * being processed, its work is considered complete and the work item can be
2538 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002539 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002540 * @warning
2541 * A submitted work item must not be modified until it has been processed
2542 * by the workqueue.
2543 *
2544 * @note Can be called by ISRs.
2545 *
2546 * @param work_q Address of workqueue.
2547 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002548 *
2549 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002550 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551 */
2552static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2553 struct k_work *work)
2554{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002555 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002556 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557 }
2558}
2559
2560/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002561 * @brief Submit a work item to a user mode workqueue
2562 *
David B. Kinder06d78352018-12-17 14:32:40 -08002563 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002564 * memory allocation is made from the caller's resource pool which is freed
2565 * once the worker thread consumes the k_work item. The workqueue
2566 * thread must have memory access to the k_work item being submitted. The caller
2567 * must have permission granted on the work_q parameter's queue object.
2568 *
2569 * Otherwise this works the same as k_work_submit_to_queue().
2570 *
2571 * @note Can be called by ISRs.
2572 *
2573 * @param work_q Address of workqueue.
2574 * @param work Address of work item.
2575 *
2576 * @retval -EBUSY if the work item was already in some workqueue
2577 * @retval -ENOMEM if no memory for thread resource pool allocation
2578 * @retval 0 Success
2579 * @req K-WORK-001
2580 */
2581static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2582 struct k_work *work)
2583{
2584 int ret = -EBUSY;
2585
2586 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2587 ret = k_queue_alloc_append(&work_q->queue, work);
2588
2589 /* Couldn't insert into the queue. Clear the pending bit
2590 * so the work item can be submitted again
2591 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002592 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002593 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2594 }
2595 }
2596
2597 return ret;
2598}
2599
2600/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002601 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002602 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002603 * This routine indicates if work item @a work is pending in a workqueue's
2604 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002605 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002606 * @note Can be called by ISRs.
2607 *
2608 * @param work Address of work item.
2609 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002610 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002611 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002612 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002613static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002614{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002615 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002616}
2617
2618/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002621 * This routine starts workqueue @a work_q. The workqueue spawns its work
2622 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002623 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002624 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002625 * @param stack Pointer to work queue thread's stack space, as defined by
2626 * K_THREAD_STACK_DEFINE()
2627 * @param stack_size Size of the work queue thread's stack (in bytes), which
2628 * should either be the same constant passed to
2629 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002630 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002631 *
2632 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002633 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634 */
Andrew Boie507852a2017-07-25 18:47:07 -07002635extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002636 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002637 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002640 * @brief Start a workqueue in user mode
2641 *
2642 * This works identically to k_work_q_start() except it is callable from user
2643 * mode, and the worker thread created will run in user mode.
2644 * The caller must have permissions granted on both the work_q parameter's
2645 * thread and queue objects, and the same restrictions on priority apply as
2646 * k_thread_create().
2647 *
2648 * @param work_q Address of workqueue.
2649 * @param stack Pointer to work queue thread's stack space, as defined by
2650 * K_THREAD_STACK_DEFINE()
2651 * @param stack_size Size of the work queue thread's stack (in bytes), which
2652 * should either be the same constant passed to
2653 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2654 * @param prio Priority of the work queue's thread.
2655 *
2656 * @return N/A
2657 * @req K-WORK-001
2658 */
2659extern void k_work_q_user_start(struct k_work_q *work_q,
2660 k_thread_stack_t *stack,
2661 size_t stack_size, int prio);
2662
2663/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002666 * This routine initializes a workqueue delayed work item, prior to
2667 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002669 * @param work Address of delayed work item.
2670 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002671 *
2672 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002673 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002675extern void k_delayed_work_init(struct k_delayed_work *work,
2676 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677
2678/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002681 * This routine schedules work item @a work to be processed by workqueue
2682 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002683 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002684 * Only when the countdown completes is the work item actually submitted to
2685 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002687 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002688 * counting down cancels the existing submission and restarts the
2689 * countdown using the new delay. Note that this behavior is
2690 * inherently subject to race conditions with the pre-existing
2691 * timeouts and work queue, so care must be taken to synchronize such
2692 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694 * @warning
2695 * A delayed work item must not be modified until it has been processed
2696 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002698 * @note Can be called by ISRs.
2699 *
2700 * @param work_q Address of workqueue.
2701 * @param work Address of delayed work item.
2702 * @param delay Delay before submitting the work item (in milliseconds).
2703 *
2704 * @retval 0 Work item countdown started.
2705 * @retval -EINPROGRESS Work item is already pending.
2706 * @retval -EINVAL Work item is being processed or has completed its work.
2707 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002708 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002710extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2711 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002712 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713
2714/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002717 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002718 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002719 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002721 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002723 * @param work Address of delayed work item.
2724 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002725 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002726 * @retval -EINPROGRESS Work item is already pending.
2727 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002728 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002730extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733 * @brief Submit a work item to the system workqueue.
2734 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002735 * This routine submits work item @a work to be processed by the system
2736 * workqueue. If the work item is already pending in the workqueue's queue
2737 * as a result of an earlier submission, this routine has no effect on the
2738 * work item. If the work item has already been processed, or is currently
2739 * being processed, its work is considered complete and the work item can be
2740 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002742 * @warning
2743 * Work items submitted to the system workqueue should avoid using handlers
2744 * that block or yield since this may prevent the system workqueue from
2745 * processing other work items in a timely manner.
2746 *
2747 * @note Can be called by ISRs.
2748 *
2749 * @param work Address of work item.
2750 *
2751 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002752 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753 */
2754static inline void k_work_submit(struct k_work *work)
2755{
2756 k_work_submit_to_queue(&k_sys_work_q, work);
2757}
2758
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002759/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760 * @brief Submit a delayed work item to the system workqueue.
2761 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002762 * This routine schedules work item @a work to be processed by the system
2763 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002764 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002765 * Only when the countdown completes is the work item actually submitted to
2766 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002768 * Submitting a previously submitted delayed work item that is still
2769 * counting down cancels the existing submission and restarts the countdown
2770 * using the new delay. If the work item is currently pending on the
2771 * workqueue's queue because the countdown has completed it is too late to
2772 * resubmit the item, and resubmission fails without impacting the work item.
2773 * If the work item has already been processed, or is currently being processed,
2774 * its work is considered complete and the work item can be resubmitted.
2775 *
2776 * @warning
2777 * Work items submitted to the system workqueue should avoid using handlers
2778 * that block or yield since this may prevent the system workqueue from
2779 * processing other work items in a timely manner.
2780 *
2781 * @note Can be called by ISRs.
2782 *
2783 * @param work Address of delayed work item.
2784 * @param delay Delay before submitting the work item (in milliseconds).
2785 *
2786 * @retval 0 Work item countdown started.
2787 * @retval -EINPROGRESS Work item is already pending.
2788 * @retval -EINVAL Work item is being processed or has completed its work.
2789 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002790 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791 */
2792static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002793 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002795 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796}
2797
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002799 * @brief Get time remaining before a delayed work gets scheduled.
2800 *
2801 * This routine computes the (approximate) time remaining before a
2802 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002803 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002804 *
2805 * @param work Delayed work item.
2806 *
2807 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002808 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002809 */
Kumar Galacc334c72017-04-21 10:55:34 -05002810static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002811{
Andy Ross52e444b2018-09-28 09:06:37 -07002812 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002813}
2814
Anas Nashif166f5192018-02-25 08:02:36 -06002815/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002816/**
Anas Nashifce78d162018-05-24 12:43:11 -05002817 * @defgroup mutex_apis Mutex APIs
2818 * @ingroup kernel_apis
2819 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002820 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821
Anas Nashifce78d162018-05-24 12:43:11 -05002822/**
2823 * Mutex Structure
2824 * @ingroup mutex_apis
2825 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826struct k_mutex {
2827 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002828 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002829 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002830 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002833 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834};
2835
Anas Nashifce78d162018-05-24 12:43:11 -05002836/**
2837 * @cond INTERNAL_HIDDEN
2838 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002839#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002841 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842 .owner = NULL, \
2843 .lock_count = 0, \
2844 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002845 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002846 }
2847
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002848#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2849
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850/**
Allan Stephensc98da842016-11-11 15:45:03 -05002851 * INTERNAL_HIDDEN @endcond
2852 */
2853
2854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002859 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002862 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002863 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002865 struct k_mutex name \
2866 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002867 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * Upon completion, the mutex is available and does not have an owner.
2875 *
2876 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002877 *
2878 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002879 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002881__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002882
2883/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * This routine locks @a mutex. If the mutex is locked by another thread,
2887 * the calling thread waits until the mutex becomes available or until
2888 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002890 * A thread is permitted to lock a mutex it has already locked. The operation
2891 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * @param mutex Address of the mutex.
2894 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 * or one of the special values K_NO_WAIT and K_FOREVER.
2896 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002897 * @retval 0 Mutex locked.
2898 * @retval -EBUSY Returned without waiting.
2899 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002900 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002902__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002903
2904/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * This routine unlocks @a mutex. The mutex must already be locked by the
2908 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002909 *
2910 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912 * thread.
2913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002915 *
2916 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002917 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002918 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002919__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002920
Allan Stephensc98da842016-11-11 15:45:03 -05002921/**
Anas Nashif166f5192018-02-25 08:02:36 -06002922 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002923 */
2924
2925/**
2926 * @cond INTERNAL_HIDDEN
2927 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928
2929struct k_sem {
2930 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05302931 u32_t count;
2932 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002933 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002935 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002936};
2937
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002938#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002939 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002940 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002941 .count = initial_count, \
2942 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002943 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002944 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002945 }
2946
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002947#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2948
Allan Stephensc98da842016-11-11 15:45:03 -05002949/**
2950 * INTERNAL_HIDDEN @endcond
2951 */
2952
2953/**
2954 * @defgroup semaphore_apis Semaphore APIs
2955 * @ingroup kernel_apis
2956 * @{
2957 */
2958
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002962 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002964 * @param sem Address of the semaphore.
2965 * @param initial_count Initial semaphore count.
2966 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002967 *
2968 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002969 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002970 */
Andrew Boie99280232017-09-29 14:17:47 -07002971__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2972 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002973
2974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2980 *
2981 * @param sem Address of the semaphore.
2982 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002983 * or one of the special values K_NO_WAIT and K_FOREVER.
2984 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002985 * @note When porting code from the nanokernel legacy API to the new API, be
2986 * careful with the return value of this function. The return value is the
2987 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2988 * non-zero means failure, while the nano_sem_take family returns 1 for success
2989 * and 0 for failure.
2990 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002991 * @retval 0 Semaphore taken.
2992 * @retval -EBUSY Returned without waiting.
2993 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002994 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002995 */
Andrew Boie99280232017-09-29 14:17:47 -07002996__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002997
2998/**
2999 * @brief Give a semaphore.
3000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * This routine gives @a sem, unless the semaphore is already at its maximum
3002 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003007 *
3008 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003009 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003010 */
Andrew Boie99280232017-09-29 14:17:47 -07003011__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003013/**
3014 * @brief Reset a semaphore's count to zero.
3015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003019 *
3020 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003021 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003022 */
Andrew Boie990bf162017-10-03 12:36:49 -07003023__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003024
Anas Nashif954d5502018-02-25 08:37:28 -06003025/**
3026 * @internal
3027 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003028static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029{
3030 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003031}
3032
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003033/**
3034 * @brief Get a semaphore's count.
3035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003040 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003041 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003042 */
Andrew Boie990bf162017-10-03 12:36:49 -07003043__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003044
Anas Nashif954d5502018-02-25 08:37:28 -06003045/**
3046 * @internal
3047 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003048static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049{
3050 return sem->count;
3051}
3052
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003053/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003057 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003058 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * @param name Name of the semaphore.
3061 * @param initial_count Initial semaphore count.
3062 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003063 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003064 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003066 struct k_sem name \
3067 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003068 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303069 BUILD_ASSERT(((count_limit) != 0) && \
3070 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071
Anas Nashif166f5192018-02-25 08:02:36 -06003072/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003073
3074/**
3075 * @defgroup alert_apis Alert APIs
3076 * @ingroup kernel_apis
3077 * @{
3078 */
3079
Allan Stephens5eceb852016-11-16 10:16:30 -05003080/**
3081 * @typedef k_alert_handler_t
3082 * @brief Alert handler function type.
3083 *
3084 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003085 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003086 * and is only invoked if the alert has been initialized with one.
3087 *
3088 * @param alert Address of the alert.
3089 *
3090 * @return 0 if alert has been consumed; non-zero if alert should pend.
3091 */
3092typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003093
Anas Nashif166f5192018-02-25 08:02:36 -06003094/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003095
3096/**
3097 * @cond INTERNAL_HIDDEN
3098 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003100#define K_ALERT_DEFAULT NULL
Adithya Baglodyd5915882018-10-05 14:46:04 +05303101#define K_ALERT_IGNORE ((k_alert_handler_t)0xFFFFFFFF)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003103struct k_alert {
3104 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105 atomic_t send_count;
3106 struct k_work work_item;
3107 struct k_sem sem;
3108
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003109 _OBJECT_TRACING_NEXT_PTR(k_alert)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110};
3111
Anas Nashif954d5502018-02-25 08:37:28 -06003112/**
3113 * @internal
3114 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003115extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003117#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003119 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003120 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003121 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3122 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003123 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003124 }
3125
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003126#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3127
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003128/**
Allan Stephensc98da842016-11-11 15:45:03 -05003129 * INTERNAL_HIDDEN @endcond
3130 */
3131
3132/**
3133 * @addtogroup alert_apis
3134 * @{
3135 */
3136
3137/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003138 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003141 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003142 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003144 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003146 * @param name Name of the alert.
3147 * @param alert_handler Action to take when alert is sent. Specify either
3148 * the address of a function to be invoked by the system workqueue
3149 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3150 * K_ALERT_DEFAULT (which causes the alert to pend).
3151 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003152 *
3153 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003154 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003155#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003156 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003157 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003158 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003159 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003160
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003161/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003166 * @param alert Address of the alert.
3167 * @param handler Action to take when alert is sent. Specify either the address
3168 * of a function to be invoked by the system workqueue thread,
3169 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3170 * K_ALERT_DEFAULT (which causes the alert to pend).
3171 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003172 *
3173 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003174 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003176extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3177 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003178
3179/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003180 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003184 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3185 *
3186 * @param alert Address of the alert.
3187 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003188 * or one of the special values K_NO_WAIT and K_FOREVER.
3189 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003190 * @retval 0 Alert received.
3191 * @retval -EBUSY Returned without waiting.
3192 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003193 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003194 */
Andrew Boie310e9872017-09-29 04:41:15 -07003195__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003196
3197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * This routine signals @a alert. The action specified for @a alert will
3201 * be taken, which may trigger the execution of an alert handler function
3202 * and/or cause the alert to pend (assuming the alert has not reached its
3203 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @note Can be called by ISRs.
3206 *
3207 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003208 *
3209 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003210 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003211 */
Andrew Boie310e9872017-09-29 04:41:15 -07003212__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213
3214/**
Anas Nashif166f5192018-02-25 08:02:36 -06003215 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003216 */
3217
Allan Stephensc98da842016-11-11 15:45:03 -05003218/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003219 * @defgroup msgq_apis Message Queue APIs
3220 * @ingroup kernel_apis
3221 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003224/**
3225 * @brief Message Queue Structure
3226 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227struct k_msgq {
3228 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003229 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003230 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003231 char *buffer_start;
3232 char *buffer_end;
3233 char *read_ptr;
3234 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003235 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003237 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003238 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003240/**
3241 * @cond INTERNAL_HIDDEN
3242 */
3243
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003245#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003247 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003248 .max_msgs = q_max_msgs, \
3249 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003250 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003251 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003252 .read_ptr = q_buffer, \
3253 .write_ptr = q_buffer, \
3254 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003255 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003257#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003258/**
3259 * INTERNAL_HIDDEN @endcond
3260 */
3261
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003262
Andrew Boie0fe789f2018-04-12 18:35:56 -07003263#define K_MSGQ_FLAG_ALLOC BIT(0)
3264
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003265/**
3266 * @brief Message Queue Attributes
3267 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303268struct k_msgq_attrs {
3269 size_t msg_size;
3270 u32_t max_msgs;
3271 u32_t used_msgs;
3272};
3273
Allan Stephensc98da842016-11-11 15:45:03 -05003274
3275/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3279 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003280 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3281 * message is similarly aligned to this boundary, @a q_msg_size must also be
3282 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * The message queue can be accessed outside the module where it is defined
3285 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003286 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003287 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003289 * @param q_name Name of the message queue.
3290 * @param q_msg_size Message size (in bytes).
3291 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003292 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003293 *
3294 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003295 */
3296#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003297 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003298 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003299 struct k_msgq q_name \
3300 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003301 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003302 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303
Peter Mitsisd7a37502016-10-13 11:37:40 -04003304/**
3305 * @brief Initialize a message queue.
3306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * This routine initializes a message queue object, prior to its first use.
3308 *
Allan Stephensda827222016-11-09 14:23:58 -06003309 * The message queue's ring buffer must contain space for @a max_msgs messages,
3310 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3311 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3312 * that each message is similarly aligned to this boundary, @a q_msg_size
3313 * must also be a multiple of N.
3314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @param q Address of the message queue.
3316 * @param buffer Pointer to ring buffer that holds queued messages.
3317 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003318 * @param max_msgs Maximum number of messages that can be queued.
3319 *
3320 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003321 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003322 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003323void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3324 u32_t max_msgs);
3325
3326/**
3327 * @brief Initialize a message queue.
3328 *
3329 * This routine initializes a message queue object, prior to its first use,
3330 * allocating its internal ring buffer from the calling thread's resource
3331 * pool.
3332 *
3333 * Memory allocated for the ring buffer can be released by calling
3334 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3335 * all of its references.
3336 *
3337 * @param q Address of the message queue.
3338 * @param msg_size Message size (in bytes).
3339 * @param max_msgs Maximum number of messages that can be queued.
3340 *
3341 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3342 * thread's resource pool, or -EINVAL if the size parameters cause
3343 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003344 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003345 */
3346__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3347 u32_t max_msgs);
3348
3349
3350void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003351
3352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003356 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003357 * @note Can be called by ISRs.
3358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * @param q Address of the message queue.
3360 * @param data Pointer to the message.
3361 * @param timeout Waiting period to add the message (in milliseconds),
3362 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003363 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003364 * @retval 0 Message sent.
3365 * @retval -ENOMSG Returned without waiting or queue purged.
3366 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003367 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003368 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003369__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003370
3371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * This routine receives a message from message queue @a q in a "first in,
3375 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003376 *
Allan Stephensc98da842016-11-11 15:45:03 -05003377 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @param q Address of the message queue.
3380 * @param data Address of area to hold the received message.
3381 * @param timeout Waiting period to receive the message (in milliseconds),
3382 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003383 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003384 * @retval 0 Message received.
3385 * @retval -ENOMSG Returned without waiting.
3386 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003387 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003388 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003389__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003390
3391/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003392 * @brief Peek/read a message from a message queue.
3393 *
3394 * This routine reads a message from message queue @a q in a "first in,
3395 * first out" manner and leaves the message in the queue.
3396 *
3397 * @note Can be called by ISRs.
3398 *
3399 * @param q Address of the message queue.
3400 * @param data Address of area to hold the message read from the queue.
3401 *
3402 * @retval 0 Message read.
3403 * @retval -ENOMSG Returned when the queue has no message.
3404 * @req K-MSGQ-002
3405 */
3406__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3407
3408/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003411 * This routine discards all unreceived messages in a message queue's ring
3412 * buffer. Any threads that are blocked waiting to send a message to the
3413 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003416 *
3417 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003418 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003419 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003420__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421
Peter Mitsis67be2492016-10-07 11:44:34 -04003422/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003425 * This routine returns the number of unused entries in a message queue's
3426 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * @param q Address of the message queue.
3429 *
3430 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003431 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003432 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003433__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3434
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303435/**
3436 * @brief Get basic attributes of a message queue.
3437 *
3438 * This routine fetches basic attributes of message queue into attr argument.
3439 *
3440 * @param q Address of the message queue.
3441 * @param attrs pointer to message queue attribute structure.
3442 *
3443 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003444 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303445 */
3446__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3447
3448
Andrew Boie82edb6e2017-10-02 10:53:06 -07003449static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003450{
3451 return q->max_msgs - q->used_msgs;
3452}
3453
Peter Mitsisd7a37502016-10-13 11:37:40 -04003454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * @param q Address of the message queue.
3460 *
3461 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003462 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003463 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003464__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3465
3466static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467{
3468 return q->used_msgs;
3469}
3470
Anas Nashif166f5192018-02-25 08:02:36 -06003471/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003472
3473/**
3474 * @defgroup mem_pool_apis Memory Pool APIs
3475 * @ingroup kernel_apis
3476 * @{
3477 */
3478
Andy Ross73cb9582017-05-09 10:42:39 -07003479/* Note on sizing: the use of a 20 bit field for block means that,
3480 * assuming a reasonable minimum block size of 16 bytes, we're limited
3481 * to 16M of memory managed by a single pool. Long term it would be
3482 * good to move to a variable bit size based on configuration.
3483 */
3484struct k_mem_block_id {
3485 u32_t pool : 8;
3486 u32_t level : 4;
3487 u32_t block : 20;
3488};
3489
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003490struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003492 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493};
3494
Anas Nashif166f5192018-02-25 08:02:36 -06003495/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003496
3497/**
3498 * @defgroup mailbox_apis Mailbox APIs
3499 * @ingroup kernel_apis
3500 * @{
3501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502
3503struct k_mbox_msg {
3504 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003505 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003507 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003508 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003509 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510 /** sender's message data buffer */
3511 void *tx_data;
3512 /** internal use only - needed for legacy API support */
3513 void *_rx_data;
3514 /** message data block descriptor */
3515 struct k_mem_block tx_block;
3516 /** source thread id */
3517 k_tid_t rx_source_thread;
3518 /** target thread id */
3519 k_tid_t tx_target_thread;
3520 /** internal use only - thread waiting on send (may be a dummy) */
3521 k_tid_t _syncing_thread;
3522#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3523 /** internal use only - semaphore used during asynchronous send */
3524 struct k_sem *_async_sem;
3525#endif
3526};
3527
3528struct k_mbox {
3529 _wait_q_t tx_msg_queue;
3530 _wait_q_t rx_msg_queue;
3531
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003532 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003533};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003534/**
3535 * @cond INTERNAL_HIDDEN
3536 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003537
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003538#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003539 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003540 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3541 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003542 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003543 }
3544
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003545#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3546
Peter Mitsis12092702016-10-14 12:57:23 -04003547/**
Allan Stephensc98da842016-11-11 15:45:03 -05003548 * INTERNAL_HIDDEN @endcond
3549 */
3550
3551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003555 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003556 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003559 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003560 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003562 struct k_mbox name \
3563 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003564 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003565
Peter Mitsis12092702016-10-14 12:57:23 -04003566/**
3567 * @brief Initialize a mailbox.
3568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * This routine initializes a mailbox object, prior to its first use.
3570 *
3571 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003572 *
3573 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003574 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003575 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003576extern void k_mbox_init(struct k_mbox *mbox);
3577
Peter Mitsis12092702016-10-14 12:57:23 -04003578/**
3579 * @brief Send a mailbox message in a synchronous manner.
3580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003581 * This routine sends a message to @a mbox and waits for a receiver to both
3582 * receive and process it. The message data may be in a buffer, in a memory
3583 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @param mbox Address of the mailbox.
3586 * @param tx_msg Address of the transmit message descriptor.
3587 * @param timeout Waiting period for the message to be received (in
3588 * milliseconds), or one of the special values K_NO_WAIT
3589 * and K_FOREVER. Once the message has been received,
3590 * this routine waits as long as necessary for the message
3591 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003592 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003593 * @retval 0 Message sent.
3594 * @retval -ENOMSG Returned without waiting.
3595 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003596 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003597 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003598extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003599 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003600
Peter Mitsis12092702016-10-14 12:57:23 -04003601/**
3602 * @brief Send a mailbox message in an asynchronous manner.
3603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * This routine sends a message to @a mbox without waiting for a receiver
3605 * to process it. The message data may be in a buffer, in a memory pool block,
3606 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3607 * will be given when the message has been both received and completely
3608 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @param mbox Address of the mailbox.
3611 * @param tx_msg Address of the transmit message descriptor.
3612 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003613 *
3614 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003615 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003616 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003617extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003618 struct k_sem *sem);
3619
Peter Mitsis12092702016-10-14 12:57:23 -04003620/**
3621 * @brief Receive a mailbox message.
3622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * This routine receives a message from @a mbox, then optionally retrieves
3624 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * @param mbox Address of the mailbox.
3627 * @param rx_msg Address of the receive message descriptor.
3628 * @param buffer Address of the buffer to receive data, or NULL to defer data
3629 * retrieval and message disposal until later.
3630 * @param timeout Waiting period for a message to be received (in
3631 * milliseconds), or one of the special values K_NO_WAIT
3632 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003633 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003634 * @retval 0 Message received.
3635 * @retval -ENOMSG Returned without waiting.
3636 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003637 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003638 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003639extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003640 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003641
3642/**
3643 * @brief Retrieve mailbox message data into a buffer.
3644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003645 * This routine completes the processing of a received message by retrieving
3646 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003647 *
3648 * Alternatively, this routine can be used to dispose of a received message
3649 * without retrieving its data.
3650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @param rx_msg Address of the receive message descriptor.
3652 * @param buffer Address of the buffer to receive data, or NULL to discard
3653 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003654 *
3655 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003656 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003657 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003658extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003659
3660/**
3661 * @brief Retrieve mailbox message data into a memory pool block.
3662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003663 * This routine completes the processing of a received message by retrieving
3664 * its data into a memory pool block, then disposing of the message.
3665 * The memory pool block that results from successful retrieval must be
3666 * returned to the pool once the data has been processed, even in cases
3667 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003668 *
3669 * Alternatively, this routine can be used to dispose of a received message
3670 * without retrieving its data. In this case there is no need to return a
3671 * memory pool block to the pool.
3672 *
3673 * This routine allocates a new memory pool block for the data only if the
3674 * data is not already in one. If a new block cannot be allocated, the routine
3675 * returns a failure code and the received message is left unchanged. This
3676 * permits the caller to reattempt data retrieval at a later time or to dispose
3677 * of the received message without retrieving its data.
3678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003679 * @param rx_msg Address of a receive message descriptor.
3680 * @param pool Address of memory pool, or NULL to discard data.
3681 * @param block Address of the area to hold memory pool block info.
3682 * @param timeout Waiting period to wait for a memory pool block (in
3683 * milliseconds), or one of the special values K_NO_WAIT
3684 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003685 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003686 * @retval 0 Data retrieved.
3687 * @retval -ENOMEM Returned without waiting.
3688 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003689 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003690 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003691extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003692 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003693 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003694
Anas Nashif166f5192018-02-25 08:02:36 -06003695/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003696
3697/**
Anas Nashifce78d162018-05-24 12:43:11 -05003698 * @defgroup pipe_apis Pipe APIs
3699 * @ingroup kernel_apis
3700 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003701 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003702
Anas Nashifce78d162018-05-24 12:43:11 -05003703/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003704struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003705 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3706 size_t size; /**< Buffer size */
3707 size_t bytes_used; /**< # bytes used in buffer */
3708 size_t read_index; /**< Where in buffer to read from */
3709 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710
3711 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003712 _wait_q_t readers; /**< Reader wait queue */
3713 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003714 } wait_q;
3715
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003716 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003717 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718};
3719
Anas Nashifce78d162018-05-24 12:43:11 -05003720/**
3721 * @cond INTERNAL_HIDDEN
3722 */
3723#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3724
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003725#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003726 { \
3727 .buffer = pipe_buffer, \
3728 .size = pipe_buffer_size, \
3729 .bytes_used = 0, \
3730 .read_index = 0, \
3731 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003732 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3733 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003734 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735 }
3736
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003737#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3738
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003739/**
Allan Stephensc98da842016-11-11 15:45:03 -05003740 * INTERNAL_HIDDEN @endcond
3741 */
3742
3743/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003744 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003746 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003747 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003748 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003750 * @param name Name of the pipe.
3751 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3752 * or zero if no ring buffer is used.
3753 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003754 *
3755 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003756 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003757#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3758 static unsigned char __kernel_noinit __aligned(pipe_align) \
3759 _k_pipe_buf_##name[pipe_buffer_size]; \
3760 struct k_pipe name \
3761 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003762 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003763
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003767 * This routine initializes a pipe object, prior to its first use.
3768 *
3769 * @param pipe Address of the pipe.
3770 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3771 * is used.
3772 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3773 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003774 *
3775 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003776 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003777 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003778void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3779
3780/**
3781 * @brief Release a pipe's allocated buffer
3782 *
3783 * If a pipe object was given a dynamically allocated buffer via
3784 * k_pipe_alloc_init(), this will free it. This function does nothing
3785 * if the buffer wasn't dynamically allocated.
3786 *
3787 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003788 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003789 */
3790void k_pipe_cleanup(struct k_pipe *pipe);
3791
3792/**
3793 * @brief Initialize a pipe and allocate a buffer for it
3794 *
3795 * Storage for the buffer region will be allocated from the calling thread's
3796 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3797 * or userspace is enabled and the pipe object loses all references to it.
3798 *
3799 * This function should only be called on uninitialized pipe objects.
3800 *
3801 * @param pipe Address of the pipe.
3802 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3803 * buffer is used.
3804 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003805 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003806 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003807 */
3808__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809
3810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003811 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 * @param pipe Address of the pipe.
3816 * @param data Address of data to write.
3817 * @param bytes_to_write Size of data (in bytes).
3818 * @param bytes_written Address of area to hold the number of bytes written.
3819 * @param min_xfer Minimum number of bytes to write.
3820 * @param timeout Waiting period to wait for the data to be written (in
3821 * milliseconds), or one of the special values K_NO_WAIT
3822 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003823 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003824 * @retval 0 At least @a min_xfer bytes of data were written.
3825 * @retval -EIO Returned without waiting; zero data bytes were written.
3826 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003827 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003828 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003830__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3831 size_t bytes_to_write, size_t *bytes_written,
3832 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833
3834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003835 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003839 * @param pipe Address of the pipe.
3840 * @param data Address to place the data read from pipe.
3841 * @param bytes_to_read Maximum number of data bytes to read.
3842 * @param bytes_read Address of area to hold the number of bytes read.
3843 * @param min_xfer Minimum number of data bytes to read.
3844 * @param timeout Waiting period to wait for the data to be read (in
3845 * milliseconds), or one of the special values K_NO_WAIT
3846 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003848 * @retval 0 At least @a min_xfer bytes of data were read.
3849 * @retval -EIO Returned without waiting; zero data bytes were read.
3850 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003852 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003853 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003854__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3855 size_t bytes_to_read, size_t *bytes_read,
3856 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857
3858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003859 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003861 * This routine writes the data contained in a memory block to @a pipe.
3862 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003863 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003865 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866 * @param block Memory block containing data to send
3867 * @param size Number of data bytes in memory block to send
3868 * @param sem Semaphore to signal upon completion (else NULL)
3869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003870 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003871 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003872 */
3873extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3874 size_t size, struct k_sem *sem);
3875
Anas Nashif166f5192018-02-25 08:02:36 -06003876/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003877
Allan Stephensc98da842016-11-11 15:45:03 -05003878/**
3879 * @cond INTERNAL_HIDDEN
3880 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003882struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003883 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003884 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003885 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003886 char *buffer;
3887 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003888 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003889
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003890 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003891};
3892
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003893#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003894 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003895 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003896 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003897 .num_blocks = slab_num_blocks, \
3898 .block_size = slab_block_size, \
3899 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003900 .free_list = NULL, \
3901 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003902 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003903 }
3904
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003905#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3906
3907
Peter Mitsis578f9112016-10-07 13:50:31 -04003908/**
Allan Stephensc98da842016-11-11 15:45:03 -05003909 * INTERNAL_HIDDEN @endcond
3910 */
3911
3912/**
3913 * @defgroup mem_slab_apis Memory Slab APIs
3914 * @ingroup kernel_apis
3915 * @{
3916 */
3917
3918/**
Allan Stephensda827222016-11-09 14:23:58 -06003919 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003920 *
Allan Stephensda827222016-11-09 14:23:58 -06003921 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003923 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3924 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003925 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003926 *
Allan Stephensda827222016-11-09 14:23:58 -06003927 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003928 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003929 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003930 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * @param name Name of the memory slab.
3933 * @param slab_block_size Size of each memory block (in bytes).
3934 * @param slab_num_blocks Number memory blocks.
3935 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003936 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003937 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003938#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3939 char __noinit __aligned(slab_align) \
3940 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3941 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003942 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003943 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003944 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003945
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003946/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003947 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003950 *
Allan Stephensda827222016-11-09 14:23:58 -06003951 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3952 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3953 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3954 * To ensure that each memory block is similarly aligned to this boundary,
3955 * @a slab_block_size must also be a multiple of N.
3956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003957 * @param slab Address of the memory slab.
3958 * @param buffer Pointer to buffer used for the memory blocks.
3959 * @param block_size Size of each memory block (in bytes).
3960 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003961 *
3962 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003963 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003964 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003965extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003966 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003967
3968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003971 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003973 * @param slab Address of the memory slab.
3974 * @param mem Pointer to block address area.
3975 * @param timeout Maximum time to wait for operation to complete
3976 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3977 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003978 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003979 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003980 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003981 * @retval -ENOMEM Returned without waiting.
3982 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003983 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003984 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003985extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003986 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003987
3988/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003989 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003991 * This routine releases a previously allocated memory block back to its
3992 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003994 * @param slab Address of the memory slab.
3995 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003996 *
3997 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003998 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003999 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004000extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004001
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004003 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004005 * This routine gets the number of memory blocks that are currently
4006 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004008 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004010 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004011 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004012 */
Kumar Galacc334c72017-04-21 10:55:34 -05004013static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004014{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004015 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004016}
4017
Peter Mitsisc001aa82016-10-13 13:53:37 -04004018/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004019 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * This routine gets the number of memory blocks that are currently
4022 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004026 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004027 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004028 */
Kumar Galacc334c72017-04-21 10:55:34 -05004029static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004030{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004031 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004032}
4033
Anas Nashif166f5192018-02-25 08:02:36 -06004034/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004035
4036/**
4037 * @cond INTERNAL_HIDDEN
4038 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004039
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004040struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004041 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004042 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004043};
4044
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004045/**
Allan Stephensc98da842016-11-11 15:45:03 -05004046 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004047 */
4048
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004049/**
Allan Stephensc98da842016-11-11 15:45:03 -05004050 * @addtogroup mem_pool_apis
4051 * @{
4052 */
4053
4054/**
4055 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004057 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4058 * long. The memory pool allows blocks to be repeatedly partitioned into
4059 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004060 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004061 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004062 * If the pool is to be accessed outside the module where it is defined, it
4063 * can be declared via
4064 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004065 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004067 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004068 * @param minsz Size of the smallest blocks in the pool (in bytes).
4069 * @param maxsz Size of the largest blocks in the pool (in bytes).
4070 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004071 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004072 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004073 */
Andy Ross73cb9582017-05-09 10:42:39 -07004074#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4075 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4076 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004077 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004078 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004079 .base = { \
4080 .buf = _mpool_buf_##name, \
4081 .max_sz = maxsz, \
4082 .n_max = nmax, \
4083 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4084 .levels = _mpool_lvls_##name, \
4085 .flags = SYS_MEM_POOL_KERNEL \
4086 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004087 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004088
Peter Mitsis937042c2016-10-13 13:18:26 -04004089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004090 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004092 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004094 * @param pool Address of the memory pool.
4095 * @param block Pointer to block descriptor for the allocated memory.
4096 * @param size Amount of memory to allocate (in bytes).
4097 * @param timeout Maximum time to wait for operation to complete
4098 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4099 * or K_FOREVER to wait as long as necessary.
4100 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004101 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004102 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004103 * @retval -ENOMEM Returned without waiting.
4104 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004105 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004106 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004107extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004108 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004109
4110/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004111 * @brief Allocate memory from a memory pool with malloc() semantics
4112 *
4113 * Such memory must be released using k_free().
4114 *
4115 * @param pool Address of the memory pool.
4116 * @param size Amount of memory to allocate (in bytes).
4117 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004118 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004119 */
4120extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4121
4122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004123 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004125 * This routine releases a previously allocated memory block back to its
4126 * memory pool.
4127 *
4128 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004129 *
4130 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004131 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004132 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004133extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004134
4135/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004136 * @brief Free memory allocated from a memory pool.
4137 *
4138 * This routine releases a previously allocated memory block back to its
4139 * memory pool
4140 *
4141 * @param id Memory block identifier.
4142 *
4143 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004144 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004145 */
4146extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4147
4148/**
Anas Nashif166f5192018-02-25 08:02:36 -06004149 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004150 */
4151
4152/**
4153 * @defgroup heap_apis Heap Memory Pool APIs
4154 * @ingroup kernel_apis
4155 * @{
4156 */
4157
4158/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004159 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004161 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004162 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004164 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004166 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004167 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004168 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004169extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004170
4171/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004172 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004173 *
4174 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004175 * returned must have been allocated from the heap memory pool or
4176 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004177 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004178 * If @a ptr is NULL, no operation is performed.
4179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004180 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004181 *
4182 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004183 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004184 */
4185extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004186
Allan Stephensc98da842016-11-11 15:45:03 -05004187/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004188 * @brief Allocate memory from heap, array style
4189 *
4190 * This routine provides traditional calloc() semantics. Memory is
4191 * allocated from the heap memory pool and zeroed.
4192 *
4193 * @param nmemb Number of elements in the requested array
4194 * @param size Size of each array element (in bytes).
4195 *
4196 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004197 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004198 */
4199extern void *k_calloc(size_t nmemb, size_t size);
4200
Anas Nashif166f5192018-02-25 08:02:36 -06004201/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004202
Benjamin Walshacc68c12017-01-29 18:57:45 -05004203/* polling API - PRIVATE */
4204
Benjamin Walshb0179862017-02-02 16:39:57 -05004205#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004206#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004207#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004208#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004209#endif
4210
Benjamin Walshacc68c12017-01-29 18:57:45 -05004211/* private - implementation data created as needed, per-type */
4212struct _poller {
4213 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004214 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004215};
4216
4217/* private - types bit positions */
4218enum _poll_types_bits {
4219 /* can be used to ignore an event */
4220 _POLL_TYPE_IGNORE,
4221
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004222 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004223 _POLL_TYPE_SIGNAL,
4224
4225 /* semaphore availability */
4226 _POLL_TYPE_SEM_AVAILABLE,
4227
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004228 /* queue/fifo/lifo data availability */
4229 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004230
4231 _POLL_NUM_TYPES
4232};
4233
4234#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4235
4236/* private - states bit positions */
4237enum _poll_states_bits {
4238 /* default state when creating event */
4239 _POLL_STATE_NOT_READY,
4240
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004241 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004242 _POLL_STATE_SIGNALED,
4243
4244 /* semaphore is available */
4245 _POLL_STATE_SEM_AVAILABLE,
4246
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004247 /* data is available to read on queue/fifo/lifo */
4248 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004250 /* queue/fifo/lifo wait was cancelled */
4251 _POLL_STATE_CANCELLED,
4252
Benjamin Walshacc68c12017-01-29 18:57:45 -05004253 _POLL_NUM_STATES
4254};
4255
4256#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4257
4258#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004259 (32 - (0 \
4260 + 8 /* tag */ \
4261 + _POLL_NUM_TYPES \
4262 + _POLL_NUM_STATES \
4263 + 1 /* modes */ \
4264 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004265
Benjamin Walshacc68c12017-01-29 18:57:45 -05004266/* end of polling API - PRIVATE */
4267
4268
4269/**
4270 * @defgroup poll_apis Async polling APIs
4271 * @ingroup kernel_apis
4272 * @{
4273 */
4274
4275/* Public polling API */
4276
4277/* public - values for k_poll_event.type bitfield */
4278#define K_POLL_TYPE_IGNORE 0
4279#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4280#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004281#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4282#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004283
4284/* public - polling modes */
4285enum k_poll_modes {
4286 /* polling thread does not take ownership of objects when available */
4287 K_POLL_MODE_NOTIFY_ONLY = 0,
4288
4289 K_POLL_NUM_MODES
4290};
4291
4292/* public - values for k_poll_event.state bitfield */
4293#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004294#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4295#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004296#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4297#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004298#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004299
4300/* public - poll signal object */
4301struct k_poll_signal {
4302 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004303 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004304
4305 /*
4306 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4307 * user resets it to 0.
4308 */
4309 unsigned int signaled;
4310
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004311 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312 int result;
4313};
4314
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004315#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004316 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004317 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004318 .signaled = 0, \
4319 .result = 0, \
4320 }
4321
4322struct k_poll_event {
4323 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004324 sys_dnode_t _node;
4325
4326 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004327 struct _poller *poller;
4328
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004329 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004330 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004331
Benjamin Walshacc68c12017-01-29 18:57:45 -05004332 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004333 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004334
4335 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004336 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004337
4338 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004339 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004340
4341 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004342 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004343
4344 /* per-type data */
4345 union {
4346 void *obj;
4347 struct k_poll_signal *signal;
4348 struct k_sem *sem;
4349 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004350 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004351 };
4352};
4353
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004354#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004355 { \
4356 .poller = NULL, \
4357 .type = event_type, \
4358 .state = K_POLL_STATE_NOT_READY, \
4359 .mode = event_mode, \
4360 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004361 { .obj = event_obj }, \
4362 }
4363
4364#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4365 event_tag) \
4366 { \
4367 .type = event_type, \
4368 .tag = event_tag, \
4369 .state = K_POLL_STATE_NOT_READY, \
4370 .mode = event_mode, \
4371 .unused = 0, \
4372 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004373 }
4374
4375/**
4376 * @brief Initialize one struct k_poll_event instance
4377 *
4378 * After this routine is called on a poll event, the event it ready to be
4379 * placed in an event array to be passed to k_poll().
4380 *
4381 * @param event The event to initialize.
4382 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4383 * values. Only values that apply to the same object being polled
4384 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4385 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004386 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004387 * @param obj Kernel object or poll signal.
4388 *
4389 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004390 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004391 */
4392
Kumar Galacc334c72017-04-21 10:55:34 -05004393extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004394 int mode, void *obj);
4395
4396/**
4397 * @brief Wait for one or many of multiple poll events to occur
4398 *
4399 * This routine allows a thread to wait concurrently for one or many of
4400 * multiple poll events to have occurred. Such events can be a kernel object
4401 * being available, like a semaphore, or a poll signal event.
4402 *
4403 * When an event notifies that a kernel object is available, the kernel object
4404 * is not "given" to the thread calling k_poll(): it merely signals the fact
4405 * that the object was available when the k_poll() call was in effect. Also,
4406 * all threads trying to acquire an object the regular way, i.e. by pending on
4407 * the object, have precedence over the thread polling on the object. This
4408 * means that the polling thread will never get the poll event on an object
4409 * until the object becomes available and its pend queue is empty. For this
4410 * reason, the k_poll() call is more effective when the objects being polled
4411 * only have one thread, the polling thread, trying to acquire them.
4412 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004413 * When k_poll() returns 0, the caller should loop on all the events that were
4414 * passed to k_poll() and check the state field for the values that were
4415 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004416 *
4417 * Before being reused for another call to k_poll(), the user has to reset the
4418 * state field to K_POLL_STATE_NOT_READY.
4419 *
Andrew Boie3772f772018-05-07 16:52:57 -07004420 * When called from user mode, a temporary memory allocation is required from
4421 * the caller's resource pool.
4422 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004423 * @param events An array of pointers to events to be polled for.
4424 * @param num_events The number of events in the array.
4425 * @param timeout Waiting period for an event to be ready (in milliseconds),
4426 * or one of the special values K_NO_WAIT and K_FOREVER.
4427 *
4428 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004429 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004430 * @retval -EINTR Polling has been interrupted, e.g. with
4431 * k_queue_cancel_wait(). All output events are still set and valid,
4432 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4433 * words, -EINTR status means that at least one of output events is
4434 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004435 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4436 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004437 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004438 */
4439
Andrew Boie3772f772018-05-07 16:52:57 -07004440__syscall int k_poll(struct k_poll_event *events, int num_events,
4441 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004442
4443/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004444 * @brief Initialize a poll signal object.
4445 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004446 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004447 *
4448 * @param signal A poll signal.
4449 *
4450 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004451 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004452 */
4453
Andrew Boie3772f772018-05-07 16:52:57 -07004454__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4455
4456/*
4457 * @brief Reset a poll signal object's state to unsignaled.
4458 *
4459 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004460 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004461 */
4462__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4463
4464static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4465{
4466 signal->signaled = 0;
4467}
4468
4469/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004470 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004471 *
4472 * @param signal A poll signal object
4473 * @param signaled An integer buffer which will be written nonzero if the
4474 * object was signaled
4475 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004476 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004477 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004478 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004479 */
4480__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4481 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004482
4483/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004484 * @brief Signal a poll signal object.
4485 *
4486 * This routine makes ready a poll signal, which is basically a poll event of
4487 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4488 * made ready to run. A @a result value can be specified.
4489 *
4490 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004491 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004492 * k_poll_signal_reset(). It thus has to be reset by the user before being
4493 * passed again to k_poll() or k_poll() will consider it being signaled, and
4494 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004495 *
4496 * @param signal A poll signal.
4497 * @param result The value to store in the result field of the signal.
4498 *
4499 * @retval 0 The signal was delivered successfully.
4500 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004501 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004502 */
4503
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004504__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004505
Anas Nashif954d5502018-02-25 08:37:28 -06004506/**
4507 * @internal
4508 */
Andy Ross8606fab2018-03-26 10:54:40 -07004509extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004510
Anas Nashif166f5192018-02-25 08:02:36 -06004511/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004512
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004513/**
4514 * @brief Make the CPU idle.
4515 *
4516 * This function makes the CPU idle until an event wakes it up.
4517 *
4518 * In a regular system, the idle thread should be the only thread responsible
4519 * for making the CPU idle and triggering any type of power management.
4520 * However, in some more constrained systems, such as a single-threaded system,
4521 * the only thread would be responsible for this if needed.
4522 *
4523 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004524 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004525 */
4526extern void k_cpu_idle(void);
4527
4528/**
4529 * @brief Make the CPU idle in an atomic fashion.
4530 *
4531 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4532 * must be done atomically before making the CPU idle.
4533 *
4534 * @param key Interrupt locking key obtained from irq_lock().
4535 *
4536 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004537 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004538 */
4539extern void k_cpu_atomic_idle(unsigned int key);
4540
Anas Nashif954d5502018-02-25 08:37:28 -06004541
4542/**
4543 * @internal
4544 */
Kumar Galacc334c72017-04-21 10:55:34 -05004545extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004546
Andrew Boiecdb94d62017-04-18 15:22:05 -07004547#ifdef _ARCH_EXCEPT
4548/* This archtecture has direct support for triggering a CPU exception */
4549#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4550#else
4551
Andrew Boiecdb94d62017-04-18 15:22:05 -07004552/* NOTE: This is the implementation for arches that do not implement
4553 * _ARCH_EXCEPT() to generate a real CPU exception.
4554 *
4555 * We won't have a real exception frame to determine the PC value when
4556 * the oops occurred, so print file and line number before we jump into
4557 * the fatal error handler.
4558 */
4559#define _k_except_reason(reason) do { \
4560 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4561 _NanoFatalErrorHandler(reason, &_default_esf); \
4562 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004563 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004564
4565#endif /* _ARCH__EXCEPT */
4566
4567/**
4568 * @brief Fatally terminate a thread
4569 *
4570 * This should be called when a thread has encountered an unrecoverable
4571 * runtime condition and needs to terminate. What this ultimately
4572 * means is determined by the _fatal_error_handler() implementation, which
4573 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4574 *
4575 * If this is called from ISR context, the default system fatal error handler
4576 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004577 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004578 */
4579#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4580
4581/**
4582 * @brief Fatally terminate the system
4583 *
4584 * This should be called when the Zephyr kernel has encountered an
4585 * unrecoverable runtime condition and needs to terminate. What this ultimately
4586 * means is determined by the _fatal_error_handler() implementation, which
4587 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004588 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004589 */
4590#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4591
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004592/*
4593 * private APIs that are utilized by one or more public APIs
4594 */
4595
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004596#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004597/**
4598 * @internal
4599 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004600extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004601#else
Anas Nashif954d5502018-02-25 08:37:28 -06004602/**
4603 * @internal
4604 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004605#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004606#endif
4607
Anas Nashif954d5502018-02-25 08:37:28 -06004608/**
4609 * @internal
4610 */
Flavio Ceolin09e362e2018-12-17 12:34:05 -08004611extern bool _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004612/**
4613 * @internal
4614 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004615extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004616
Andrew Boiedc5d9352017-06-02 12:56:47 -07004617/* arch/cpu.h may declare an architecture or platform-specific macro
4618 * for properly declaring stacks, compatible with MMU/MPU constraints if
4619 * enabled
4620 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004621
4622/**
4623 * @brief Obtain an extern reference to a stack
4624 *
4625 * This macro properly brings the symbol of a thread stack declared
4626 * elsewhere into scope.
4627 *
4628 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004629 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004630 */
4631#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4632
Andrew Boiedc5d9352017-06-02 12:56:47 -07004633#ifdef _ARCH_THREAD_STACK_DEFINE
4634#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4635#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4636 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304637#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004638#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4639#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004640static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004641{
4642 return _ARCH_THREAD_STACK_BUFFER(sym);
4643}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004644#else
4645/**
4646 * @brief Declare a toplevel thread stack memory region
4647 *
4648 * This declares a region of memory suitable for use as a thread's stack.
4649 *
4650 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4651 * 'noinit' section so that it isn't zeroed at boot
4652 *
Andrew Boie507852a2017-07-25 18:47:07 -07004653 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004654 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004655 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004656 *
4657 * It is legal to precede this definition with the 'static' keyword.
4658 *
4659 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4660 * parameter of k_thread_create(), it may not be the same as the
4661 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4662 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004663 * Some arches may round the size of the usable stack region up to satisfy
4664 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4665 * size.
4666 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004667 * @param sym Thread stack symbol name
4668 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004669 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004670 */
4671#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004672 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004673
4674/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304675 * @brief Calculate size of stacks to be allocated in a stack array
4676 *
4677 * This macro calculates the size to be allocated for the stacks
4678 * inside a stack array. It accepts the indicated "size" as a parameter
4679 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4680 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4681 *
4682 * @param size Size of the stack memory region
4683 * @req K-TSTACK-001
4684 */
4685#define K_THREAD_STACK_LEN(size) (size)
4686
4687/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004688 * @brief Declare a toplevel array of thread stack memory regions
4689 *
4690 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4691 * definition for additional details and constraints.
4692 *
4693 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4694 * 'noinit' section so that it isn't zeroed at boot
4695 *
4696 * @param sym Thread stack symbol name
4697 * @param nmemb Number of stacks to declare
4698 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004699 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004700 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004701#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004702 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304703 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004704
4705/**
4706 * @brief Declare an embedded stack memory region
4707 *
4708 * Used for stacks embedded within other data structures. Use is highly
4709 * discouraged but in some cases necessary. For memory protection scenarios,
4710 * it is very important that any RAM preceding this member not be writable
4711 * by threads else a stack overflow will lead to silent corruption. In other
4712 * words, the containing data structure should live in RAM owned by the kernel.
4713 *
4714 * @param sym Thread stack symbol name
4715 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004716 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004717 */
4718#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004719 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004720
4721/**
4722 * @brief Return the size in bytes of a stack memory region
4723 *
4724 * Convenience macro for passing the desired stack size to k_thread_create()
4725 * since the underlying implementation may actually create something larger
4726 * (for instance a guard area).
4727 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004728 * The value returned here is not guaranteed to match the 'size' parameter
4729 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004730 *
4731 * @param sym Stack memory symbol
4732 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004733 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004734 */
4735#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4736
4737/**
4738 * @brief Get a pointer to the physical stack buffer
4739 *
4740 * Convenience macro to get at the real underlying stack buffer used by
4741 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4742 * This is really only intended for diagnostic tools which want to examine
4743 * stack memory contents.
4744 *
4745 * @param sym Declared stack symbol name
4746 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004747 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004748 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004749static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004750{
4751 return (char *)sym;
4752}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004753
4754#endif /* _ARCH_DECLARE_STACK */
4755
Chunlin Hane9c97022017-07-07 20:29:30 +08004756/**
4757 * @defgroup mem_domain_apis Memory domain APIs
4758 * @ingroup kernel_apis
4759 * @{
4760 */
4761
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004762/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004763 * @def K_MEM_PARTITION_DEFINE
4764 * @brief Used to declare a memory partition
4765 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004766 */
4767#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4768#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4769 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304770 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004771 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004772#else
4773#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304774 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004775 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004776#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4777
Chunlin Hane9c97022017-07-07 20:29:30 +08004778/* memory partition */
4779struct k_mem_partition {
4780 /* start address of memory partition */
4781 u32_t start;
4782 /* size of memory partition */
4783 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004784#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004785 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304786 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004787#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004788};
4789
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004790/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304791 * Note: Always declare this structure with __kernel prefix
4792 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004793struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304794#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004795 /* partitions in the domain */
4796 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304797#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004798 /* domain q */
4799 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004800 /* number of partitions in the domain */
4801 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004802};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304803
Chunlin Hane9c97022017-07-07 20:29:30 +08004804
4805/**
4806 * @brief Initialize a memory domain.
4807 *
4808 * Initialize a memory domain with given name and memory partitions.
4809 *
4810 * @param domain The memory domain to be initialized.
4811 * @param num_parts The number of array items of "parts" parameter.
4812 * @param parts An array of pointers to the memory partitions. Can be NULL
4813 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004814 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004815 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004816extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004817 struct k_mem_partition *parts[]);
4818/**
4819 * @brief Destroy a memory domain.
4820 *
4821 * Destroy a memory domain.
4822 *
4823 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004824 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004825 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004826extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4827
4828/**
4829 * @brief Add a memory partition into a memory domain.
4830 *
4831 * Add a memory partition into a memory domain.
4832 *
4833 * @param domain The memory domain to be added a memory partition.
4834 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004835 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004836 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004837extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4838 struct k_mem_partition *part);
4839
4840/**
4841 * @brief Remove a memory partition from a memory domain.
4842 *
4843 * Remove a memory partition from a memory domain.
4844 *
4845 * @param domain The memory domain to be removed a memory partition.
4846 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004847 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004848 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004849extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4850 struct k_mem_partition *part);
4851
4852/**
4853 * @brief Add a thread into a memory domain.
4854 *
4855 * Add a thread into a memory domain.
4856 *
4857 * @param domain The memory domain that the thread is going to be added into.
4858 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004859 *
4860 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004861 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004862extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4863 k_tid_t thread);
4864
4865/**
4866 * @brief Remove a thread from its memory domain.
4867 *
4868 * Remove a thread from its memory domain.
4869 *
4870 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004871 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004872 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004873extern void k_mem_domain_remove_thread(k_tid_t thread);
4874
Anas Nashif166f5192018-02-25 08:02:36 -06004875/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004876
Andrew Boie756f9072017-10-10 16:01:49 -07004877/**
4878 * @brief Emit a character buffer to the console device
4879 *
4880 * @param c String of characters to print
4881 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004882 *
4883 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004884 */
4885__syscall void k_str_out(char *c, size_t n);
4886
Andy Rosse7172672018-01-24 15:48:32 -08004887/**
4888 * @brief Start a numbered CPU on a MP-capable system
4889
4890 * This starts and initializes a specific CPU. The main thread on
4891 * startup is running on CPU zero, other processors are numbered
4892 * sequentially. On return from this function, the CPU is known to
4893 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004894 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004895 * with the provided key will work to enable them.
4896 *
4897 * Normally, in SMP mode this function will be called by the kernel
4898 * initialization and should not be used as a user API. But it is
4899 * defined here for special-purpose apps which want Zephyr running on
4900 * one core and to use others for design-specific processing.
4901 *
4902 * @param cpu_num Integer number of the CPU
4903 * @param stack Stack memory for the CPU
4904 * @param sz Stack buffer size, in bytes
4905 * @param fn Function to begin running on the CPU. First argument is
4906 * an irq_unlock() key.
4907 * @param arg Untyped argument to be passed to "fn"
4908 */
4909extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004910 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004911
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004912#ifdef __cplusplus
4913}
4914#endif
4915
Anas Nashifb6304e62018-07-04 08:03:03 -05004916#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004917#include <syscalls/kernel.h>
4918
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004919#endif /* !_ASMLANGUAGE */
4920
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004921#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */