blob: 957ba0fe140d6fb275759a9810cbb824f31185d0 [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
Patrik Flykt4344e272019-03-08 14:19:05 -070084extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070085
Patrik Flykt4344e272019-03-08 14:19:05 -070086#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070087
88#else
89
Andy Rossccf3bf72018-05-10 11:10:34 -070090typedef struct {
91 sys_dlist_t waitq;
92} _wait_q_t;
93
Patrik Flykt4344e272019-03-08 14:19:05 -070094#define Z_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 Walsh456c6da2016-09-02 18:55:39 -0400118struct k_msgq;
119struct k_mbox;
120struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200121struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_fifo;
123struct k_lifo;
124struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400125struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126struct k_mem_pool;
127struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128struct k_poll_event;
129struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800130struct k_mem_domain;
131struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400132
Andrew Boie5bd891d2017-09-27 12:59:28 -0700133/* This enumeration needs to be kept in sync with the lists of kernel objects
134 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
135 * function in kernel/userspace.c
136 */
Andrew Boie945af952017-08-22 13:15:23 -0700137enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700138 K_OBJ_ANY,
139
Leandro Pereirac2003672018-04-04 13:50:32 -0700140 /** @cond
141 * Doxygen should ignore this build-time generated include file
142 * when genrating API documentation. Enumeration values are
143 * generated during build by gen_kobject_list.py. It includes
144 * basic kernel objects (e.g. pipes and mutexes) and driver types.
145 */
146#include <kobj-types-enum.h>
147 /** @endcond
148 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700149
Andrew Boie945af952017-08-22 13:15:23 -0700150 K_OBJ_LAST
151};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500152/**
153 * @defgroup usermode_apis User Mode APIs
154 * @ingroup kernel_apis
155 * @{
156 */
Andrew Boie945af952017-08-22 13:15:23 -0700157
158#ifdef CONFIG_USERSPACE
159/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700160 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700161struct _k_object {
162 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700163 u8_t perms[CONFIG_MAX_THREAD_BYTES];
164 u8_t type;
165 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700166 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700167} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700168
Andrew Boie877f82e2017-10-17 11:20:22 -0700169struct _k_object_assignment {
170 struct k_thread *thread;
171 void * const *objects;
172};
173
174/**
175 * @brief Grant a static thread access to a list of kernel objects
176 *
177 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
178 * a set of kernel objects. These objects do not need to be in an initialized
179 * state. The permissions will be granted when the threads are initialized
180 * in the early boot sequence.
181 *
182 * All arguments beyond the first must be pointers to kernel objects.
183 *
184 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
185 */
186#define K_THREAD_ACCESS_GRANT(name_, ...) \
187 static void * const _CONCAT(_object_list_, name_)[] = \
188 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400189 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
190 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700191 { (&_k_thread_obj_ ## name_), \
192 (_CONCAT(_object_list_, name_)) }
193
Andrew Boie945af952017-08-22 13:15:23 -0700194#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700195#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700196#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700197
198/**
199 * Lookup a kernel object and init its metadata if it exists
200 *
201 * Calling this on an object will make it usable from userspace.
202 * Intended to be called as the last statement in kernel object init
203 * functions.
204 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500205 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700206 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700207void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700208#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700209
210#define K_THREAD_ACCESS_GRANT(thread, ...)
211
Anas Nashif954d5502018-02-25 08:37:28 -0600212/**
213 * @internal
214 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700215static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700216{
217 ARG_UNUSED(obj);
218}
219
Anas Nashif954d5502018-02-25 08:37:28 -0600220/**
221 * @internal
222 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700223static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700224 struct k_thread *thread)
225{
226 ARG_UNUSED(object);
227 ARG_UNUSED(thread);
228}
229
Anas Nashif954d5502018-02-25 08:37:28 -0600230/**
231 * @internal
232 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700233static inline void k_object_access_revoke(void *object,
234 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700235{
236 ARG_UNUSED(object);
237 ARG_UNUSED(thread);
238}
239
Andrew Boiee9cfc542018-04-13 13:15:28 -0700240/**
241 * @internal
242 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700243static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700244{
245 ARG_UNUSED(object);
246}
247
Andrew Boie41bab6e2017-10-14 14:42:23 -0700248static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700249{
250 ARG_UNUSED(object);
251}
252#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700253
254/**
255 * grant a thread access to a kernel object
256 *
257 * The thread will be granted access to the object if the caller is from
258 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700259 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700260 *
261 * @param object Address of kernel object
262 * @param thread Thread to grant access to the object
263 */
Andrew Boie743e4682017-10-04 12:25:50 -0700264__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700265
Andrew Boiea89bf012017-10-09 14:47:55 -0700266/**
267 * grant a thread access to a kernel object
268 *
269 * The thread will lose access to the object if the caller is from
270 * supervisor mode, or the caller is from user mode AND has permissions
271 * on both the object and the thread whose access is being revoked.
272 *
273 * @param object Address of kernel object
274 * @param thread Thread to remove access to the object
275 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700276void k_object_access_revoke(void *object, struct k_thread *thread);
277
278
279__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700280
281/**
282 * grant all present and future threads access to an object
283 *
284 * If the caller is from supervisor mode, or the caller is from user mode and
285 * have sufficient permissions on the object, then that object will have
286 * permissions granted to it for *all* current and future threads running in
287 * the system, effectively becoming a public kernel object.
288 *
289 * Use of this API should be avoided on systems that are running untrusted code
290 * as it is possible for such code to derive the addresses of kernel objects
291 * and perform unwanted operations on them.
292 *
Andrew Boie04caa672017-10-13 13:57:07 -0700293 * It is not possible to revoke permissions on public objects; once public,
294 * any thread may use it.
295 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700296 * @param object Address of kernel object
297 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700298void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700299
Andrew Boie31bdfc02017-11-08 16:38:03 -0800300/**
301 * Allocate a kernel object of a designated type
302 *
303 * This will instantiate at runtime a kernel object of the specified type,
304 * returning a pointer to it. The object will be returned in an uninitialized
305 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700306 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800307 *
308 * Currently, allocation of thread stacks is not supported.
309 *
310 * @param otype Requested kernel object type
311 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
312 * available
313 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700314__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800315
Andrew Boie97bf0012018-04-24 17:01:37 -0700316#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800317/**
318 * Free a kernel object previously allocated with k_object_alloc()
319 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700320 * This will return memory for a kernel object back to resource pool it was
321 * allocated from. Care must be exercised that the object will not be used
322 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800323 *
324 * @param obj Pointer to the kernel object memory address.
325 */
326void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700327#else
Patrik Flykt4344e272019-03-08 14:19:05 -0700328static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700329{
Kumar Gala85699f72018-05-17 09:26:03 -0500330 ARG_UNUSED(otype);
331
Andrew Boie97bf0012018-04-24 17:01:37 -0700332 return NULL;
333}
334
335static inline void k_obj_free(void *obj)
336{
337 ARG_UNUSED(obj);
338}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800339#endif /* CONFIG_DYNAMIC_OBJECTS */
340
Anas Nashif4bcb2942019-01-23 23:06:29 -0500341/** @} */
342
Andrew Boiebca15da2017-10-15 14:17:48 -0700343/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700344 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700345 *
346 * The purpose of this data type is to clearly distinguish between the
347 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
348 * buffer which composes the stack data actually used by the underlying
349 * thread; they cannot be used interchangably as some arches precede the
350 * stack buffer region with guard areas that trigger a MPU or MMU fault
351 * if written to.
352 *
353 * APIs that want to work with the buffer inside should continue to use
354 * char *.
355 *
356 * Stacks should always be created with K_THREAD_STACK_DEFINE().
357 */
358struct __packed _k_thread_stack_element {
359 char data;
360};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700361typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700362
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700363/**
364 * @typedef k_thread_entry_t
365 * @brief Thread entry point function type.
366 *
367 * A thread's entry point function is invoked when the thread starts executing.
368 * Up to 3 argument values can be passed to the function.
369 *
370 * The thread terminates execution permanently if the entry point function
371 * returns. The thread is responsible for releasing any shared resources
372 * it may own (such as mutexes and dynamically allocated memory), prior to
373 * returning.
374 *
375 * @param p1 First argument.
376 * @param p2 Second argument.
377 * @param p3 Third argument.
378 *
379 * @return N/A
380 */
381typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700382
383#ifdef CONFIG_THREAD_MONITOR
384struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700385 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700386 void *parameter1;
387 void *parameter2;
388 void *parameter3;
389};
390#endif
391
392/* can be used for creating 'dummy' threads, e.g. for pending on objects */
393struct _thread_base {
394
395 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700396 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600397 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700398 struct rbnode qnode_rb;
399 };
400
Andy Ross1acd8c22018-05-03 14:51:49 -0700401 /* wait queue on which the thread is pended (needed only for
402 * trees, not dumb lists)
403 */
404 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700405
406 /* user facing 'thread options'; values defined in include/kernel.h */
407 u8_t user_options;
408
409 /* thread state */
410 u8_t thread_state;
411
412 /*
413 * scheduler lock count and thread priority
414 *
415 * These two fields control the preemptibility of a thread.
416 *
417 * When the scheduler is locked, sched_locked is decremented, which
418 * means that the scheduler is locked for values from 0xff to 0x01. A
419 * thread is coop if its prio is negative, thus 0x80 to 0xff when
420 * looked at the value as unsigned.
421 *
422 * By putting them end-to-end, this means that a thread is
423 * non-preemptible if the bundled value is greater than or equal to
424 * 0x0080.
425 */
426 union {
427 struct {
428#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
429 u8_t sched_locked;
430 s8_t prio;
431#else /* LITTLE and PDP */
432 s8_t prio;
433 u8_t sched_locked;
434#endif
435 };
436 u16_t preempt;
437 };
438
Andy Ross4a2e50f2018-05-15 11:06:25 -0700439#ifdef CONFIG_SCHED_DEADLINE
440 int prio_deadline;
441#endif
442
Andy Ross1acd8c22018-05-03 14:51:49 -0700443 u32_t order_key;
444
Andy Ross2724fd12018-01-29 14:55:20 -0800445#ifdef CONFIG_SMP
446 /* True for the per-CPU idle threads */
447 u8_t is_idle;
448
Andy Ross2724fd12018-01-29 14:55:20 -0800449 /* CPU index on which thread was last run */
450 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700451
452 /* Recursive count of irq_lock() calls */
453 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800454
455#endif
456
457#ifdef CONFIG_SCHED_CPU_MASK
458 /* "May run on" bits for each CPU */
459 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800460#endif
461
Andrew Boie73abd322017-04-04 13:19:13 -0700462 /* data returned by APIs */
463 void *swap_data;
464
465#ifdef CONFIG_SYS_CLOCK_EXISTS
466 /* this thread's entry in a timeout queue */
467 struct _timeout timeout;
468#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700469};
470
471typedef struct _thread_base _thread_base_t;
472
473#if defined(CONFIG_THREAD_STACK_INFO)
474/* Contains the stack information of a thread */
475struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700476 /* Stack start - Represents the start address of the thread-writable
477 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700478 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400479 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700480
481 /* Stack Size - Thread writable stack buffer size. Represents
482 * the size of the actual area, starting from the start member,
483 * that should be writable by the thread
484 */
Andrew Boie73abd322017-04-04 13:19:13 -0700485 u32_t size;
486};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700487
488typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700489#endif /* CONFIG_THREAD_STACK_INFO */
490
Chunlin Hane9c97022017-07-07 20:29:30 +0800491#if defined(CONFIG_USERSPACE)
492struct _mem_domain_info {
493 /* memory domain queue node */
494 sys_dnode_t mem_domain_q_node;
495 /* memory domain of the thread */
496 struct k_mem_domain *mem_domain;
497};
498
499#endif /* CONFIG_USERSPACE */
500
Daniel Leungfc182432018-08-16 15:42:28 -0700501#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
502struct _thread_userspace_local_data {
503 int errno_var;
504};
505#endif
506
Anas Nashifce78d162018-05-24 12:43:11 -0500507/**
508 * @ingroup thread_apis
509 * Thread Structure
510 */
Andrew Boie73abd322017-04-04 13:19:13 -0700511struct k_thread {
512
513 struct _thread_base base;
514
Anas Nashifce78d162018-05-24 12:43:11 -0500515 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700516 struct _callee_saved callee_saved;
517
Anas Nashifce78d162018-05-24 12:43:11 -0500518 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700519 void *init_data;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /**
522 * abort function
523 * @req K-THREAD-002
524 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700525 void (*fn_abort)(void);
526
527#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500528 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700529 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700530
Anas Nashifce78d162018-05-24 12:43:11 -0500531 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700532 struct k_thread *next_thread;
533#endif
534
Anas Nashif57554052018-03-03 02:31:05 -0600535#if defined(CONFIG_THREAD_NAME)
536 /* Thread name */
537 const char *name;
538#endif
539
Andrew Boie73abd322017-04-04 13:19:13 -0700540#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500541 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700542 void *custom_data;
543#endif
544
Daniel Leungfc182432018-08-16 15:42:28 -0700545#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
546 struct _thread_userspace_local_data *userspace_local_data;
547#endif
548
Andrew Boie73abd322017-04-04 13:19:13 -0700549#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700550#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500551 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700552 int errno_var;
553#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700554#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700555
556#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500557 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700558 struct _thread_stack_info stack_info;
559#endif /* CONFIG_THREAD_STACK_INFO */
560
Chunlin Hane9c97022017-07-07 20:29:30 +0800561#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800563 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500564 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700565 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800566#endif /* CONFIG_USERSPACE */
567
Andy Ross042d8ec2017-12-09 08:37:20 -0800568#if defined(CONFIG_USE_SWITCH)
569 /* When using __switch() a few previously arch-specific items
570 * become part of the core OS
571 */
572
Patrik Flykt4344e272019-03-08 14:19:05 -0700573 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800574 int swap_retval;
575
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600576 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800577 void *switch_handle;
578#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700580 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800581
Anas Nashifce78d162018-05-24 12:43:11 -0500582 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700583 struct _thread_arch arch;
584};
585
586typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400587typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400588
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400589enum execution_context_types {
590 K_ISR = 0,
591 K_COOP_THREAD,
592 K_PREEMPT_THREAD,
593};
594
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400595/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500596 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100597 * @{
598 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530599typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
600 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100601
602/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530603 * @brief Iterate over all the threads in the system.
604 *
605 * This routine iterates over all the threads in the system and
606 * calls the user_cb function for each thread.
607 *
608 * @param user_cb Pointer to the user callback function.
609 * @param user_data Pointer to user data.
610 *
611 * @note CONFIG_THREAD_MONITOR must be set for this function
612 * to be effective. Also this API uses irq_lock to protect the
613 * _kernel.threads list which means creation of new threads and
614 * terminations of existing threads are blocked until this
615 * API returns.
616 *
617 * @return N/A
618 */
619extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
620
Anas Nashif166f5192018-02-25 08:02:36 -0600621/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100622
623/**
Allan Stephensc98da842016-11-11 15:45:03 -0500624 * @defgroup thread_apis Thread APIs
625 * @ingroup kernel_apis
626 * @{
627 */
628
Benjamin Walshed240f22017-01-22 13:05:08 -0500629#endif /* !_ASMLANGUAGE */
630
631
632/*
633 * Thread user options. May be needed by assembly code. Common part uses low
634 * bits, arch-specific use high bits.
635 */
636
Anas Nashifa541e932018-05-24 11:19:16 -0500637/**
638 * @brief system thread that must not abort
639 * @req K-THREAD-000
640 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700641#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500642
643#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500644/**
645 * @brief thread uses floating point registers
646 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700647#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500648#endif
649
Anas Nashifa541e932018-05-24 11:19:16 -0500650/**
651 * @brief user mode thread
652 *
653 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700654 * has additional restrictions
655 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700656#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700657
Anas Nashifa541e932018-05-24 11:19:16 -0500658/**
659 * @brief Inherit Permissions
660 *
661 * @details
662 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700663 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
664 * is not enabled.
665 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700666#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700667
Benjamin Walshed240f22017-01-22 13:05:08 -0500668#ifdef CONFIG_X86
669/* x86 Bitmask definitions for threads user options */
670
671#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
672/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700673#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500674#endif
675#endif
676
677/* end - thread options */
678
679#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700681 * @brief Create a thread.
682 *
683 * This routine initializes a thread, then schedules it for execution.
684 *
685 * The new thread may be scheduled for immediate execution or a delayed start.
686 * If the newly spawned thread does not have a delayed start the kernel
687 * scheduler may preempt the current thread to allow the new thread to
688 * execute.
689 *
690 * Thread options are architecture-specific, and can include K_ESSENTIAL,
691 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
692 * them using "|" (the logical OR operator).
693 *
694 * Historically, users often would use the beginning of the stack memory region
695 * to store the struct k_thread data, although corruption will occur if the
696 * stack overflows this region and stack protection features may not detect this
697 * situation.
698 *
699 * @param new_thread Pointer to uninitialized struct k_thread
700 * @param stack Pointer to the stack space.
701 * @param stack_size Stack size in bytes.
702 * @param entry Thread entry function.
703 * @param p1 1st entry point parameter.
704 * @param p2 2nd entry point parameter.
705 * @param p3 3rd entry point parameter.
706 * @param prio Thread priority.
707 * @param options Thread options.
708 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
709 *
710 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400711 *
712 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700713 */
Andrew Boie662c3452017-10-02 10:51:18 -0700714__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700715 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700716 size_t stack_size,
717 k_thread_entry_t entry,
718 void *p1, void *p2, void *p3,
719 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700720
Andrew Boie3f091b52017-08-30 14:34:14 -0700721/**
722 * @brief Drop a thread's privileges permanently to user mode
723 *
724 * @param entry Function to start executing from
725 * @param p1 1st entry point parameter
726 * @param p2 2nd entry point parameter
727 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400728 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700729 */
730extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
731 void *p1, void *p2,
732 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700733
Andrew Boied26cf2d2017-03-30 13:07:02 -0700734/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530735 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700736 *
737 * This is a convenience function. For the provided thread, grant access to
738 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700739 *
740 * The thread object must be initialized (i.e. running). The objects don't
741 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530742 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700743 *
744 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530745 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400746 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700747 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530748#define k_thread_access_grant(thread, ...) \
749 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700750
751/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700752 * @brief Assign a resource memory pool to a thread
753 *
754 * By default, threads have no resource pool assigned unless their parent
755 * thread has a resource pool, in which case it is inherited. Multiple
756 * threads may be assigned to the same memory pool.
757 *
758 * Changing a thread's resource pool will not migrate allocations from the
759 * previous pool.
760 *
761 * @param thread Target thread to assign a memory pool for resource requests,
762 * or NULL if the thread should no longer have a memory pool.
763 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400764 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700765 */
766static inline void k_thread_resource_pool_assign(struct k_thread *thread,
767 struct k_mem_pool *pool)
768{
769 thread->resource_pool = pool;
770}
771
772#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
773/**
774 * @brief Assign the system heap as a thread's resource pool
775 *
776 * Similar to k_thread_resource_pool_assign(), but the thread will use
777 * the kernel heap to draw memory.
778 *
779 * Use with caution, as a malicious thread could perform DoS attacks on the
780 * kernel heap.
781 *
782 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400783 *
784 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700785 */
786void k_thread_system_pool_assign(struct k_thread *thread);
787#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
788
789/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700792 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700794 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200796 * @return Zero if the requested time has elapsed or the number of milliseconds
797 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700799__syscall s32_t k_sleep(s32_t ms);
800
801/**
802 * @brief Put the current thread to sleep with microsecond resolution.
803 *
804 * This function is unlikely to work as expected without kernel tuning.
805 * In particular, because the lower bound on the duration of a sleep is
806 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
807 * to achieve the resolution desired. The implications of doing this must
808 * be understood before attempting to use k_usleep(). Use with caution.
809 *
810 * @param us Number of microseconds to sleep.
811 *
812 * @return Zero if the requested time has elapsed or the number of microseconds
813 * left to sleep, if thread was woken up by \ref k_wakeup call.
814 */
815__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816
817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
820 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 * @return N/A
824 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800825__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826
827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
834 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400835 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 */
Andrew Boie468190a2017-09-29 14:00:48 -0700837__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838
839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * If @a thread is not currently sleeping, the routine has no effect.
845 *
846 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
848 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400849 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 */
Andrew Boie468190a2017-09-29 14:00:48 -0700851__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852
853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400857 *
858 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700860__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861
862/**
Allan Stephensc98da842016-11-11 15:45:03 -0500863 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * This routine permanently stops execution of @a thread. The thread is taken
866 * off all kernel queues it is part of (i.e. the ready queue, the timeout
867 * queue, or a kernel object wait queue). However, any kernel resources the
868 * thread might currently own (such as mutexes or memory blocks) are not
869 * released. It is the responsibility of the caller of this routine to ensure
870 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 *
874 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400875 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 */
Andrew Boie468190a2017-09-29 14:00:48 -0700877__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400878
Andrew Boie7d627c52017-08-30 11:01:56 -0700879
880/**
881 * @brief Start an inactive thread
882 *
883 * If a thread was created with K_FOREVER in the delay parameter, it will
884 * not be added to the scheduling queue until this function is called
885 * on it.
886 *
887 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400888 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700889 */
Andrew Boie468190a2017-09-29 14:00:48 -0700890__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700891
Allan Stephensc98da842016-11-11 15:45:03 -0500892/**
893 * @cond INTERNAL_HIDDEN
894 */
895
Benjamin Walshd211a522016-12-06 11:44:01 -0500896/* timeout has timed out and is not on _timeout_q anymore */
897#define _EXPIRED (-2)
898
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400899struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700900 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700901 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400902 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700903 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500904 void *init_p1;
905 void *init_p2;
906 void *init_p3;
907 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500908 u32_t init_options;
909 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500910 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600911 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912};
913
Andrew Boied26cf2d2017-03-30 13:07:02 -0700914#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400915 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600916 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500917 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700918 .init_thread = (thread), \
919 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500920 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700921 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400922 .init_p1 = (void *)p1, \
923 .init_p2 = (void *)p2, \
924 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500925 .init_prio = (prio), \
926 .init_options = (options), \
927 .init_delay = (delay), \
928 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600929 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930 }
931
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400932/**
Allan Stephensc98da842016-11-11 15:45:03 -0500933 * INTERNAL_HIDDEN @endcond
934 */
935
936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * @brief Statically define and initialize a thread.
938 *
939 * The thread may be scheduled for immediate execution or a delayed start.
940 *
941 * Thread options are architecture-specific, and can include K_ESSENTIAL,
942 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
943 * them using "|" (the logical OR operator).
944 *
945 * The ID of the thread can be accessed using:
946 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500947 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 *
949 * @param name Name of the thread.
950 * @param stack_size Stack size in bytes.
951 * @param entry Thread entry function.
952 * @param p1 1st entry point parameter.
953 * @param p2 2nd entry point parameter.
954 * @param p3 3rd entry point parameter.
955 * @param prio Thread priority.
956 * @param options Thread options.
957 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400958 *
Anas Nashif47420d02018-05-24 14:20:56 -0400959 * @req K-THREAD-010
960 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400961 * @internal It has been observed that the x86 compiler by default aligns
962 * these _static_thread_data structures to 32-byte boundaries, thereby
963 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400964 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400965 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500966#define K_THREAD_DEFINE(name, stack_size, \
967 entry, p1, p2, p3, \
968 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700969 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400970 struct k_thread _k_thread_obj_##name; \
971 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700972 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
973 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500974 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600975 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700976 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 * @param thread ID of thread whose priority is needed.
984 *
985 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400986 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400987 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700988__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989
990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
995 * Rescheduling can occur immediately depending on the priority @a thread is
996 * set to:
997 *
998 * - If its priority is raised above the priority of the caller of this
999 * function, and the caller is preemptible, @a thread will be scheduled in.
1000 *
1001 * - If the caller operates on itself, it lowers its priority below that of
1002 * other threads in the system, and the caller is preemptible, the thread of
1003 * highest priority will be scheduled in.
1004 *
1005 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1006 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1007 * highest priority.
1008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010 * @param prio New priority.
1011 *
1012 * @warning Changing the priority of a thread currently involved in mutex
1013 * priority inheritance may result in undefined behavior.
1014 *
1015 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001016 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001017 */
Andrew Boie468190a2017-09-29 14:00:48 -07001018__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001019
Andy Ross4a2e50f2018-05-15 11:06:25 -07001020
1021#ifdef CONFIG_SCHED_DEADLINE
1022/**
1023 * @brief Set deadline expiration time for scheduler
1024 *
1025 * This sets the "deadline" expiration as a time delta from the
1026 * current time, in the same units used by k_cycle_get_32(). The
1027 * scheduler (when deadline scheduling is enabled) will choose the
1028 * next expiring thread when selecting between threads at the same
1029 * static priority. Threads at different priorities will be scheduled
1030 * according to their static priority.
1031 *
1032 * @note Deadlines that are negative (i.e. in the past) are still seen
1033 * as higher priority than others, even if the thread has "finished"
1034 * its work. If you don't want it scheduled anymore, you have to
1035 * reset the deadline into the future, block/pend the thread, or
1036 * modify its priority with k_thread_priority_set().
1037 *
1038 * @note Despite the API naming, the scheduler makes no guarantees the
1039 * the thread WILL be scheduled within that deadline, nor does it take
1040 * extra metadata (like e.g. the "runtime" and "period" parameters in
1041 * Linux sched_setattr()) that allows the kernel to validate the
1042 * scheduling for achievability. Such features could be implemented
1043 * above this call, which is simply input to the priority selection
1044 * logic.
1045 *
1046 * @param thread A thread on which to set the deadline
1047 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001048 *
1049 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001050 */
1051__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1052#endif
1053
Andy Rossab46b1b2019-01-30 15:00:42 -08001054#ifdef CONFIG_SCHED_CPU_MASK
1055/**
1056 * @brief Sets all CPU enable masks to zero
1057 *
1058 * After this returns, the thread will no longer be schedulable on any
1059 * CPUs. The thread must not be currently runnable.
1060 *
1061 * @param thread Thread to operate upon
1062 * @return Zero on success, otherwise error code
1063 */
1064int k_thread_cpu_mask_clear(k_tid_t thread);
1065
1066/**
1067 * @brief Sets all CPU enable masks to one
1068 *
1069 * After this returns, the thread will be schedulable on any CPU. The
1070 * thread must not be currently runnable.
1071 *
1072 * @param thread Thread to operate upon
1073 * @return Zero on success, otherwise error code
1074 */
1075int k_thread_cpu_mask_enable_all(k_tid_t thread);
1076
1077/**
1078 * @brief Enable thread to run on specified CPU
1079 *
1080 * The thread must not be currently runnable.
1081 *
1082 * @param thread Thread to operate upon
1083 * @param cpu CPU index
1084 * @return Zero on success, otherwise error code
1085 */
1086int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1087
1088/**
1089 * @brief Prevent thread to run on specified CPU
1090 *
1091 * The thread must not be currently runnable.
1092 *
1093 * @param thread Thread to operate upon
1094 * @param cpu CPU index
1095 * @return Zero on success, otherwise error code
1096 */
1097int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1098#endif
1099
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * This routine prevents the kernel scheduler from making @a thread the
1104 * current thread. All other internal operations on @a thread are still
1105 * performed; for example, any timeout it is waiting on keeps ticking,
1106 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * If @a thread is already suspended, the routine has no effect.
1109 *
1110 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
1112 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001113 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 */
Andrew Boie468190a2017-09-29 14:00:48 -07001115__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116
1117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * This routine allows the kernel scheduler to make @a thread the current
1121 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * If @a thread is not currently suspended, the routine has no effect.
1124 *
1125 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
1127 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001128 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 */
Andrew Boie468190a2017-09-29 14:00:48 -07001130__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001131
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * This routine specifies how the scheduler will perform time slicing of
1136 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001138 * To enable time slicing, @a slice must be non-zero. The scheduler
1139 * ensures that no thread runs for more than the specified time limit
1140 * before other threads of that priority are given a chance to execute.
1141 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001142 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 * execute. Once the scheduler selects a thread for execution, there is no
1146 * minimum guaranteed time the thread will execute before threads of greater or
1147 * equal priority are scheduled.
1148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001149 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 * for execution, this routine has no effect; the thread is immediately
1151 * rescheduled after the slice period expires.
1152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001153 * To disable timeslicing, set both @a slice and @a prio to zero.
1154 *
1155 * @param slice Maximum time slice length (in milliseconds).
1156 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
1158 * @return N/A
1159 */
Kumar Galacc334c72017-04-21 10:55:34 -05001160extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161
Anas Nashif166f5192018-02-25 08:02:36 -06001162/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001163
1164/**
1165 * @addtogroup isr_apis
1166 * @{
1167 */
1168
1169/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001171 *
Allan Stephensc98da842016-11-11 15:45:03 -05001172 * This routine allows the caller to customize its actions, depending on
1173 * whether it is a thread or an ISR.
1174 *
1175 * @note Can be called by ISRs.
1176 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001177 * @return false if invoked by a thread.
1178 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001180extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001181
Benjamin Walsh445830d2016-11-10 15:54:27 -05001182/**
1183 * @brief Determine if code is running in a preemptible thread.
1184 *
Allan Stephensc98da842016-11-11 15:45:03 -05001185 * This routine allows the caller to customize its actions, depending on
1186 * whether it can be preempted by another thread. The routine returns a 'true'
1187 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001188 *
Allan Stephensc98da842016-11-11 15:45:03 -05001189 * - The code is running in a thread, not at ISR.
1190 * - The thread's priority is in the preemptible range.
1191 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001192 *
Allan Stephensc98da842016-11-11 15:45:03 -05001193 * @note Can be called by ISRs.
1194 *
1195 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001196 * @return Non-zero if invoked by a preemptible thread.
1197 */
Andrew Boie468190a2017-09-29 14:00:48 -07001198__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001199
Allan Stephensc98da842016-11-11 15:45:03 -05001200/**
Anas Nashif166f5192018-02-25 08:02:36 -06001201 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001202 */
1203
1204/**
1205 * @addtogroup thread_apis
1206 * @{
1207 */
1208
1209/**
1210 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001211 *
Allan Stephensc98da842016-11-11 15:45:03 -05001212 * This routine prevents the current thread from being preempted by another
1213 * thread by instructing the scheduler to treat it as a cooperative thread.
1214 * If the thread subsequently performs an operation that makes it unready,
1215 * it will be context switched out in the normal manner. When the thread
1216 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001217 *
Allan Stephensc98da842016-11-11 15:45:03 -05001218 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001219 *
Allan Stephensc98da842016-11-11 15:45:03 -05001220 * @note k_sched_lock() and k_sched_unlock() should normally be used
1221 * when the operation being performed can be safely interrupted by ISRs.
1222 * However, if the amount of processing involved is very small, better
1223 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001224 *
1225 * @return N/A
1226 */
1227extern void k_sched_lock(void);
1228
Allan Stephensc98da842016-11-11 15:45:03 -05001229/**
1230 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001231 *
Allan Stephensc98da842016-11-11 15:45:03 -05001232 * This routine reverses the effect of a previous call to k_sched_lock().
1233 * A thread must call the routine once for each time it called k_sched_lock()
1234 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001235 *
1236 * @return N/A
1237 */
1238extern void k_sched_unlock(void);
1239
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001243 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001245 * Custom data is not used by the kernel itself, and is freely available
1246 * for a thread to use as it sees fit. It can be used as a framework
1247 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001249 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001250 *
1251 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001252 *
1253 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001254 */
Andrew Boie468190a2017-09-29 14:00:48 -07001255__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001256
1257/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001258 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001260 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001263 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001264 */
Andrew Boie468190a2017-09-29 14:00:48 -07001265__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001266
1267/**
Anas Nashif57554052018-03-03 02:31:05 -06001268 * @brief Set current thread name
1269 *
1270 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1271 * tracing and debugging.
1272 *
1273 */
1274__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1275
1276/**
1277 * @brief Get thread name
1278 *
1279 * Get the name of a thread
1280 *
1281 * @param thread_id Thread ID
1282 *
1283 */
1284__syscall const char *k_thread_name_get(k_tid_t thread_id);
1285
1286/**
Andy Rosscfe62032018-09-29 07:34:55 -07001287 * @}
1288 */
1289
1290/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001291 * @addtogroup clock_apis
1292 * @{
1293 */
1294
1295/**
1296 * @brief Generate null timeout delay.
1297 *
1298 * This macro generates a timeout delay that that instructs a kernel API
1299 * not to wait if the requested operation cannot be performed immediately.
1300 *
1301 * @return Timeout delay value.
1302 */
1303#define K_NO_WAIT 0
1304
1305/**
1306 * @brief Generate timeout delay from milliseconds.
1307 *
1308 * This macro generates a timeout delay that that instructs a kernel API
1309 * to wait up to @a ms milliseconds to perform the requested operation.
1310 *
1311 * @param ms Duration in milliseconds.
1312 *
1313 * @return Timeout delay value.
1314 */
Johan Hedberg14471692016-11-13 10:52:15 +02001315#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001316
1317/**
1318 * @brief Generate timeout delay from seconds.
1319 *
1320 * This macro generates a timeout delay that that instructs a kernel API
1321 * to wait up to @a s seconds to perform the requested operation.
1322 *
1323 * @param s Duration in seconds.
1324 *
1325 * @return Timeout delay value.
1326 */
Johan Hedberg14471692016-11-13 10:52:15 +02001327#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001328
1329/**
1330 * @brief Generate timeout delay from minutes.
1331 *
1332 * This macro generates a timeout delay that that instructs a kernel API
1333 * to wait up to @a m minutes to perform the requested operation.
1334 *
1335 * @param m Duration in minutes.
1336 *
1337 * @return Timeout delay value.
1338 */
Johan Hedberg14471692016-11-13 10:52:15 +02001339#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001340
1341/**
1342 * @brief Generate timeout delay from hours.
1343 *
1344 * This macro generates a timeout delay that that instructs a kernel API
1345 * to wait up to @a h hours to perform the requested operation.
1346 *
1347 * @param h Duration in hours.
1348 *
1349 * @return Timeout delay value.
1350 */
Johan Hedberg14471692016-11-13 10:52:15 +02001351#define K_HOURS(h) K_MINUTES((h) * 60)
1352
Allan Stephensc98da842016-11-11 15:45:03 -05001353/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001354 * @brief Generate infinite timeout delay.
1355 *
1356 * This macro generates a timeout delay that that instructs a kernel API
1357 * to wait as long as necessary to perform the requested operation.
1358 *
1359 * @return Timeout delay value.
1360 */
1361#define K_FOREVER (-1)
1362
1363/**
Anas Nashif166f5192018-02-25 08:02:36 -06001364 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001365 */
1366
1367/**
Allan Stephensc98da842016-11-11 15:45:03 -05001368 * @cond INTERNAL_HIDDEN
1369 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001370
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001371struct k_timer {
1372 /*
1373 * _timeout structure must be first here if we want to use
1374 * dynamic timer allocation. timeout.node is used in the double-linked
1375 * list of free timers
1376 */
1377 struct _timeout timeout;
1378
Allan Stephens45bfa372016-10-12 12:39:42 -05001379 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380 _wait_q_t wait_q;
1381
1382 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001383 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384
1385 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001386 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001387
1388 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001389 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390
Allan Stephens45bfa372016-10-12 12:39:42 -05001391 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001392 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001394 /* user-specific data, also used to support legacy features */
1395 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001397 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398};
1399
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001400#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001402 .timeout = { \
1403 .node = {},\
1404 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001405 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001406 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001407 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001408 .expiry_fn = expiry, \
1409 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001410 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001411 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001412 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001413 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414 }
1415
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001416#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001417
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001418/**
Allan Stephensc98da842016-11-11 15:45:03 -05001419 * INTERNAL_HIDDEN @endcond
1420 */
1421
1422/**
1423 * @defgroup timer_apis Timer APIs
1424 * @ingroup kernel_apis
1425 * @{
1426 */
1427
1428/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001429 * @typedef k_timer_expiry_t
1430 * @brief Timer expiry function type.
1431 *
1432 * A timer's expiry function is executed by the system clock interrupt handler
1433 * each time the timer expires. The expiry function is optional, and is only
1434 * invoked if the timer has been initialized with one.
1435 *
1436 * @param timer Address of timer.
1437 *
1438 * @return N/A
1439 */
1440typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1441
1442/**
1443 * @typedef k_timer_stop_t
1444 * @brief Timer stop function type.
1445 *
1446 * A timer's stop function is executed if the timer is stopped prematurely.
1447 * The function runs in the context of the thread that stops the timer.
1448 * The stop function is optional, and is only invoked if the timer has been
1449 * initialized with one.
1450 *
1451 * @param timer Address of timer.
1452 *
1453 * @return N/A
1454 */
1455typedef void (*k_timer_stop_t)(struct k_timer *timer);
1456
1457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001461 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001462 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463 *
1464 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001465 * @param expiry_fn Function to invoke each time the timer expires.
1466 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001467 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001468#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001469 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001470 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001471
Allan Stephens45bfa372016-10-12 12:39:42 -05001472/**
1473 * @brief Initialize a timer.
1474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001476 *
1477 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * @param expiry_fn Function to invoke each time the timer expires.
1479 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001480 *
1481 * @return N/A
1482 */
1483extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001484 k_timer_expiry_t expiry_fn,
1485 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001486
Allan Stephens45bfa372016-10-12 12:39:42 -05001487/**
1488 * @brief Start a timer.
1489 *
1490 * This routine starts a timer, and resets its status to zero. The timer
1491 * begins counting down using the specified duration and period values.
1492 *
1493 * Attempting to start a timer that is already running is permitted.
1494 * The timer's status is reset to zero and the timer begins counting down
1495 * using the new duration and period values.
1496 *
1497 * @param timer Address of timer.
1498 * @param duration Initial timer duration (in milliseconds).
1499 * @param period Timer period (in milliseconds).
1500 *
1501 * @return N/A
1502 */
Andrew Boiea354d492017-09-29 16:22:28 -07001503__syscall void k_timer_start(struct k_timer *timer,
1504 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001505
1506/**
1507 * @brief Stop a timer.
1508 *
1509 * This routine stops a running timer prematurely. The timer's stop function,
1510 * if one exists, is invoked by the caller.
1511 *
1512 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001513 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001514 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001515 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1516 * if @a k_timer_stop is to be called from ISRs.
1517 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001518 * @param timer Address of timer.
1519 *
1520 * @return N/A
1521 */
Andrew Boiea354d492017-09-29 16:22:28 -07001522__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001523
1524/**
1525 * @brief Read timer status.
1526 *
1527 * This routine reads the timer's status, which indicates the number of times
1528 * it has expired since its status was last read.
1529 *
1530 * Calling this routine resets the timer's status to zero.
1531 *
1532 * @param timer Address of timer.
1533 *
1534 * @return Timer status.
1535 */
Andrew Boiea354d492017-09-29 16:22:28 -07001536__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001537
1538/**
1539 * @brief Synchronize thread to timer expiration.
1540 *
1541 * This routine blocks the calling thread until the timer's status is non-zero
1542 * (indicating that it has expired at least once since it was last examined)
1543 * or the timer is stopped. If the timer status is already non-zero,
1544 * or the timer is already stopped, the caller continues without waiting.
1545 *
1546 * Calling this routine resets the timer's status to zero.
1547 *
1548 * This routine must not be used by interrupt handlers, since they are not
1549 * allowed to block.
1550 *
1551 * @param timer Address of timer.
1552 *
1553 * @return Timer status.
1554 */
Andrew Boiea354d492017-09-29 16:22:28 -07001555__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001556
Andy Ross52e444b2018-09-28 09:06:37 -07001557extern s32_t z_timeout_remaining(struct _timeout *timeout);
1558
Allan Stephens45bfa372016-10-12 12:39:42 -05001559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001560 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001561 *
1562 * This routine computes the (approximate) time remaining before a running
1563 * timer next expires. If the timer is not running, it returns zero.
1564 *
1565 * @param timer Address of timer.
1566 *
1567 * @return Remaining time (in milliseconds).
1568 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001569__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001570
Patrik Flykt4344e272019-03-08 14:19:05 -07001571static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001572{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001573 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1574 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001575}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001576
Allan Stephensc98da842016-11-11 15:45:03 -05001577/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001578 * @brief Associate user-specific data with a timer.
1579 *
1580 * This routine records the @a user_data with the @a timer, to be retrieved
1581 * later.
1582 *
1583 * It can be used e.g. in a timer handler shared across multiple subsystems to
1584 * retrieve data specific to the subsystem this timer is associated with.
1585 *
1586 * @param timer Address of timer.
1587 * @param user_data User data to associate with the timer.
1588 *
1589 * @return N/A
1590 */
Andrew Boiea354d492017-09-29 16:22:28 -07001591__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1592
Anas Nashif954d5502018-02-25 08:37:28 -06001593/**
1594 * @internal
1595 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001596static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001597 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001598{
1599 timer->user_data = user_data;
1600}
1601
1602/**
1603 * @brief Retrieve the user-specific data from a timer.
1604 *
1605 * @param timer Address of timer.
1606 *
1607 * @return The user data.
1608 */
Andrew Boiea354d492017-09-29 16:22:28 -07001609__syscall void *k_timer_user_data_get(struct k_timer *timer);
1610
Patrik Flykt4344e272019-03-08 14:19:05 -07001611static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001612{
1613 return timer->user_data;
1614}
1615
Anas Nashif166f5192018-02-25 08:02:36 -06001616/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001617
Allan Stephensc98da842016-11-11 15:45:03 -05001618/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001619 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001620 * @{
1621 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001622
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001623/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * This routine returns the elapsed time since the system booted,
1627 * in milliseconds.
1628 *
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001629 * @note While this function returns time in milliseconds, it does not mean it
1630 * has millisecond resolution. The actual resolution depends on
1631 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the default
1632 * setting of 100, system time is updated in increments of 10ms.
1633 *
1634 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001635 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001636__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001637
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001638/**
1639 * @brief Enable clock always on in tickless kernel
1640 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001641 * This routine enables keeping the clock running (that is, it always
1642 * keeps an active timer interrupt scheduled) when there are no timer
1643 * events programmed in tickless kernel scheduling. This is necessary
1644 * if the clock is used to track passage of time (e.g. via
1645 * k_uptime_get_32()), otherwise the internal hardware counter may
1646 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001647 *
1648 * @retval prev_status Previous status of always on flag
1649 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001650int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001651
1652/**
1653 * @brief Disable clock always on in tickless kernel
1654 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001655 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001656 * there are no timer events programmed in tickless kernel
1657 * scheduling. To save power, this routine should be called
1658 * immediately when clock is not used to track time.
1659 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001660void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001661
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001663 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001665 * This routine returns the lower 32-bits of the elapsed time since the system
1666 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * This routine can be more efficient than k_uptime_get(), as it reduces the
1669 * need for interrupt locking and 64-bit math. However, the 32-bit result
1670 * cannot hold a system uptime time larger than approximately 50 days, so the
1671 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001672 *
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001673 * @note While this function returns time in milliseconds, it does not mean it
1674 * has millisecond resolution. The actual resolution depends on
1675 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the default
1676 * setting of 100, system time is updated in increments of 10ms.
1677 *
1678 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001679 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001680__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001681
1682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * This routine computes the elapsed time between the current system uptime
1686 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * @param reftime Pointer to a reference time, which is updated to the current
1689 * uptime upon return.
1690 *
1691 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001692 */
Andy Ross987c0e52018-09-27 16:50:00 -07001693static inline s64_t k_uptime_delta(s64_t *reftime)
1694{
1695 s64_t uptime, delta;
1696
1697 uptime = k_uptime_get();
1698 delta = uptime - *reftime;
1699 *reftime = uptime;
1700
1701 return delta;
1702}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703
1704/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001707 * This routine computes the elapsed time between the current system uptime
1708 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1711 * need for interrupt locking and 64-bit math. However, the 32-bit result
1712 * cannot hold an elapsed time larger than approximately 50 days, so the
1713 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * @param reftime Pointer to a reference time, which is updated to the current
1716 * uptime upon return.
1717 *
1718 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001719 */
Andy Ross987c0e52018-09-27 16:50:00 -07001720static inline u32_t k_uptime_delta_32(s64_t *reftime)
1721{
1722 return (u32_t)k_uptime_delta(reftime);
1723}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001724
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * This routine returns the current time, as measured by the system's hardware
1729 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001733#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001735/**
Anas Nashif166f5192018-02-25 08:02:36 -06001736 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001737 */
1738
Allan Stephensc98da842016-11-11 15:45:03 -05001739/**
1740 * @cond INTERNAL_HIDDEN
1741 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001742
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001743struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001744 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001745 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001746 union {
1747 _wait_q_t wait_q;
1748
1749 _POLL_EVENT;
1750 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001751
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001752 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001753};
1754
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001755#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001756 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001757 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001758 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001759 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001760 _OBJECT_TRACING_INIT \
1761 }
1762
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001763#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1764
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001765extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1766
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001767/**
1768 * INTERNAL_HIDDEN @endcond
1769 */
1770
1771/**
1772 * @defgroup queue_apis Queue APIs
1773 * @ingroup kernel_apis
1774 * @{
1775 */
1776
1777/**
1778 * @brief Initialize a queue.
1779 *
1780 * This routine initializes a queue object, prior to its first use.
1781 *
1782 * @param queue Address of the queue.
1783 *
1784 * @return N/A
1785 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001786__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001787
1788/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001789 * @brief Cancel waiting on a queue.
1790 *
1791 * This routine causes first thread pending on @a queue, if any, to
1792 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001793 * If the queue is being waited on by k_poll(), it will return with
1794 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1795 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001796 *
1797 * @note Can be called by ISRs.
1798 *
1799 * @param queue Address of the queue.
1800 *
1801 * @return N/A
1802 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001803__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001804
1805/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001806 * @brief Append an element to the end of a queue.
1807 *
1808 * This routine appends a data item to @a queue. A queue data item must be
1809 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1810 * reserved for the kernel's use.
1811 *
1812 * @note Can be called by ISRs.
1813 *
1814 * @param queue Address of the queue.
1815 * @param data Address of the data item.
1816 *
1817 * @return N/A
1818 */
1819extern void k_queue_append(struct k_queue *queue, void *data);
1820
1821/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001822 * @brief Append an element to a queue.
1823 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001824 * This routine appends a data item to @a queue. There is an implicit memory
1825 * allocation to create an additional temporary bookkeeping data structure from
1826 * the calling thread's resource pool, which is automatically freed when the
1827 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001828 *
1829 * @note Can be called by ISRs.
1830 *
1831 * @param queue Address of the queue.
1832 * @param data Address of the data item.
1833 *
1834 * @retval 0 on success
1835 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1836 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301837__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001838
1839/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840 * @brief Prepend an element to a queue.
1841 *
1842 * This routine prepends a data item to @a queue. A queue data item must be
1843 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1844 * reserved for the kernel's use.
1845 *
1846 * @note Can be called by ISRs.
1847 *
1848 * @param queue Address of the queue.
1849 * @param data Address of the data item.
1850 *
1851 * @return N/A
1852 */
1853extern void k_queue_prepend(struct k_queue *queue, void *data);
1854
1855/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001856 * @brief Prepend an element to a queue.
1857 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001858 * This routine prepends a data item to @a queue. There is an implicit memory
1859 * allocation to create an additional temporary bookkeeping data structure from
1860 * the calling thread's resource pool, which is automatically freed when the
1861 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001862 *
1863 * @note Can be called by ISRs.
1864 *
1865 * @param queue Address of the queue.
1866 * @param data Address of the data item.
1867 *
1868 * @retval 0 on success
1869 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1870 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301871__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001872
1873/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001874 * @brief Inserts an element to a queue.
1875 *
1876 * This routine inserts a data item to @a queue after previous item. A queue
1877 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1878 * item are reserved for the kernel's use.
1879 *
1880 * @note Can be called by ISRs.
1881 *
1882 * @param queue Address of the queue.
1883 * @param prev Address of the previous data item.
1884 * @param data Address of the data item.
1885 *
1886 * @return N/A
1887 */
1888extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1889
1890/**
1891 * @brief Atomically append a list of elements to a queue.
1892 *
1893 * This routine adds a list of data items to @a queue in one operation.
1894 * The data items must be in a singly-linked list, with the first 32 bits
1895 * in each data item pointing to the next data item; the list must be
1896 * NULL-terminated.
1897 *
1898 * @note Can be called by ISRs.
1899 *
1900 * @param queue Address of the queue.
1901 * @param head Pointer to first node in singly-linked list.
1902 * @param tail Pointer to last node in singly-linked list.
1903 *
1904 * @return N/A
1905 */
1906extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1907
1908/**
1909 * @brief Atomically add a list of elements to a queue.
1910 *
1911 * This routine adds a list of data items to @a queue in one operation.
1912 * The data items must be in a singly-linked list implemented using a
1913 * sys_slist_t object. Upon completion, the original list is empty.
1914 *
1915 * @note Can be called by ISRs.
1916 *
1917 * @param queue Address of the queue.
1918 * @param list Pointer to sys_slist_t object.
1919 *
1920 * @return N/A
1921 */
1922extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1923
1924/**
1925 * @brief Get an element from a queue.
1926 *
1927 * This routine removes first data item from @a queue. The first 32 bits of the
1928 * data item are reserved for the kernel's use.
1929 *
1930 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1931 *
1932 * @param queue Address of the queue.
1933 * @param timeout Waiting period to obtain a data item (in milliseconds),
1934 * or one of the special values K_NO_WAIT and K_FOREVER.
1935 *
1936 * @return Address of the data item if successful; NULL if returned
1937 * without waiting, or waiting period timed out.
1938 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001939__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001940
1941/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001942 * @brief Remove an element from a queue.
1943 *
1944 * This routine removes data item from @a queue. The first 32 bits of the
1945 * data item are reserved for the kernel's use. Removing elements from k_queue
1946 * rely on sys_slist_find_and_remove which is not a constant time operation.
1947 *
1948 * @note Can be called by ISRs
1949 *
1950 * @param queue Address of the queue.
1951 * @param data Address of the data item.
1952 *
1953 * @return true if data item was removed
1954 */
1955static inline bool k_queue_remove(struct k_queue *queue, void *data)
1956{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001957 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001958}
1959
1960/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001961 * @brief Append an element to a queue only if it's not present already.
1962 *
1963 * This routine appends data item to @a queue. The first 32 bits of the
1964 * data item are reserved for the kernel's use. Appending elements to k_queue
1965 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1966 *
1967 * @note Can be called by ISRs
1968 *
1969 * @param queue Address of the queue.
1970 * @param data Address of the data item.
1971 *
1972 * @return true if data item was added, false if not
1973 */
1974static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1975{
1976 sys_sfnode_t *test;
1977
1978 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1979 if (test == (sys_sfnode_t *) data) {
1980 return false;
1981 }
1982 }
1983
1984 k_queue_append(queue, data);
1985 return true;
1986}
1987
1988/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001989 * @brief Query a queue to see if it has data available.
1990 *
1991 * Note that the data might be already gone by the time this function returns
1992 * if other threads are also trying to read from the queue.
1993 *
1994 * @note Can be called by ISRs.
1995 *
1996 * @param queue Address of the queue.
1997 *
1998 * @return Non-zero if the queue is empty.
1999 * @return 0 if data is available.
2000 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002001__syscall int k_queue_is_empty(struct k_queue *queue);
2002
Patrik Flykt4344e272019-03-08 14:19:05 -07002003static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002004{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002005 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002006}
2007
2008/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002009 * @brief Peek element at the head of queue.
2010 *
2011 * Return element from the head of queue without removing it.
2012 *
2013 * @param queue Address of the queue.
2014 *
2015 * @return Head element, or NULL if queue is empty.
2016 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002017__syscall void *k_queue_peek_head(struct k_queue *queue);
2018
Patrik Flykt4344e272019-03-08 14:19:05 -07002019static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002020{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002021 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002022}
2023
2024/**
2025 * @brief Peek element at the tail of queue.
2026 *
2027 * Return element from the tail of queue without removing it.
2028 *
2029 * @param queue Address of the queue.
2030 *
2031 * @return Tail element, or NULL if queue is empty.
2032 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002033__syscall void *k_queue_peek_tail(struct k_queue *queue);
2034
Patrik Flykt4344e272019-03-08 14:19:05 -07002035static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002036{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002037 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002038}
2039
2040/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002041 * @brief Statically define and initialize a queue.
2042 *
2043 * The queue can be accessed outside the module where it is defined using:
2044 *
2045 * @code extern struct k_queue <name>; @endcode
2046 *
2047 * @param name Name of the queue.
2048 */
2049#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002050 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002051 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002052
Anas Nashif166f5192018-02-25 08:02:36 -06002053/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002054
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002055struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002056 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057};
2058
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002059/**
2060 * @cond INTERNAL_HIDDEN
2061 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002062#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002063 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002064 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002065 }
2066
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002067#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002068
Allan Stephensc98da842016-11-11 15:45:03 -05002069/**
2070 * INTERNAL_HIDDEN @endcond
2071 */
2072
2073/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002074 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002075 * @ingroup kernel_apis
2076 * @{
2077 */
2078
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002080 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002082 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002084 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085 *
2086 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002087 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002089#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002090 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091
2092/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002093 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002094 *
2095 * This routine causes first thread pending on @a fifo, if any, to
2096 * return from k_fifo_get() call with NULL value (as if timeout
2097 * expired).
2098 *
2099 * @note Can be called by ISRs.
2100 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002101 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002102 *
2103 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002104 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002105 */
2106#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002107 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002108
2109/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002110 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002113 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2114 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002116 * @note Can be called by ISRs.
2117 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002118 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002120 *
2121 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002122 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002123 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002124#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002125 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126
2127/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002128 * @brief Add an element to a FIFO queue.
2129 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002130 * This routine adds a data item to @a fifo. There is an implicit memory
2131 * allocation to create an additional temporary bookkeeping data structure from
2132 * the calling thread's resource pool, which is automatically freed when the
2133 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002134 *
2135 * @note Can be called by ISRs.
2136 *
2137 * @param fifo Address of the FIFO.
2138 * @param data Address of the data item.
2139 *
2140 * @retval 0 on success
2141 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002142 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002143 */
2144#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002145 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002146
2147/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * This routine adds a list of data items to @a fifo in one operation.
2151 * The data items must be in a singly-linked list, with the first 32 bits
2152 * each data item pointing to the next data item; the list must be
2153 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002155 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002157 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002158 * @param head Pointer to first node in singly-linked list.
2159 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
2161 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002162 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002164#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002165 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166
2167/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002168 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * This routine adds a list of data items to @a fifo in one operation.
2171 * The data items must be in a singly-linked list implemented using a
2172 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 * and must be re-initialized via sys_slist_init().
2174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002175 * @note Can be called by ISRs.
2176 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002177 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
2180 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002181 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002183#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002184 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185
2186/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002187 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002189 * This routine removes a data item from @a fifo in a "first in, first out"
2190 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002192 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2193 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002194 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002196 * or one of the special values K_NO_WAIT and K_FOREVER.
2197 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002198 * @return Address of the data item if successful; NULL if returned
2199 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002200 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002201 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002202#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002203 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002204
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002207 *
2208 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002209 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002210 *
2211 * @note Can be called by ISRs.
2212 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002214 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002216 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002217 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002218 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002219#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002220 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002221
2222/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002223 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002224 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002225 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302226 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002227 * on each iteration of processing, a head container will be peeked,
2228 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002229 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002230 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002232 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002233 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002234 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002235 */
2236#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002237 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002238
2239/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002240 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002241 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002242 * Return element from the tail of FIFO queue (without removing it). A usecase
2243 * for this is if elements of the FIFO queue are themselves containers. Then
2244 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002247 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002249 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002250 */
2251#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002252 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002253
2254/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002255 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002259 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002262 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002265 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002266 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267
Anas Nashif166f5192018-02-25 08:02:36 -06002268/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002269
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002271 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272};
2273
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002274/**
2275 * @cond INTERNAL_HIDDEN
2276 */
2277
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002278#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002279 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002280 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002281 }
2282
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002283#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2284
Allan Stephensc98da842016-11-11 15:45:03 -05002285/**
2286 * INTERNAL_HIDDEN @endcond
2287 */
2288
2289/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002290 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002291 * @ingroup kernel_apis
2292 * @{
2293 */
2294
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002296 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002298 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002300 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301 *
2302 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002303 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002305#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002306 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002307
2308/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002309 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002311 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002312 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2313 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * @note Can be called by ISRs.
2316 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002317 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319 *
2320 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002321 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002322 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002323#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002324 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325
2326/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002327 * @brief Add an element to a LIFO queue.
2328 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002329 * This routine adds a data item to @a lifo. There is an implicit memory
2330 * allocation to create an additional temporary bookkeeping data structure from
2331 * the calling thread's resource pool, which is automatically freed when the
2332 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002333 *
2334 * @note Can be called by ISRs.
2335 *
2336 * @param lifo Address of the LIFO.
2337 * @param data Address of the data item.
2338 *
2339 * @retval 0 on success
2340 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002341 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002342 */
2343#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002344 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002345
2346/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002347 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * This routine removes a data item from @a lifo in a "last in, first out"
2350 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2353 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 * or one of the special values K_NO_WAIT and K_FOREVER.
2357 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002358 * @return Address of the data item if successful; NULL if returned
2359 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002360 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002362#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002363 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002366 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002367 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002368 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002370 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002373 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002376 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002377 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378
Anas Nashif166f5192018-02-25 08:02:36 -06002379/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002380
2381/**
2382 * @cond INTERNAL_HIDDEN
2383 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302384#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385
2386struct k_stack {
2387 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002388 struct k_spinlock lock;
Kumar Galacc334c72017-04-21 10:55:34 -05002389 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002390
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002391 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002392 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393};
2394
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002395#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002396 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002397 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002398 .base = stack_buffer, \
2399 .next = stack_buffer, \
2400 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002401 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002402 }
2403
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002404#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2405
Allan Stephensc98da842016-11-11 15:45:03 -05002406/**
2407 * INTERNAL_HIDDEN @endcond
2408 */
2409
2410/**
2411 * @defgroup stack_apis Stack APIs
2412 * @ingroup kernel_apis
2413 * @{
2414 */
2415
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416/**
2417 * @brief Initialize a stack.
2418 *
2419 * This routine initializes a stack object, prior to its first use.
2420 *
2421 * @param stack Address of the stack.
2422 * @param buffer Address of array used to hold stacked values.
2423 * @param num_entries Maximum number of values that can be stacked.
2424 *
2425 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002426 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 */
Andrew Boief3bee952018-05-02 17:44:39 -07002428void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302429 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002430
2431
2432/**
2433 * @brief Initialize a stack.
2434 *
2435 * This routine initializes a stack object, prior to its first use. Internal
2436 * buffers will be allocated from the calling thread's resource pool.
2437 * This memory will be released if k_stack_cleanup() is called, or
2438 * userspace is enabled and the stack object loses all references to it.
2439 *
2440 * @param stack Address of the stack.
2441 * @param num_entries Maximum number of values that can be stacked.
2442 *
2443 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002444 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002445 */
2446
Adithya Baglody28080d32018-10-15 11:48:51 +05302447__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2448 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002449
2450/**
2451 * @brief Release a stack's allocated buffer
2452 *
2453 * If a stack object was given a dynamically allocated buffer via
2454 * k_stack_alloc_init(), this will free it. This function does nothing
2455 * if the buffer wasn't dynamically allocated.
2456 *
2457 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002458 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002459 */
2460void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461
2462/**
2463 * @brief Push an element onto a stack.
2464 *
2465 * This routine adds a 32-bit value @a data to @a stack.
2466 *
2467 * @note Can be called by ISRs.
2468 *
2469 * @param stack Address of the stack.
2470 * @param data Value to push onto the stack.
2471 *
2472 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002473 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 */
Andrew Boiee8734462017-09-29 16:42:07 -07002475__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476
2477/**
2478 * @brief Pop an element from a stack.
2479 *
2480 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2481 * manner and stores the value in @a data.
2482 *
2483 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2484 *
2485 * @param stack Address of the stack.
2486 * @param data Address of area to hold the value popped from the stack.
2487 * @param timeout Waiting period to obtain a value (in milliseconds),
2488 * or one of the special values K_NO_WAIT and K_FOREVER.
2489 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002490 * @retval 0 Element popped from stack.
2491 * @retval -EBUSY Returned without waiting.
2492 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002493 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 */
Andrew Boiee8734462017-09-29 16:42:07 -07002495__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002502 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @param name Name of the stack.
2505 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002506 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002508#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002509 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002510 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002511 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002512 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002513 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514
Anas Nashif166f5192018-02-25 08:02:36 -06002515/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002516
Allan Stephens6bba9b02016-11-16 14:56:54 -05002517struct k_work;
2518
Allan Stephensc98da842016-11-11 15:45:03 -05002519/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002520 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002521 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522 */
2523
Allan Stephens6bba9b02016-11-16 14:56:54 -05002524/**
2525 * @typedef k_work_handler_t
2526 * @brief Work item handler function type.
2527 *
2528 * A work item's handler function is executed by a workqueue's thread
2529 * when the work item is processed by the workqueue.
2530 *
2531 * @param work Address of the work item.
2532 *
2533 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002534 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002535 */
2536typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537
2538/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002539 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002541
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002543 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002544 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545};
2546
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002548 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549};
2550
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002552 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553 k_work_handler_t handler;
2554 atomic_t flags[1];
2555};
2556
Allan Stephens6bba9b02016-11-16 14:56:54 -05002557struct k_delayed_work {
2558 struct k_work work;
2559 struct _timeout timeout;
2560 struct k_work_q *work_q;
2561};
2562
2563extern struct k_work_q k_sys_work_q;
2564
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002566 * INTERNAL_HIDDEN @endcond
2567 */
2568
Patrik Flykt4344e272019-03-08 14:19:05 -07002569#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002570 { \
2571 ._reserved = NULL, \
2572 .handler = work_handler, \
2573 .flags = { 0 } \
2574 }
2575
Patrik Flykt4344e272019-03-08 14:19:05 -07002576#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002577
Allan Stephens6bba9b02016-11-16 14:56:54 -05002578/**
2579 * @brief Initialize a statically-defined work item.
2580 *
2581 * This macro can be used to initialize a statically-defined workqueue work
2582 * item, prior to its first use. For example,
2583 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002584 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002585 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002586 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002587 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002588 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002590#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002591 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592
2593/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002594 * @brief Initialize a work item.
2595 *
2596 * This routine initializes a workqueue work item, prior to its first use.
2597 *
2598 * @param work Address of work item.
2599 * @param handler Function to invoke each time work item is processed.
2600 *
2601 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002602 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603 */
2604static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2605{
Patrik Flykt4344e272019-03-08 14:19:05 -07002606 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607}
2608
2609/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002610 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002611 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002612 * This routine submits work item @a work to be processed by workqueue
2613 * @a work_q. If the work item is already pending in the workqueue's queue
2614 * as a result of an earlier submission, this routine has no effect on the
2615 * work item. If the work item has already been processed, or is currently
2616 * being processed, its work is considered complete and the work item can be
2617 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002618 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * @warning
2620 * A submitted work item must not be modified until it has been processed
2621 * by the workqueue.
2622 *
2623 * @note Can be called by ISRs.
2624 *
2625 * @param work_q Address of workqueue.
2626 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002627 *
2628 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002629 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630 */
2631static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2632 struct k_work *work)
2633{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002634 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002635 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636 }
2637}
2638
2639/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002640 * @brief Submit a work item to a user mode workqueue
2641 *
David B. Kinder06d78352018-12-17 14:32:40 -08002642 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002643 * memory allocation is made from the caller's resource pool which is freed
2644 * once the worker thread consumes the k_work item. The workqueue
2645 * thread must have memory access to the k_work item being submitted. The caller
2646 * must have permission granted on the work_q parameter's queue object.
2647 *
2648 * Otherwise this works the same as k_work_submit_to_queue().
2649 *
2650 * @note Can be called by ISRs.
2651 *
2652 * @param work_q Address of workqueue.
2653 * @param work Address of work item.
2654 *
2655 * @retval -EBUSY if the work item was already in some workqueue
2656 * @retval -ENOMEM if no memory for thread resource pool allocation
2657 * @retval 0 Success
2658 * @req K-WORK-001
2659 */
2660static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2661 struct k_work *work)
2662{
2663 int ret = -EBUSY;
2664
2665 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2666 ret = k_queue_alloc_append(&work_q->queue, work);
2667
2668 /* Couldn't insert into the queue. Clear the pending bit
2669 * so the work item can be submitted again
2670 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002671 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002672 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2673 }
2674 }
2675
2676 return ret;
2677}
2678
2679/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002680 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002682 * This routine indicates if work item @a work is pending in a workqueue's
2683 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002684 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002685 * @note Can be called by ISRs.
2686 *
2687 * @param work Address of work item.
2688 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002689 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002690 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002691 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002692static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002693{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002694 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002695}
2696
2697/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002698 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002699 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002700 * This routine starts workqueue @a work_q. The workqueue spawns its work
2701 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002702 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002703 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002704 * @param stack Pointer to work queue thread's stack space, as defined by
2705 * K_THREAD_STACK_DEFINE()
2706 * @param stack_size Size of the work queue thread's stack (in bytes), which
2707 * should either be the same constant passed to
2708 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002709 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002710 *
2711 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002712 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 */
Andrew Boie507852a2017-07-25 18:47:07 -07002714extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002715 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002716 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002719 * @brief Start a workqueue in user mode
2720 *
2721 * This works identically to k_work_q_start() except it is callable from user
2722 * mode, and the worker thread created will run in user mode.
2723 * The caller must have permissions granted on both the work_q parameter's
2724 * thread and queue objects, and the same restrictions on priority apply as
2725 * k_thread_create().
2726 *
2727 * @param work_q Address of workqueue.
2728 * @param stack Pointer to work queue thread's stack space, as defined by
2729 * K_THREAD_STACK_DEFINE()
2730 * @param stack_size Size of the work queue thread's stack (in bytes), which
2731 * should either be the same constant passed to
2732 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2733 * @param prio Priority of the work queue's thread.
2734 *
2735 * @return N/A
2736 * @req K-WORK-001
2737 */
2738extern void k_work_q_user_start(struct k_work_q *work_q,
2739 k_thread_stack_t *stack,
2740 size_t stack_size, int prio);
2741
2742/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002743 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002745 * This routine initializes a workqueue delayed work item, prior to
2746 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002747 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002748 * @param work Address of delayed work item.
2749 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750 *
2751 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002752 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002754extern void k_delayed_work_init(struct k_delayed_work *work,
2755 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756
2757/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002758 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002760 * This routine schedules work item @a work to be processed by workqueue
2761 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002762 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002763 * Only when the countdown completes is the work item actually submitted to
2764 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002766 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002767 * counting down cancels the existing submission and restarts the
2768 * countdown using the new delay. Note that this behavior is
2769 * inherently subject to race conditions with the pre-existing
2770 * timeouts and work queue, so care must be taken to synchronize such
2771 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002773 * @warning
2774 * A delayed work item must not be modified until it has been processed
2775 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002777 * @note Can be called by ISRs.
2778 *
2779 * @param work_q Address of workqueue.
2780 * @param work Address of delayed work item.
2781 * @param delay Delay before submitting the work item (in milliseconds).
2782 *
2783 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002784 * @retval -EINVAL Work item is being processed or has completed its work.
2785 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002786 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002788extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2789 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002790 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791
2792/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002793 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002795 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002796 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002797 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002799 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002801 * @note The result of calling this on a k_delayed_work item that has
2802 * not been submitted (i.e. before the return of the
2803 * k_delayed_work_submit_to_queue() call) is undefined.
2804 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002805 * @param work Address of delayed work item.
2806 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002807 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002808 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002809 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002810 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002811extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002813/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002814 * @brief Submit a work item to the system workqueue.
2815 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002816 * This routine submits work item @a work to be processed by the system
2817 * workqueue. If the work item is already pending in the workqueue's queue
2818 * as a result of an earlier submission, this routine has no effect on the
2819 * work item. If the work item has already been processed, or is currently
2820 * being processed, its work is considered complete and the work item can be
2821 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002823 * @warning
2824 * Work items submitted to the system workqueue should avoid using handlers
2825 * that block or yield since this may prevent the system workqueue from
2826 * processing other work items in a timely manner.
2827 *
2828 * @note Can be called by ISRs.
2829 *
2830 * @param work Address of work item.
2831 *
2832 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002833 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 */
2835static inline void k_work_submit(struct k_work *work)
2836{
2837 k_work_submit_to_queue(&k_sys_work_q, work);
2838}
2839
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002840/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 * @brief Submit a delayed work item to the system workqueue.
2842 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002843 * This routine schedules work item @a work to be processed by the system
2844 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002845 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002846 * Only when the countdown completes is the work item actually submitted to
2847 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002849 * Submitting a previously submitted delayed work item that is still
2850 * counting down cancels the existing submission and restarts the countdown
2851 * using the new delay. If the work item is currently pending on the
2852 * workqueue's queue because the countdown has completed it is too late to
2853 * resubmit the item, and resubmission fails without impacting the work item.
2854 * If the work item has already been processed, or is currently being processed,
2855 * its work is considered complete and the work item can be resubmitted.
2856 *
2857 * @warning
2858 * Work items submitted to the system workqueue should avoid using handlers
2859 * that block or yield since this may prevent the system workqueue from
2860 * processing other work items in a timely manner.
2861 *
2862 * @note Can be called by ISRs.
2863 *
2864 * @param work Address of delayed work item.
2865 * @param delay Delay before submitting the work item (in milliseconds).
2866 *
2867 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002868 * @retval -EINVAL Work item is being processed or has completed its work.
2869 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002870 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002871 */
2872static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002873 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002875 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002876}
2877
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002879 * @brief Get time remaining before a delayed work gets scheduled.
2880 *
2881 * This routine computes the (approximate) time remaining before a
2882 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002883 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002884 *
2885 * @param work Delayed work item.
2886 *
2887 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002888 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002889 */
Kumar Galacc334c72017-04-21 10:55:34 -05002890static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002891{
Andy Ross52e444b2018-09-28 09:06:37 -07002892 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002893}
2894
Anas Nashif166f5192018-02-25 08:02:36 -06002895/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002896/**
Anas Nashifce78d162018-05-24 12:43:11 -05002897 * @defgroup mutex_apis Mutex APIs
2898 * @ingroup kernel_apis
2899 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002900 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901
Anas Nashifce78d162018-05-24 12:43:11 -05002902/**
2903 * Mutex Structure
2904 * @ingroup mutex_apis
2905 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906struct k_mutex {
2907 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002908 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002909 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002910 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002911 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002913 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914};
2915
Anas Nashifce78d162018-05-24 12:43:11 -05002916/**
2917 * @cond INTERNAL_HIDDEN
2918 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002919#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002920 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002921 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002922 .owner = NULL, \
2923 .lock_count = 0, \
2924 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002925 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926 }
2927
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002928#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2929
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002930/**
Allan Stephensc98da842016-11-11 15:45:03 -05002931 * INTERNAL_HIDDEN @endcond
2932 */
2933
2934/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002938 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002939 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002942 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002943 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002945 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002946 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002949 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002951 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * Upon completion, the mutex is available and does not have an owner.
2954 *
2955 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002956 *
2957 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002958 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002959 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002960__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002961
2962/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * This routine locks @a mutex. If the mutex is locked by another thread,
2966 * the calling thread waits until the mutex becomes available or until
2967 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * A thread is permitted to lock a mutex it has already locked. The operation
2970 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @param mutex Address of the mutex.
2973 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002974 * or one of the special values K_NO_WAIT and K_FOREVER.
2975 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002976 * @retval 0 Mutex locked.
2977 * @retval -EBUSY Returned without waiting.
2978 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002979 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002980 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002981__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982
2983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * This routine unlocks @a mutex. The mutex must already be locked by the
2987 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002988 *
2989 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002991 * thread.
2992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002994 *
2995 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002996 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002997 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002998__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999
Allan Stephensc98da842016-11-11 15:45:03 -05003000/**
Anas Nashif166f5192018-02-25 08:02:36 -06003001 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003002 */
3003
3004/**
3005 * @cond INTERNAL_HIDDEN
3006 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007
3008struct k_sem {
3009 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303010 u32_t count;
3011 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003012 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003013
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003014 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015};
3016
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003017#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003018 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003019 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003020 .count = initial_count, \
3021 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003022 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003023 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003024 }
3025
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003026#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003027
Allan Stephensc98da842016-11-11 15:45:03 -05003028/**
3029 * INTERNAL_HIDDEN @endcond
3030 */
3031
3032/**
3033 * @defgroup semaphore_apis Semaphore APIs
3034 * @ingroup kernel_apis
3035 * @{
3036 */
3037
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003041 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003043 * @param sem Address of the semaphore.
3044 * @param initial_count Initial semaphore count.
3045 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003046 *
3047 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003048 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003049 */
Andrew Boie99280232017-09-29 14:17:47 -07003050__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3051 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052
3053/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3059 *
3060 * @param sem Address of the semaphore.
3061 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003062 * or one of the special values K_NO_WAIT and K_FOREVER.
3063 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003064 * @note When porting code from the nanokernel legacy API to the new API, be
3065 * careful with the return value of this function. The return value is the
3066 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3067 * non-zero means failure, while the nano_sem_take family returns 1 for success
3068 * and 0 for failure.
3069 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003070 * @retval 0 Semaphore taken.
3071 * @retval -EBUSY Returned without waiting.
3072 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003073 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003074 */
Andrew Boie99280232017-09-29 14:17:47 -07003075__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003076
3077/**
3078 * @brief Give a semaphore.
3079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003080 * This routine gives @a sem, unless the semaphore is already at its maximum
3081 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003083 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003086 *
3087 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003088 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003089 */
Andrew Boie99280232017-09-29 14:17:47 -07003090__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003092/**
3093 * @brief Reset a semaphore's count to zero.
3094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003095 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003098 *
3099 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003100 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003101 */
Andrew Boie990bf162017-10-03 12:36:49 -07003102__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003103
Anas Nashif954d5502018-02-25 08:37:28 -06003104/**
3105 * @internal
3106 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003107static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108{
Patrik Flykt24d71432019-03-26 19:57:45 -06003109 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110}
3111
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003112/**
3113 * @brief Get a semaphore's count.
3114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003120 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003121 */
Andrew Boie990bf162017-10-03 12:36:49 -07003122__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003123
Anas Nashif954d5502018-02-25 08:37:28 -06003124/**
3125 * @internal
3126 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003127static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003128{
3129 return sem->count;
3130}
3131
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003136 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003137 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @param name Name of the semaphore.
3140 * @param initial_count Initial semaphore count.
3141 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003142 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003143 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003144#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003145 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003146 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303147 BUILD_ASSERT(((count_limit) != 0) && \
3148 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149
Anas Nashif166f5192018-02-25 08:02:36 -06003150/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003151
3152/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003153 * @defgroup msgq_apis Message Queue APIs
3154 * @ingroup kernel_apis
3155 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003156 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003158/**
3159 * @brief Message Queue Structure
3160 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003161struct k_msgq {
3162 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003163 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003164 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003165 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166 char *buffer_start;
3167 char *buffer_end;
3168 char *read_ptr;
3169 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003170 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003171
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003172 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003173 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003174};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003175/**
3176 * @cond INTERNAL_HIDDEN
3177 */
3178
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003180#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003181 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003182 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003183 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003184 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003186 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003187 .read_ptr = q_buffer, \
3188 .write_ptr = q_buffer, \
3189 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003190 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003192#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003193/**
3194 * INTERNAL_HIDDEN @endcond
3195 */
3196
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003197
Andrew Boie0fe789f2018-04-12 18:35:56 -07003198#define K_MSGQ_FLAG_ALLOC BIT(0)
3199
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003200/**
3201 * @brief Message Queue Attributes
3202 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303203struct k_msgq_attrs {
3204 size_t msg_size;
3205 u32_t max_msgs;
3206 u32_t used_msgs;
3207};
3208
Allan Stephensc98da842016-11-11 15:45:03 -05003209
3210/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003212 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3214 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003215 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3216 * message is similarly aligned to this boundary, @a q_msg_size must also be
3217 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003219 * The message queue can be accessed outside the module where it is defined
3220 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003221 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003222 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * @param q_name Name of the message queue.
3225 * @param q_msg_size Message size (in bytes).
3226 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003227 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003228 *
3229 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003230 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003231#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3232 static char __noinit __aligned(q_align) \
3233 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3234 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3235 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003236 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237
Peter Mitsisd7a37502016-10-13 11:37:40 -04003238/**
3239 * @brief Initialize a message queue.
3240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003241 * This routine initializes a message queue object, prior to its first use.
3242 *
Allan Stephensda827222016-11-09 14:23:58 -06003243 * The message queue's ring buffer must contain space for @a max_msgs messages,
3244 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3245 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3246 * that each message is similarly aligned to this boundary, @a q_msg_size
3247 * must also be a multiple of N.
3248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * @param q Address of the message queue.
3250 * @param buffer Pointer to ring buffer that holds queued messages.
3251 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003252 * @param max_msgs Maximum number of messages that can be queued.
3253 *
3254 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003255 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003256 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003257void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3258 u32_t max_msgs);
3259
3260/**
3261 * @brief Initialize a message queue.
3262 *
3263 * This routine initializes a message queue object, prior to its first use,
3264 * allocating its internal ring buffer from the calling thread's resource
3265 * pool.
3266 *
3267 * Memory allocated for the ring buffer can be released by calling
3268 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3269 * all of its references.
3270 *
3271 * @param q Address of the message queue.
3272 * @param msg_size Message size (in bytes).
3273 * @param max_msgs Maximum number of messages that can be queued.
3274 *
3275 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3276 * thread's resource pool, or -EINVAL if the size parameters cause
3277 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003278 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003279 */
3280__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3281 u32_t max_msgs);
3282
3283
3284void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003285
3286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003289 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003290 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003291 * @note Can be called by ISRs.
3292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @param q Address of the message queue.
3294 * @param data Pointer to the message.
3295 * @param timeout Waiting period to add the message (in milliseconds),
3296 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003297 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003298 * @retval 0 Message sent.
3299 * @retval -ENOMSG Returned without waiting or queue purged.
3300 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003301 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003302 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003303__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003304
3305/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003306 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * This routine receives a message from message queue @a q in a "first in,
3309 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003310 *
Allan Stephensc98da842016-11-11 15:45:03 -05003311 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * @param q Address of the message queue.
3314 * @param data Address of area to hold the received message.
3315 * @param timeout Waiting period to receive the message (in milliseconds),
3316 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003317 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003318 * @retval 0 Message received.
3319 * @retval -ENOMSG Returned without waiting.
3320 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003321 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003322 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003323__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003324
3325/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003326 * @brief Peek/read a message from a message queue.
3327 *
3328 * This routine reads a message from message queue @a q in a "first in,
3329 * first out" manner and leaves the message in the queue.
3330 *
3331 * @note Can be called by ISRs.
3332 *
3333 * @param q Address of the message queue.
3334 * @param data Address of area to hold the message read from the queue.
3335 *
3336 * @retval 0 Message read.
3337 * @retval -ENOMSG Returned when the queue has no message.
3338 * @req K-MSGQ-002
3339 */
3340__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3341
3342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * This routine discards all unreceived messages in a message queue's ring
3346 * buffer. Any threads that are blocked waiting to send a message to the
3347 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003350 *
3351 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003352 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003353 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003354__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003355
Peter Mitsis67be2492016-10-07 11:44:34 -04003356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * This routine returns the number of unused entries in a message queue's
3360 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @param q Address of the message queue.
3363 *
3364 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003365 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003366 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003367__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3368
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303369/**
3370 * @brief Get basic attributes of a message queue.
3371 *
3372 * This routine fetches basic attributes of message queue into attr argument.
3373 *
3374 * @param q Address of the message queue.
3375 * @param attrs pointer to message queue attribute structure.
3376 *
3377 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003378 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303379 */
3380__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3381
3382
Patrik Flykt4344e272019-03-08 14:19:05 -07003383static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003384{
3385 return q->max_msgs - q->used_msgs;
3386}
3387
Peter Mitsisd7a37502016-10-13 11:37:40 -04003388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * @param q Address of the message queue.
3394 *
3395 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003396 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003397 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003398__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3399
Patrik Flykt4344e272019-03-08 14:19:05 -07003400static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003401{
3402 return q->used_msgs;
3403}
3404
Anas Nashif166f5192018-02-25 08:02:36 -06003405/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003406
3407/**
3408 * @defgroup mem_pool_apis Memory Pool APIs
3409 * @ingroup kernel_apis
3410 * @{
3411 */
3412
Andy Ross73cb9582017-05-09 10:42:39 -07003413/* Note on sizing: the use of a 20 bit field for block means that,
3414 * assuming a reasonable minimum block size of 16 bytes, we're limited
3415 * to 16M of memory managed by a single pool. Long term it would be
3416 * good to move to a variable bit size based on configuration.
3417 */
3418struct k_mem_block_id {
3419 u32_t pool : 8;
3420 u32_t level : 4;
3421 u32_t block : 20;
3422};
3423
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003424struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003426 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003427};
3428
Anas Nashif166f5192018-02-25 08:02:36 -06003429/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003430
3431/**
3432 * @defgroup mailbox_apis Mailbox APIs
3433 * @ingroup kernel_apis
3434 * @{
3435 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003436
3437struct k_mbox_msg {
3438 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003439 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003440 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003441 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003443 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003444 /** sender's message data buffer */
3445 void *tx_data;
3446 /** internal use only - needed for legacy API support */
3447 void *_rx_data;
3448 /** message data block descriptor */
3449 struct k_mem_block tx_block;
3450 /** source thread id */
3451 k_tid_t rx_source_thread;
3452 /** target thread id */
3453 k_tid_t tx_target_thread;
3454 /** internal use only - thread waiting on send (may be a dummy) */
3455 k_tid_t _syncing_thread;
3456#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3457 /** internal use only - semaphore used during asynchronous send */
3458 struct k_sem *_async_sem;
3459#endif
3460};
3461
3462struct k_mbox {
3463 _wait_q_t tx_msg_queue;
3464 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003465 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003466
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003467 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003468};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003469/**
3470 * @cond INTERNAL_HIDDEN
3471 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003473#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003475 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3476 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003477 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003478 }
3479
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003480#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3481
Peter Mitsis12092702016-10-14 12:57:23 -04003482/**
Allan Stephensc98da842016-11-11 15:45:03 -05003483 * INTERNAL_HIDDEN @endcond
3484 */
3485
3486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003490 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003491 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003493 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003494 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003496#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003497 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003498 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499
Peter Mitsis12092702016-10-14 12:57:23 -04003500/**
3501 * @brief Initialize a mailbox.
3502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003503 * This routine initializes a mailbox object, prior to its first use.
3504 *
3505 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003506 *
3507 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003508 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510extern void k_mbox_init(struct k_mbox *mbox);
3511
Peter Mitsis12092702016-10-14 12:57:23 -04003512/**
3513 * @brief Send a mailbox message in a synchronous manner.
3514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * This routine sends a message to @a mbox and waits for a receiver to both
3516 * receive and process it. The message data may be in a buffer, in a memory
3517 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @param mbox Address of the mailbox.
3520 * @param tx_msg Address of the transmit message descriptor.
3521 * @param timeout Waiting period for the message to be received (in
3522 * milliseconds), or one of the special values K_NO_WAIT
3523 * and K_FOREVER. Once the message has been received,
3524 * this routine waits as long as necessary for the message
3525 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003526 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003527 * @retval 0 Message sent.
3528 * @retval -ENOMSG Returned without waiting.
3529 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003530 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003531 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003532extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003533 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003534
Peter Mitsis12092702016-10-14 12:57:23 -04003535/**
3536 * @brief Send a mailbox message in an asynchronous manner.
3537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 * This routine sends a message to @a mbox without waiting for a receiver
3539 * to process it. The message data may be in a buffer, in a memory pool block,
3540 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3541 * will be given when the message has been both received and completely
3542 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @param mbox Address of the mailbox.
3545 * @param tx_msg Address of the transmit message descriptor.
3546 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003547 *
3548 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003549 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003550 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003551extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003552 struct k_sem *sem);
3553
Peter Mitsis12092702016-10-14 12:57:23 -04003554/**
3555 * @brief Receive a mailbox message.
3556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * This routine receives a message from @a mbox, then optionally retrieves
3558 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 * @param mbox Address of the mailbox.
3561 * @param rx_msg Address of the receive message descriptor.
3562 * @param buffer Address of the buffer to receive data, or NULL to defer data
3563 * retrieval and message disposal until later.
3564 * @param timeout Waiting period for a message to be received (in
3565 * milliseconds), or one of the special values K_NO_WAIT
3566 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003567 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003568 * @retval 0 Message received.
3569 * @retval -ENOMSG Returned without waiting.
3570 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003571 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003572 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003573extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003574 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003575
3576/**
3577 * @brief Retrieve mailbox message data into a buffer.
3578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003579 * This routine completes the processing of a received message by retrieving
3580 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003581 *
3582 * Alternatively, this routine can be used to dispose of a received message
3583 * without retrieving its data.
3584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @param rx_msg Address of the receive message descriptor.
3586 * @param buffer Address of the buffer to receive data, or NULL to discard
3587 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003588 *
3589 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003590 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003591 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003592extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003593
3594/**
3595 * @brief Retrieve mailbox message data into a memory pool block.
3596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * This routine completes the processing of a received message by retrieving
3598 * its data into a memory pool block, then disposing of the message.
3599 * The memory pool block that results from successful retrieval must be
3600 * returned to the pool once the data has been processed, even in cases
3601 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003602 *
3603 * Alternatively, this routine can be used to dispose of a received message
3604 * without retrieving its data. In this case there is no need to return a
3605 * memory pool block to the pool.
3606 *
3607 * This routine allocates a new memory pool block for the data only if the
3608 * data is not already in one. If a new block cannot be allocated, the routine
3609 * returns a failure code and the received message is left unchanged. This
3610 * permits the caller to reattempt data retrieval at a later time or to dispose
3611 * of the received message without retrieving its data.
3612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * @param rx_msg Address of a receive message descriptor.
3614 * @param pool Address of memory pool, or NULL to discard data.
3615 * @param block Address of the area to hold memory pool block info.
3616 * @param timeout Waiting period to wait for a memory pool block (in
3617 * milliseconds), or one of the special values K_NO_WAIT
3618 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003619 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003620 * @retval 0 Data retrieved.
3621 * @retval -ENOMEM Returned without waiting.
3622 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003623 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003624 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003625extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003626 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003627 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003628
Anas Nashif166f5192018-02-25 08:02:36 -06003629/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003630
3631/**
Anas Nashifce78d162018-05-24 12:43:11 -05003632 * @defgroup pipe_apis Pipe APIs
3633 * @ingroup kernel_apis
3634 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003635 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003636
Anas Nashifce78d162018-05-24 12:43:11 -05003637/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003638struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003639 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3640 size_t size; /**< Buffer size */
3641 size_t bytes_used; /**< # bytes used in buffer */
3642 size_t read_index; /**< Where in buffer to read from */
3643 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003644 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003645
3646 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003647 _wait_q_t readers; /**< Reader wait queue */
3648 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003649 } wait_q;
3650
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003651 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003652 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003653};
3654
Anas Nashifce78d162018-05-24 12:43:11 -05003655/**
3656 * @cond INTERNAL_HIDDEN
3657 */
3658#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3659
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003660#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3661 { \
3662 .buffer = pipe_buffer, \
3663 .size = pipe_buffer_size, \
3664 .bytes_used = 0, \
3665 .read_index = 0, \
3666 .write_index = 0, \
3667 .lock = {}, \
3668 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003669 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3670 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003671 }, \
3672 _OBJECT_TRACING_INIT \
3673 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674 }
3675
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003676#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3677
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003678/**
Allan Stephensc98da842016-11-11 15:45:03 -05003679 * INTERNAL_HIDDEN @endcond
3680 */
3681
3682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003685 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003686 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003687 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003689 * @param name Name of the pipe.
3690 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3691 * or zero if no ring buffer is used.
3692 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003693 *
3694 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003695 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003696#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003697 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003698 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003699 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003700 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003701
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * This routine initializes a pipe object, prior to its first use.
3706 *
3707 * @param pipe Address of the pipe.
3708 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3709 * is used.
3710 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3711 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003712 *
3713 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003714 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003716void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3717
3718/**
3719 * @brief Release a pipe's allocated buffer
3720 *
3721 * If a pipe object was given a dynamically allocated buffer via
3722 * k_pipe_alloc_init(), this will free it. This function does nothing
3723 * if the buffer wasn't dynamically allocated.
3724 *
3725 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003726 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003727 */
3728void k_pipe_cleanup(struct k_pipe *pipe);
3729
3730/**
3731 * @brief Initialize a pipe and allocate a buffer for it
3732 *
3733 * Storage for the buffer region will be allocated from the calling thread's
3734 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3735 * or userspace is enabled and the pipe object loses all references to it.
3736 *
3737 * This function should only be called on uninitialized pipe objects.
3738 *
3739 * @param pipe Address of the pipe.
3740 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3741 * buffer is used.
3742 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003743 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003744 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003745 */
3746__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003747
3748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003749 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003751 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003753 * @param pipe Address of the pipe.
3754 * @param data Address of data to write.
3755 * @param bytes_to_write Size of data (in bytes).
3756 * @param bytes_written Address of area to hold the number of bytes written.
3757 * @param min_xfer Minimum number of bytes to write.
3758 * @param timeout Waiting period to wait for the data to be written (in
3759 * milliseconds), or one of the special values K_NO_WAIT
3760 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003761 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003762 * @retval 0 At least @a min_xfer bytes of data were written.
3763 * @retval -EIO Returned without waiting; zero data bytes were written.
3764 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003766 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003767 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003768__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3769 size_t bytes_to_write, size_t *bytes_written,
3770 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003771
3772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003773 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003775 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003777 * @param pipe Address of the pipe.
3778 * @param data Address to place the data read from pipe.
3779 * @param bytes_to_read Maximum number of data bytes to read.
3780 * @param bytes_read Address of area to hold the number of bytes read.
3781 * @param min_xfer Minimum number of data bytes to read.
3782 * @param timeout Waiting period to wait for the data to be read (in
3783 * milliseconds), or one of the special values K_NO_WAIT
3784 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003786 * @retval 0 At least @a min_xfer bytes of data were read.
3787 * @retval -EIO Returned without waiting; zero data bytes were read.
3788 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003790 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003791 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003792__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3793 size_t bytes_to_read, size_t *bytes_read,
3794 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003795
3796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003797 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003799 * This routine writes the data contained in a memory block to @a pipe.
3800 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003803 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804 * @param block Memory block containing data to send
3805 * @param size Number of data bytes in memory block to send
3806 * @param sem Semaphore to signal upon completion (else NULL)
3807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003808 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003809 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810 */
3811extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3812 size_t size, struct k_sem *sem);
3813
Anas Nashif166f5192018-02-25 08:02:36 -06003814/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815
Allan Stephensc98da842016-11-11 15:45:03 -05003816/**
3817 * @cond INTERNAL_HIDDEN
3818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003820struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003822 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003823 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824 char *buffer;
3825 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003826 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003828 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829};
3830
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003831#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003832 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003834 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003835 .num_blocks = slab_num_blocks, \
3836 .block_size = slab_block_size, \
3837 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838 .free_list = NULL, \
3839 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003840 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 }
3842
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003843#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3844
3845
Peter Mitsis578f9112016-10-07 13:50:31 -04003846/**
Allan Stephensc98da842016-11-11 15:45:03 -05003847 * INTERNAL_HIDDEN @endcond
3848 */
3849
3850/**
3851 * @defgroup mem_slab_apis Memory Slab APIs
3852 * @ingroup kernel_apis
3853 * @{
3854 */
3855
3856/**
Allan Stephensda827222016-11-09 14:23:58 -06003857 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003858 *
Allan Stephensda827222016-11-09 14:23:58 -06003859 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003861 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3862 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003863 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003864 *
Allan Stephensda827222016-11-09 14:23:58 -06003865 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003866 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003867 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003868 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003870 * @param name Name of the memory slab.
3871 * @param slab_block_size Size of each memory block (in bytes).
3872 * @param slab_num_blocks Number memory blocks.
3873 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003874 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003875 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003876#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3877 char __noinit __aligned(slab_align) \
3878 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003879 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003880 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003881 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003882
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003883/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003884 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003886 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003887 *
Allan Stephensda827222016-11-09 14:23:58 -06003888 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3889 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3890 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3891 * To ensure that each memory block is similarly aligned to this boundary,
3892 * @a slab_block_size must also be a multiple of N.
3893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003894 * @param slab Address of the memory slab.
3895 * @param buffer Pointer to buffer used for the memory blocks.
3896 * @param block_size Size of each memory block (in bytes).
3897 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003898 *
3899 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003900 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003901 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003902extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003903 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003904
3905/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003906 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003908 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003910 * @param slab Address of the memory slab.
3911 * @param mem Pointer to block address area.
3912 * @param timeout Maximum time to wait for operation to complete
3913 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3914 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003915 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003916 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003917 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003918 * @retval -ENOMEM Returned without waiting.
3919 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003920 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003921 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003922extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003923 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003924
3925/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003926 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003928 * This routine releases a previously allocated memory block back to its
3929 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * @param slab Address of the memory slab.
3932 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003933 *
3934 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003935 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003936 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003937extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003940 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003942 * This routine gets the number of memory blocks that are currently
3943 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003945 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003948 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003949 */
Kumar Galacc334c72017-04-21 10:55:34 -05003950static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003951{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003952 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003953}
3954
Peter Mitsisc001aa82016-10-13 13:53:37 -04003955/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003956 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003958 * This routine gets the number of memory blocks that are currently
3959 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003961 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003964 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003965 */
Kumar Galacc334c72017-04-21 10:55:34 -05003966static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003967{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003968 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003969}
3970
Anas Nashif166f5192018-02-25 08:02:36 -06003971/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003972
3973/**
3974 * @cond INTERNAL_HIDDEN
3975 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003977struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003978 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003979 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003980};
3981
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003982/**
Allan Stephensc98da842016-11-11 15:45:03 -05003983 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003984 */
3985
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003986/**
Allan Stephensc98da842016-11-11 15:45:03 -05003987 * @addtogroup mem_pool_apis
3988 * @{
3989 */
3990
3991/**
3992 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003994 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3995 * long. The memory pool allows blocks to be repeatedly partitioned into
3996 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003997 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003998 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003999 * If the pool is to be accessed outside the module where it is defined, it
4000 * can be declared via
4001 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004002 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004004 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004005 * @param minsz Size of the smallest blocks in the pool (in bytes).
4006 * @param maxsz Size of the largest blocks in the pool (in bytes).
4007 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004008 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004009 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004010 */
Andy Ross73cb9582017-05-09 10:42:39 -07004011#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4012 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4013 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004014 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004015 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004016 .base = { \
4017 .buf = _mpool_buf_##name, \
4018 .max_sz = maxsz, \
4019 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004020 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004021 .levels = _mpool_lvls_##name, \
4022 .flags = SYS_MEM_POOL_KERNEL \
4023 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004024 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004025
Peter Mitsis937042c2016-10-13 13:18:26 -04004026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004027 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004029 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004031 * @param pool Address of the memory pool.
4032 * @param block Pointer to block descriptor for the allocated memory.
4033 * @param size Amount of memory to allocate (in bytes).
4034 * @param timeout Maximum time to wait for operation to complete
4035 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4036 * or K_FOREVER to wait as long as necessary.
4037 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004038 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004039 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004040 * @retval -ENOMEM Returned without waiting.
4041 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004042 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004043 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004044extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004045 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004046
4047/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004048 * @brief Allocate memory from a memory pool with malloc() semantics
4049 *
4050 * Such memory must be released using k_free().
4051 *
4052 * @param pool Address of the memory pool.
4053 * @param size Amount of memory to allocate (in bytes).
4054 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004055 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004056 */
4057extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4058
4059/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004060 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004062 * This routine releases a previously allocated memory block back to its
4063 * memory pool.
4064 *
4065 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004066 *
4067 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004068 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004069 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004070extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004071
4072/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004073 * @brief Free memory allocated from a memory pool.
4074 *
4075 * This routine releases a previously allocated memory block back to its
4076 * memory pool
4077 *
4078 * @param id Memory block identifier.
4079 *
4080 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004081 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004082 */
4083extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4084
4085/**
Anas Nashif166f5192018-02-25 08:02:36 -06004086 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004087 */
4088
4089/**
4090 * @defgroup heap_apis Heap Memory Pool APIs
4091 * @ingroup kernel_apis
4092 * @{
4093 */
4094
4095/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004096 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004099 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004101 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004103 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004104 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004105 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004106extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004107
4108/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004109 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004110 *
4111 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004112 * returned must have been allocated from the heap memory pool or
4113 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004114 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004115 * If @a ptr is NULL, no operation is performed.
4116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004117 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004118 *
4119 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004120 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004121 */
4122extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004123
Allan Stephensc98da842016-11-11 15:45:03 -05004124/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004125 * @brief Allocate memory from heap, array style
4126 *
4127 * This routine provides traditional calloc() semantics. Memory is
4128 * allocated from the heap memory pool and zeroed.
4129 *
4130 * @param nmemb Number of elements in the requested array
4131 * @param size Size of each array element (in bytes).
4132 *
4133 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004134 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004135 */
4136extern void *k_calloc(size_t nmemb, size_t size);
4137
Anas Nashif166f5192018-02-25 08:02:36 -06004138/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004139
Benjamin Walshacc68c12017-01-29 18:57:45 -05004140/* polling API - PRIVATE */
4141
Benjamin Walshb0179862017-02-02 16:39:57 -05004142#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004143#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004144#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004145#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004146#endif
4147
Benjamin Walshacc68c12017-01-29 18:57:45 -05004148/* private - implementation data created as needed, per-type */
4149struct _poller {
4150 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004151 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004152};
4153
4154/* private - types bit positions */
4155enum _poll_types_bits {
4156 /* can be used to ignore an event */
4157 _POLL_TYPE_IGNORE,
4158
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004159 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004160 _POLL_TYPE_SIGNAL,
4161
4162 /* semaphore availability */
4163 _POLL_TYPE_SEM_AVAILABLE,
4164
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004165 /* queue/fifo/lifo data availability */
4166 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004167
4168 _POLL_NUM_TYPES
4169};
4170
Patrik Flykt4344e272019-03-08 14:19:05 -07004171#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004172
4173/* private - states bit positions */
4174enum _poll_states_bits {
4175 /* default state when creating event */
4176 _POLL_STATE_NOT_READY,
4177
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004178 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004179 _POLL_STATE_SIGNALED,
4180
4181 /* semaphore is available */
4182 _POLL_STATE_SEM_AVAILABLE,
4183
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004184 /* data is available to read on queue/fifo/lifo */
4185 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004186
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004187 /* queue/fifo/lifo wait was cancelled */
4188 _POLL_STATE_CANCELLED,
4189
Benjamin Walshacc68c12017-01-29 18:57:45 -05004190 _POLL_NUM_STATES
4191};
4192
Patrik Flykt4344e272019-03-08 14:19:05 -07004193#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004194
4195#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004196 (32 - (0 \
4197 + 8 /* tag */ \
4198 + _POLL_NUM_TYPES \
4199 + _POLL_NUM_STATES \
4200 + 1 /* modes */ \
4201 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004202
Benjamin Walshacc68c12017-01-29 18:57:45 -05004203/* end of polling API - PRIVATE */
4204
4205
4206/**
4207 * @defgroup poll_apis Async polling APIs
4208 * @ingroup kernel_apis
4209 * @{
4210 */
4211
4212/* Public polling API */
4213
4214/* public - values for k_poll_event.type bitfield */
4215#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004216#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4217#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4218#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004219#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004220
4221/* public - polling modes */
4222enum k_poll_modes {
4223 /* polling thread does not take ownership of objects when available */
4224 K_POLL_MODE_NOTIFY_ONLY = 0,
4225
4226 K_POLL_NUM_MODES
4227};
4228
4229/* public - values for k_poll_event.state bitfield */
4230#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004231#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4232#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4233#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004234#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004235#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004236
4237/* public - poll signal object */
4238struct k_poll_signal {
4239 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004240 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004241
4242 /*
4243 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4244 * user resets it to 0.
4245 */
4246 unsigned int signaled;
4247
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004248 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249 int result;
4250};
4251
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004252#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004253 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004254 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255 .signaled = 0, \
4256 .result = 0, \
4257 }
4258
4259struct k_poll_event {
4260 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004261 sys_dnode_t _node;
4262
4263 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004264 struct _poller *poller;
4265
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004266 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004267 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004268
Benjamin Walshacc68c12017-01-29 18:57:45 -05004269 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004270 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004271
4272 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004273 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004274
4275 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004276 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004277
4278 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004279 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004280
4281 /* per-type data */
4282 union {
4283 void *obj;
4284 struct k_poll_signal *signal;
4285 struct k_sem *sem;
4286 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004287 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004288 };
4289};
4290
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004291#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004292 { \
4293 .poller = NULL, \
4294 .type = event_type, \
4295 .state = K_POLL_STATE_NOT_READY, \
4296 .mode = event_mode, \
4297 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004298 { .obj = event_obj }, \
4299 }
4300
4301#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4302 event_tag) \
4303 { \
4304 .type = event_type, \
4305 .tag = event_tag, \
4306 .state = K_POLL_STATE_NOT_READY, \
4307 .mode = event_mode, \
4308 .unused = 0, \
4309 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004310 }
4311
4312/**
4313 * @brief Initialize one struct k_poll_event instance
4314 *
4315 * After this routine is called on a poll event, the event it ready to be
4316 * placed in an event array to be passed to k_poll().
4317 *
4318 * @param event The event to initialize.
4319 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4320 * values. Only values that apply to the same object being polled
4321 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4322 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004323 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004324 * @param obj Kernel object or poll signal.
4325 *
4326 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004327 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004328 */
4329
Kumar Galacc334c72017-04-21 10:55:34 -05004330extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004331 int mode, void *obj);
4332
4333/**
4334 * @brief Wait for one or many of multiple poll events to occur
4335 *
4336 * This routine allows a thread to wait concurrently for one or many of
4337 * multiple poll events to have occurred. Such events can be a kernel object
4338 * being available, like a semaphore, or a poll signal event.
4339 *
4340 * When an event notifies that a kernel object is available, the kernel object
4341 * is not "given" to the thread calling k_poll(): it merely signals the fact
4342 * that the object was available when the k_poll() call was in effect. Also,
4343 * all threads trying to acquire an object the regular way, i.e. by pending on
4344 * the object, have precedence over the thread polling on the object. This
4345 * means that the polling thread will never get the poll event on an object
4346 * until the object becomes available and its pend queue is empty. For this
4347 * reason, the k_poll() call is more effective when the objects being polled
4348 * only have one thread, the polling thread, trying to acquire them.
4349 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004350 * When k_poll() returns 0, the caller should loop on all the events that were
4351 * passed to k_poll() and check the state field for the values that were
4352 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004353 *
4354 * Before being reused for another call to k_poll(), the user has to reset the
4355 * state field to K_POLL_STATE_NOT_READY.
4356 *
Andrew Boie3772f772018-05-07 16:52:57 -07004357 * When called from user mode, a temporary memory allocation is required from
4358 * the caller's resource pool.
4359 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004360 * @param events An array of pointers to events to be polled for.
4361 * @param num_events The number of events in the array.
4362 * @param timeout Waiting period for an event to be ready (in milliseconds),
4363 * or one of the special values K_NO_WAIT and K_FOREVER.
4364 *
4365 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004366 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004367 * @retval -EINTR Polling has been interrupted, e.g. with
4368 * k_queue_cancel_wait(). All output events are still set and valid,
4369 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4370 * words, -EINTR status means that at least one of output events is
4371 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004372 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4373 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004374 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004375 */
4376
Andrew Boie3772f772018-05-07 16:52:57 -07004377__syscall int k_poll(struct k_poll_event *events, int num_events,
4378 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004379
4380/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004381 * @brief Initialize a poll signal object.
4382 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004383 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004384 *
4385 * @param signal A poll signal.
4386 *
4387 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004388 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004389 */
4390
Andrew Boie3772f772018-05-07 16:52:57 -07004391__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4392
4393/*
4394 * @brief Reset a poll signal object's state to unsignaled.
4395 *
4396 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004397 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004398 */
4399__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4400
Patrik Flykt4344e272019-03-08 14:19:05 -07004401static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004402{
Patrik Flykt24d71432019-03-26 19:57:45 -06004403 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004404}
4405
4406/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004407 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004408 *
4409 * @param signal A poll signal object
4410 * @param signaled An integer buffer which will be written nonzero if the
4411 * object was signaled
4412 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004413 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004414 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004415 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004416 */
4417__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4418 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004419
4420/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004421 * @brief Signal a poll signal object.
4422 *
4423 * This routine makes ready a poll signal, which is basically a poll event of
4424 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4425 * made ready to run. A @a result value can be specified.
4426 *
4427 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004428 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004429 * k_poll_signal_reset(). It thus has to be reset by the user before being
4430 * passed again to k_poll() or k_poll() will consider it being signaled, and
4431 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004432 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004433 * @note The result is stored and the 'signaled' field is set even if
4434 * this function returns an error indicating that an expiring poll was
4435 * not notified. The next k_poll() will detect the missed raise.
4436 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004437 * @param signal A poll signal.
4438 * @param result The value to store in the result field of the signal.
4439 *
4440 * @retval 0 The signal was delivered successfully.
4441 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004442 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004443 */
4444
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004445__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004446
Anas Nashif954d5502018-02-25 08:37:28 -06004447/**
4448 * @internal
4449 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004450extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004451
Anas Nashif166f5192018-02-25 08:02:36 -06004452/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004453
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004454/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004455 * @defgroup cpu_idle_apis CPU Idling APIs
4456 * @ingroup kernel_apis
4457 * @{
4458 */
4459
4460/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004461 * @brief Make the CPU idle.
4462 *
4463 * This function makes the CPU idle until an event wakes it up.
4464 *
4465 * In a regular system, the idle thread should be the only thread responsible
4466 * for making the CPU idle and triggering any type of power management.
4467 * However, in some more constrained systems, such as a single-threaded system,
4468 * the only thread would be responsible for this if needed.
4469 *
4470 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004471 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004472 */
4473extern void k_cpu_idle(void);
4474
4475/**
4476 * @brief Make the CPU idle in an atomic fashion.
4477 *
4478 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4479 * must be done atomically before making the CPU idle.
4480 *
4481 * @param key Interrupt locking key obtained from irq_lock().
4482 *
4483 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004484 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004485 */
4486extern void k_cpu_atomic_idle(unsigned int key);
4487
Anas Nashif30c3cff2019-01-22 08:18:13 -05004488/**
4489 * @}
4490 */
Anas Nashif954d5502018-02-25 08:37:28 -06004491
4492/**
4493 * @internal
4494 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004495extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004496
Patrik Flykt4344e272019-03-08 14:19:05 -07004497#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004498/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004499#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004500#else
4501
Andrew Boiecdb94d62017-04-18 15:22:05 -07004502/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004503 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004504 *
4505 * We won't have a real exception frame to determine the PC value when
4506 * the oops occurred, so print file and line number before we jump into
4507 * the fatal error handler.
4508 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004509#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004510 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Patrik Flykt4344e272019-03-08 14:19:05 -07004511 z_NanoFatalErrorHandler(reason, &_default_esf); \
Andy Ross92ce7672019-05-31 13:12:15 -07004512 k_thread_abort(k_current_get()); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004513 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004514
4515#endif /* _ARCH__EXCEPT */
4516
4517/**
4518 * @brief Fatally terminate a thread
4519 *
4520 * This should be called when a thread has encountered an unrecoverable
4521 * runtime condition and needs to terminate. What this ultimately
4522 * means is determined by the _fatal_error_handler() implementation, which
4523 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4524 *
4525 * If this is called from ISR context, the default system fatal error handler
4526 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004527 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004528 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004529#define k_oops() z_except_reason(_NANO_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004530
4531/**
4532 * @brief Fatally terminate the system
4533 *
4534 * This should be called when the Zephyr kernel has encountered an
4535 * unrecoverable runtime condition and needs to terminate. What this ultimately
4536 * means is determined by the _fatal_error_handler() implementation, which
4537 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004538 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004539 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004540#define k_panic() z_except_reason(_NANO_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004541
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004542/*
4543 * private APIs that are utilized by one or more public APIs
4544 */
4545
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004546#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004547/**
4548 * @internal
4549 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004550extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004551#else
Anas Nashif954d5502018-02-25 08:37:28 -06004552/**
4553 * @internal
4554 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004555#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004556#endif
4557
Anas Nashif954d5502018-02-25 08:37:28 -06004558/**
4559 * @internal
4560 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004561extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004562/**
4563 * @internal
4564 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004565extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004566
Andrew Boiedc5d9352017-06-02 12:56:47 -07004567/* arch/cpu.h may declare an architecture or platform-specific macro
4568 * for properly declaring stacks, compatible with MMU/MPU constraints if
4569 * enabled
4570 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004571
4572/**
4573 * @brief Obtain an extern reference to a stack
4574 *
4575 * This macro properly brings the symbol of a thread stack declared
4576 * elsewhere into scope.
4577 *
4578 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004579 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004580 */
4581#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4582
Patrik Flykt4344e272019-03-08 14:19:05 -07004583#ifdef Z_ARCH_THREAD_STACK_DEFINE
4584#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004585#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004586 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4587#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4588#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4589#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004590#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004591static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004592{
Patrik Flykt4344e272019-03-08 14:19:05 -07004593 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004594}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004595#else
4596/**
4597 * @brief Declare a toplevel thread stack memory region
4598 *
4599 * This declares a region of memory suitable for use as a thread's stack.
4600 *
4601 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4602 * 'noinit' section so that it isn't zeroed at boot
4603 *
Andrew Boie507852a2017-07-25 18:47:07 -07004604 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004605 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004606 * inside needs to be examined, examine thread->stack_info for the associated
4607 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004608 *
4609 * It is legal to precede this definition with the 'static' keyword.
4610 *
4611 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4612 * parameter of k_thread_create(), it may not be the same as the
4613 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4614 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004615 * Some arches may round the size of the usable stack region up to satisfy
4616 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4617 * size.
4618 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004619 * @param sym Thread stack symbol name
4620 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004621 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004622 */
4623#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004624 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004625
4626/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304627 * @brief Calculate size of stacks to be allocated in a stack array
4628 *
4629 * This macro calculates the size to be allocated for the stacks
4630 * inside a stack array. It accepts the indicated "size" as a parameter
4631 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4632 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4633 *
4634 * @param size Size of the stack memory region
4635 * @req K-TSTACK-001
4636 */
4637#define K_THREAD_STACK_LEN(size) (size)
4638
4639/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004640 * @brief Declare a toplevel array of thread stack memory regions
4641 *
4642 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4643 * definition for additional details and constraints.
4644 *
4645 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4646 * 'noinit' section so that it isn't zeroed at boot
4647 *
4648 * @param sym Thread stack symbol name
4649 * @param nmemb Number of stacks to declare
4650 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004651 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004652 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004653#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004654 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304655 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004656
4657/**
4658 * @brief Declare an embedded stack memory region
4659 *
4660 * Used for stacks embedded within other data structures. Use is highly
4661 * discouraged but in some cases necessary. For memory protection scenarios,
4662 * it is very important that any RAM preceding this member not be writable
4663 * by threads else a stack overflow will lead to silent corruption. In other
4664 * words, the containing data structure should live in RAM owned by the kernel.
4665 *
4666 * @param sym Thread stack symbol name
4667 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004668 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004669 */
4670#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004671 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004672
4673/**
4674 * @brief Return the size in bytes of a stack memory region
4675 *
4676 * Convenience macro for passing the desired stack size to k_thread_create()
4677 * since the underlying implementation may actually create something larger
4678 * (for instance a guard area).
4679 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004680 * The value returned here is not guaranteed to match the 'size' parameter
4681 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004682 *
4683 * @param sym Stack memory symbol
4684 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004685 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004686 */
4687#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4688
Andrew Boie575abc02019-03-19 10:42:24 -07004689
4690/**
4691 * @brief Indicate how much additional memory is reserved for stack objects
4692 *
4693 * Any given stack declaration may have additional memory in it for guard
4694 * areas or supervisor mode stacks. This macro indicates how much space
4695 * is reserved for this. The memory reserved may not be contiguous within
4696 * the stack object, and does not account for additional space used due to
4697 * enforce alignment.
4698 */
4699#define K_THREAD_STACK_RESERVED 0
4700
Andrew Boiedc5d9352017-06-02 12:56:47 -07004701/**
4702 * @brief Get a pointer to the physical stack buffer
4703 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004704 * This macro is deprecated. If a stack buffer needs to be examined, the
4705 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004706 *
4707 * @param sym Declared stack symbol name
4708 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004709 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004710 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004711static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004712{
4713 return (char *)sym;
4714}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004715
4716#endif /* _ARCH_DECLARE_STACK */
4717
Chunlin Hane9c97022017-07-07 20:29:30 +08004718/**
4719 * @defgroup mem_domain_apis Memory domain APIs
4720 * @ingroup kernel_apis
4721 * @{
4722 */
4723
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004724/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004725 * @def K_MEM_PARTITION_DEFINE
4726 * @brief Used to declare a memory partition
4727 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004728 */
4729#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4730#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4731 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004732 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004733 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004734#else
4735#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004736 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004737 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004738#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4739
Chunlin Hane9c97022017-07-07 20:29:30 +08004740/* memory partition */
4741struct k_mem_partition {
4742 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004743 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004744 /* size of memory partition */
4745 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004746#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004747 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304748 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004749#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004750};
4751
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004752/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304753 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004754struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304755#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004756 /* partitions in the domain */
4757 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304758#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004759 /* domain q */
4760 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004761 /* number of partitions in the domain */
4762 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004763};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304764
Chunlin Hane9c97022017-07-07 20:29:30 +08004765
4766/**
4767 * @brief Initialize a memory domain.
4768 *
4769 * Initialize a memory domain with given name and memory partitions.
4770 *
4771 * @param domain The memory domain to be initialized.
4772 * @param num_parts The number of array items of "parts" parameter.
4773 * @param parts An array of pointers to the memory partitions. Can be NULL
4774 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004775 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004776 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004777extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004778 struct k_mem_partition *parts[]);
4779/**
4780 * @brief Destroy a memory domain.
4781 *
4782 * Destroy a memory domain.
4783 *
4784 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004785 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004786 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004787extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4788
4789/**
4790 * @brief Add a memory partition into a memory domain.
4791 *
4792 * Add a memory partition into a memory domain.
4793 *
4794 * @param domain The memory domain to be added a memory partition.
4795 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004796 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004797 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004798extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4799 struct k_mem_partition *part);
4800
4801/**
4802 * @brief Remove a memory partition from a memory domain.
4803 *
4804 * Remove a memory partition from a memory domain.
4805 *
4806 * @param domain The memory domain to be removed a memory partition.
4807 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004808 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004809 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004810extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4811 struct k_mem_partition *part);
4812
4813/**
4814 * @brief Add a thread into a memory domain.
4815 *
4816 * Add a thread into a memory domain.
4817 *
4818 * @param domain The memory domain that the thread is going to be added into.
4819 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004820 *
4821 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004822 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004823extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4824 k_tid_t thread);
4825
4826/**
4827 * @brief Remove a thread from its memory domain.
4828 *
4829 * Remove a thread from its memory domain.
4830 *
4831 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004832 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004833 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004834extern void k_mem_domain_remove_thread(k_tid_t thread);
4835
Anas Nashif166f5192018-02-25 08:02:36 -06004836/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004837
Andrew Boie756f9072017-10-10 16:01:49 -07004838/**
4839 * @brief Emit a character buffer to the console device
4840 *
4841 * @param c String of characters to print
4842 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004843 *
4844 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004845 */
4846__syscall void k_str_out(char *c, size_t n);
4847
Andy Rosse7172672018-01-24 15:48:32 -08004848/**
4849 * @brief Start a numbered CPU on a MP-capable system
4850
4851 * This starts and initializes a specific CPU. The main thread on
4852 * startup is running on CPU zero, other processors are numbered
4853 * sequentially. On return from this function, the CPU is known to
4854 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004855 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004856 * with the provided key will work to enable them.
4857 *
4858 * Normally, in SMP mode this function will be called by the kernel
4859 * initialization and should not be used as a user API. But it is
4860 * defined here for special-purpose apps which want Zephyr running on
4861 * one core and to use others for design-specific processing.
4862 *
4863 * @param cpu_num Integer number of the CPU
4864 * @param stack Stack memory for the CPU
4865 * @param sz Stack buffer size, in bytes
4866 * @param fn Function to begin running on the CPU. First argument is
4867 * an irq_unlock() key.
4868 * @param arg Untyped argument to be passed to "fn"
4869 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004870extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004871 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004872
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004873#ifdef __cplusplus
4874}
4875#endif
4876
Anas Nashifb6304e62018-07-04 08:03:03 -05004877#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004878#include <syscalls/kernel.h>
4879
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004880#endif /* !_ASMLANGUAGE */
4881
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004882#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */