blob: bb40aa0291ac7328da0692858c9eb891edede4a5 [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 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001629 * @note
1630 * @rststar
1631 * While this function returns time in milliseconds, it does
1632 * not mean it has millisecond resolution. The actual resolution depends on
1633 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the
1634 * default setting of 100, system time is updated in increments of 10ms.
1635 * @endrststar
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001636 *
1637 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001638 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001639__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001641/**
1642 * @brief Enable clock always on in tickless kernel
1643 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001644 * This routine enables keeping the clock running (that is, it always
1645 * keeps an active timer interrupt scheduled) when there are no timer
1646 * events programmed in tickless kernel scheduling. This is necessary
1647 * if the clock is used to track passage of time (e.g. via
1648 * k_uptime_get_32()), otherwise the internal hardware counter may
1649 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001650 *
1651 * @retval prev_status Previous status of always on flag
1652 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001653int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001654
1655/**
1656 * @brief Disable clock always on in tickless kernel
1657 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001658 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001659 * there are no timer events programmed in tickless kernel
1660 * scheduling. To save power, this routine should be called
1661 * immediately when clock is not used to track time.
1662 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001663void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001664
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001665/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001666 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * This routine returns the lower 32-bits of the elapsed time since the system
1669 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001671 * This routine can be more efficient than k_uptime_get(), as it reduces the
1672 * need for interrupt locking and 64-bit math. However, the 32-bit result
1673 * cannot hold a system uptime time larger than approximately 50 days, so the
1674 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001675 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001676 * @note
1677 * @rststar
1678 * While this function returns time in milliseconds, it does
1679 * not mean it has millisecond resolution. The actual resolution depends on
1680 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the
1681 * default setting of 100, system time is updated in increments of 10ms.
1682 * @endrststar
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001683 *
1684 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001685 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001686__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001687
1688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * This routine computes the elapsed time between the current system uptime
1692 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @param reftime Pointer to a reference time, which is updated to the current
1695 * uptime upon return.
1696 *
1697 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001698 */
Andy Ross987c0e52018-09-27 16:50:00 -07001699static inline s64_t k_uptime_delta(s64_t *reftime)
1700{
1701 s64_t uptime, delta;
1702
1703 uptime = k_uptime_get();
1704 delta = uptime - *reftime;
1705 *reftime = uptime;
1706
1707 return delta;
1708}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001709
1710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001711 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * This routine computes the elapsed time between the current system uptime
1714 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1717 * need for interrupt locking and 64-bit math. However, the 32-bit result
1718 * cannot hold an elapsed time larger than approximately 50 days, so the
1719 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @param reftime Pointer to a reference time, which is updated to the current
1722 * uptime upon return.
1723 *
1724 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001725 */
Andy Ross987c0e52018-09-27 16:50:00 -07001726static inline u32_t k_uptime_delta_32(s64_t *reftime)
1727{
1728 return (u32_t)k_uptime_delta(reftime);
1729}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001730
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * This routine returns the current time, as measured by the system's hardware
1735 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001739#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001740
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001741/**
Anas Nashif166f5192018-02-25 08:02:36 -06001742 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743 */
1744
Allan Stephensc98da842016-11-11 15:45:03 -05001745/**
1746 * @cond INTERNAL_HIDDEN
1747 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001749struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001750 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001751 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001752 union {
1753 _wait_q_t wait_q;
1754
1755 _POLL_EVENT;
1756 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001757
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001758 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001759};
1760
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001761#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001762 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001763 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001764 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001765 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001766 _OBJECT_TRACING_INIT \
1767 }
1768
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001769#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1770
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001771extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1772
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001773/**
1774 * INTERNAL_HIDDEN @endcond
1775 */
1776
1777/**
1778 * @defgroup queue_apis Queue APIs
1779 * @ingroup kernel_apis
1780 * @{
1781 */
1782
1783/**
1784 * @brief Initialize a queue.
1785 *
1786 * This routine initializes a queue object, prior to its first use.
1787 *
1788 * @param queue Address of the queue.
1789 *
1790 * @return N/A
1791 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001792__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001793
1794/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001795 * @brief Cancel waiting on a queue.
1796 *
1797 * This routine causes first thread pending on @a queue, if any, to
1798 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001799 * If the queue is being waited on by k_poll(), it will return with
1800 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1801 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001802 *
1803 * @note Can be called by ISRs.
1804 *
1805 * @param queue Address of the queue.
1806 *
1807 * @return N/A
1808 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001809__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001810
1811/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001812 * @brief Append an element to the end of a queue.
1813 *
1814 * This routine appends a data item to @a queue. A queue data item must be
1815 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1816 * reserved for the kernel's use.
1817 *
1818 * @note Can be called by ISRs.
1819 *
1820 * @param queue Address of the queue.
1821 * @param data Address of the data item.
1822 *
1823 * @return N/A
1824 */
1825extern void k_queue_append(struct k_queue *queue, void *data);
1826
1827/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001828 * @brief Append an element to a queue.
1829 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001830 * This routine appends a data item to @a queue. There is an implicit memory
1831 * allocation to create an additional temporary bookkeeping data structure from
1832 * the calling thread's resource pool, which is automatically freed when the
1833 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001834 *
1835 * @note Can be called by ISRs.
1836 *
1837 * @param queue Address of the queue.
1838 * @param data Address of the data item.
1839 *
1840 * @retval 0 on success
1841 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1842 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301843__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001844
1845/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001846 * @brief Prepend an element to a queue.
1847 *
1848 * This routine prepends a data item to @a queue. A queue data item must be
1849 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1850 * reserved for the kernel's use.
1851 *
1852 * @note Can be called by ISRs.
1853 *
1854 * @param queue Address of the queue.
1855 * @param data Address of the data item.
1856 *
1857 * @return N/A
1858 */
1859extern void k_queue_prepend(struct k_queue *queue, void *data);
1860
1861/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001862 * @brief Prepend an element to a queue.
1863 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001864 * This routine prepends a data item to @a queue. There is an implicit memory
1865 * allocation to create an additional temporary bookkeeping data structure from
1866 * the calling thread's resource pool, which is automatically freed when the
1867 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001868 *
1869 * @note Can be called by ISRs.
1870 *
1871 * @param queue Address of the queue.
1872 * @param data Address of the data item.
1873 *
1874 * @retval 0 on success
1875 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1876 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301877__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001878
1879/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001880 * @brief Inserts an element to a queue.
1881 *
1882 * This routine inserts a data item to @a queue after previous item. A queue
1883 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1884 * item are reserved for the kernel's use.
1885 *
1886 * @note Can be called by ISRs.
1887 *
1888 * @param queue Address of the queue.
1889 * @param prev Address of the previous data item.
1890 * @param data Address of the data item.
1891 *
1892 * @return N/A
1893 */
1894extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1895
1896/**
1897 * @brief Atomically append a list of elements to a queue.
1898 *
1899 * This routine adds a list of data items to @a queue in one operation.
1900 * The data items must be in a singly-linked list, with the first 32 bits
1901 * in each data item pointing to the next data item; the list must be
1902 * NULL-terminated.
1903 *
1904 * @note Can be called by ISRs.
1905 *
1906 * @param queue Address of the queue.
1907 * @param head Pointer to first node in singly-linked list.
1908 * @param tail Pointer to last node in singly-linked list.
1909 *
1910 * @return N/A
1911 */
1912extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1913
1914/**
1915 * @brief Atomically add a list of elements to a queue.
1916 *
1917 * This routine adds a list of data items to @a queue in one operation.
1918 * The data items must be in a singly-linked list implemented using a
1919 * sys_slist_t object. Upon completion, the original list is empty.
1920 *
1921 * @note Can be called by ISRs.
1922 *
1923 * @param queue Address of the queue.
1924 * @param list Pointer to sys_slist_t object.
1925 *
1926 * @return N/A
1927 */
1928extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1929
1930/**
1931 * @brief Get an element from a queue.
1932 *
1933 * This routine removes first data item from @a queue. The first 32 bits of the
1934 * data item are reserved for the kernel's use.
1935 *
1936 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1937 *
1938 * @param queue Address of the queue.
1939 * @param timeout Waiting period to obtain a data item (in milliseconds),
1940 * or one of the special values K_NO_WAIT and K_FOREVER.
1941 *
1942 * @return Address of the data item if successful; NULL if returned
1943 * without waiting, or waiting period timed out.
1944 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001945__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001946
1947/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001948 * @brief Remove an element from a queue.
1949 *
1950 * This routine removes data item from @a queue. The first 32 bits of the
1951 * data item are reserved for the kernel's use. Removing elements from k_queue
1952 * rely on sys_slist_find_and_remove which is not a constant time operation.
1953 *
1954 * @note Can be called by ISRs
1955 *
1956 * @param queue Address of the queue.
1957 * @param data Address of the data item.
1958 *
1959 * @return true if data item was removed
1960 */
1961static inline bool k_queue_remove(struct k_queue *queue, void *data)
1962{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001963 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001964}
1965
1966/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001967 * @brief Append an element to a queue only if it's not present already.
1968 *
1969 * This routine appends data item to @a queue. The first 32 bits of the
1970 * data item are reserved for the kernel's use. Appending elements to k_queue
1971 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1972 *
1973 * @note Can be called by ISRs
1974 *
1975 * @param queue Address of the queue.
1976 * @param data Address of the data item.
1977 *
1978 * @return true if data item was added, false if not
1979 */
1980static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1981{
1982 sys_sfnode_t *test;
1983
1984 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1985 if (test == (sys_sfnode_t *) data) {
1986 return false;
1987 }
1988 }
1989
1990 k_queue_append(queue, data);
1991 return true;
1992}
1993
1994/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001995 * @brief Query a queue to see if it has data available.
1996 *
1997 * Note that the data might be already gone by the time this function returns
1998 * if other threads are also trying to read from the queue.
1999 *
2000 * @note Can be called by ISRs.
2001 *
2002 * @param queue Address of the queue.
2003 *
2004 * @return Non-zero if the queue is empty.
2005 * @return 0 if data is available.
2006 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002007__syscall int k_queue_is_empty(struct k_queue *queue);
2008
Patrik Flykt4344e272019-03-08 14:19:05 -07002009static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002010{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002011 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002012}
2013
2014/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002015 * @brief Peek element at the head of queue.
2016 *
2017 * Return element from the head of queue without removing it.
2018 *
2019 * @param queue Address of the queue.
2020 *
2021 * @return Head element, or NULL if queue is empty.
2022 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002023__syscall void *k_queue_peek_head(struct k_queue *queue);
2024
Patrik Flykt4344e272019-03-08 14:19:05 -07002025static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002026{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002027 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002028}
2029
2030/**
2031 * @brief Peek element at the tail of queue.
2032 *
2033 * Return element from the tail of queue without removing it.
2034 *
2035 * @param queue Address of the queue.
2036 *
2037 * @return Tail element, or NULL if queue is empty.
2038 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002039__syscall void *k_queue_peek_tail(struct k_queue *queue);
2040
Patrik Flykt4344e272019-03-08 14:19:05 -07002041static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002042{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002043 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002044}
2045
2046/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002047 * @brief Statically define and initialize a queue.
2048 *
2049 * The queue can be accessed outside the module where it is defined using:
2050 *
2051 * @code extern struct k_queue <name>; @endcode
2052 *
2053 * @param name Name of the queue.
2054 */
2055#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002056 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002057 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002058
Anas Nashif166f5192018-02-25 08:02:36 -06002059/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002060
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002062 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063};
2064
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002065/**
2066 * @cond INTERNAL_HIDDEN
2067 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002068#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002069 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002070 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002071 }
2072
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002073#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002074
Allan Stephensc98da842016-11-11 15:45:03 -05002075/**
2076 * INTERNAL_HIDDEN @endcond
2077 */
2078
2079/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002080 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002081 * @ingroup kernel_apis
2082 * @{
2083 */
2084
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002088 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002090 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 *
2092 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002093 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002095#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002096 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097
2098/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002099 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002100 *
2101 * This routine causes first thread pending on @a fifo, if any, to
2102 * return from k_fifo_get() call with NULL value (as if timeout
2103 * expired).
2104 *
2105 * @note Can be called by ISRs.
2106 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002107 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002108 *
2109 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002110 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002111 */
2112#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002113 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002114
2115/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002116 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002118 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2120 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002122 * @note Can be called by ISRs.
2123 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002124 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002125 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 *
2127 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002128 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002129 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002130#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002131 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002132
2133/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002134 * @brief Add an element to a FIFO queue.
2135 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002136 * This routine adds a data item to @a fifo. There is an implicit memory
2137 * allocation to create an additional temporary bookkeeping data structure from
2138 * the calling thread's resource pool, which is automatically freed when the
2139 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002140 *
2141 * @note Can be called by ISRs.
2142 *
2143 * @param fifo Address of the FIFO.
2144 * @param data Address of the data item.
2145 *
2146 * @retval 0 on success
2147 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002148 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002149 */
2150#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002151 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002152
2153/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002154 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002156 * This routine adds a list of data items to @a fifo in one operation.
2157 * The data items must be in a singly-linked list, with the first 32 bits
2158 * each data item pointing to the next data item; the list must be
2159 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002161 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002164 * @param head Pointer to first node in singly-linked list.
2165 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 *
2167 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002168 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002170#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002171 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002172
2173/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002174 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002176 * This routine adds a list of data items to @a fifo in one operation.
2177 * The data items must be in a singly-linked list implemented using a
2178 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 * and must be re-initialized via sys_slist_init().
2180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002181 * @note Can be called by ISRs.
2182 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002183 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
2186 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002187 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002189#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002190 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191
2192/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002193 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * This routine removes a data item from @a fifo in a "first in, first out"
2196 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002198 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2199 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002200 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002201 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002202 * or one of the special values K_NO_WAIT and K_FOREVER.
2203 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002204 * @return Address of the data item if successful; NULL if returned
2205 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002206 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002207 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002208#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002209 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002213 *
2214 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002216 *
2217 * @note Can be called by ISRs.
2218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002220 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002221 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002222 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002223 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002224 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002225#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002226 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002227
2228/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002229 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002230 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302232 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002233 * on each iteration of processing, a head container will be peeked,
2234 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002236 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002237 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002238 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002239 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002240 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002241 */
2242#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002243 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002244
2245/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002247 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * Return element from the tail of FIFO queue (without removing it). A usecase
2249 * for this is if elements of the FIFO queue are themselves containers. Then
2250 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002251 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002252 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002253 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002254 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002255 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002256 */
2257#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002258 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002259
2260/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002263 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002265 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002267 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002268 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002271 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002272 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273
Anas Nashif166f5192018-02-25 08:02:36 -06002274/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002275
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002276struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002277 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278};
2279
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002280/**
2281 * @cond INTERNAL_HIDDEN
2282 */
2283
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002284#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002285 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002286 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002287 }
2288
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002289#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2290
Allan Stephensc98da842016-11-11 15:45:03 -05002291/**
2292 * INTERNAL_HIDDEN @endcond
2293 */
2294
2295/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002296 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002297 * @ingroup kernel_apis
2298 * @{
2299 */
2300
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002302 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002304 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002306 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002307 *
2308 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002309 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002311#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002312 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313
2314/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002315 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002316 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002317 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2319 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002321 * @note Can be called by ISRs.
2322 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002323 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002324 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325 *
2326 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002327 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002328 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002329#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002330 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331
2332/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002333 * @brief Add an element to a LIFO queue.
2334 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002335 * This routine adds a data item to @a lifo. There is an implicit memory
2336 * allocation to create an additional temporary bookkeeping data structure from
2337 * the calling thread's resource pool, which is automatically freed when the
2338 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002339 *
2340 * @note Can be called by ISRs.
2341 *
2342 * @param lifo Address of the LIFO.
2343 * @param data Address of the data item.
2344 *
2345 * @retval 0 on success
2346 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002347 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002348 */
2349#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002350 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002351
2352/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002353 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * This routine removes a data item from @a lifo in a "last in, first out"
2356 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2359 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002360 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 * or one of the special values K_NO_WAIT and K_FOREVER.
2363 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002364 * @return Address of the data item if successful; NULL if returned
2365 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002366 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002367 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002368#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002369 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002371/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002372 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002374 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002375 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002376 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002378 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002379 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002380 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002382 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002383 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384
Anas Nashif166f5192018-02-25 08:02:36 -06002385/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002386
2387/**
2388 * @cond INTERNAL_HIDDEN
2389 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302390#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391
2392struct k_stack {
2393 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002394 struct k_spinlock lock;
Kumar Galacc334c72017-04-21 10:55:34 -05002395 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002397 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002398 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399};
2400
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002401#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002402 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002403 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002404 .base = stack_buffer, \
2405 .next = stack_buffer, \
2406 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002407 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002408 }
2409
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002410#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2411
Allan Stephensc98da842016-11-11 15:45:03 -05002412/**
2413 * INTERNAL_HIDDEN @endcond
2414 */
2415
2416/**
2417 * @defgroup stack_apis Stack APIs
2418 * @ingroup kernel_apis
2419 * @{
2420 */
2421
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422/**
2423 * @brief Initialize a stack.
2424 *
2425 * This routine initializes a stack object, prior to its first use.
2426 *
2427 * @param stack Address of the stack.
2428 * @param buffer Address of array used to hold stacked values.
2429 * @param num_entries Maximum number of values that can be stacked.
2430 *
2431 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002432 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 */
Andrew Boief3bee952018-05-02 17:44:39 -07002434void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302435 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002436
2437
2438/**
2439 * @brief Initialize a stack.
2440 *
2441 * This routine initializes a stack object, prior to its first use. Internal
2442 * buffers will be allocated from the calling thread's resource pool.
2443 * This memory will be released if k_stack_cleanup() is called, or
2444 * userspace is enabled and the stack object loses all references to it.
2445 *
2446 * @param stack Address of the stack.
2447 * @param num_entries Maximum number of values that can be stacked.
2448 *
2449 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002450 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002451 */
2452
Adithya Baglody28080d32018-10-15 11:48:51 +05302453__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2454 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002455
2456/**
2457 * @brief Release a stack's allocated buffer
2458 *
2459 * If a stack object was given a dynamically allocated buffer via
2460 * k_stack_alloc_init(), this will free it. This function does nothing
2461 * if the buffer wasn't dynamically allocated.
2462 *
2463 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002464 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002465 */
2466void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467
2468/**
2469 * @brief Push an element onto a stack.
2470 *
2471 * This routine adds a 32-bit value @a data to @a stack.
2472 *
2473 * @note Can be called by ISRs.
2474 *
2475 * @param stack Address of the stack.
2476 * @param data Value to push onto the stack.
2477 *
2478 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002479 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 */
Andrew Boiee8734462017-09-29 16:42:07 -07002481__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482
2483/**
2484 * @brief Pop an element from a stack.
2485 *
2486 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2487 * manner and stores the value in @a data.
2488 *
2489 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2490 *
2491 * @param stack Address of the stack.
2492 * @param data Address of area to hold the value popped from the stack.
2493 * @param timeout Waiting period to obtain a value (in milliseconds),
2494 * or one of the special values K_NO_WAIT and K_FOREVER.
2495 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002496 * @retval 0 Element popped from stack.
2497 * @retval -EBUSY Returned without waiting.
2498 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002499 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 */
Andrew Boiee8734462017-09-29 16:42:07 -07002501__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002508 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @param name Name of the stack.
2511 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002512 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002514#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002515 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002516 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002517 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002518 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002519 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520
Anas Nashif166f5192018-02-25 08:02:36 -06002521/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002522
Allan Stephens6bba9b02016-11-16 14:56:54 -05002523struct k_work;
2524
Allan Stephensc98da842016-11-11 15:45:03 -05002525/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002526 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002527 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528 */
2529
Allan Stephens6bba9b02016-11-16 14:56:54 -05002530/**
2531 * @typedef k_work_handler_t
2532 * @brief Work item handler function type.
2533 *
2534 * A work item's handler function is executed by a workqueue's thread
2535 * when the work item is processed by the workqueue.
2536 *
2537 * @param work Address of the work item.
2538 *
2539 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002540 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002541 */
2542typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543
2544/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002545 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002547
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002549 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002550 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551};
2552
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002554 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555};
2556
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002558 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559 k_work_handler_t handler;
2560 atomic_t flags[1];
2561};
2562
Allan Stephens6bba9b02016-11-16 14:56:54 -05002563struct k_delayed_work {
2564 struct k_work work;
2565 struct _timeout timeout;
2566 struct k_work_q *work_q;
2567};
2568
2569extern struct k_work_q k_sys_work_q;
2570
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002571/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002572 * INTERNAL_HIDDEN @endcond
2573 */
2574
Patrik Flykt4344e272019-03-08 14:19:05 -07002575#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002576 { \
2577 ._reserved = NULL, \
2578 .handler = work_handler, \
2579 .flags = { 0 } \
2580 }
2581
Patrik Flykt4344e272019-03-08 14:19:05 -07002582#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002583
Allan Stephens6bba9b02016-11-16 14:56:54 -05002584/**
2585 * @brief Initialize a statically-defined work item.
2586 *
2587 * This macro can be used to initialize a statically-defined workqueue work
2588 * item, prior to its first use. For example,
2589 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002590 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002591 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002592 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002593 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002594 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002596#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002597 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598
2599/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002600 * @brief Initialize a work item.
2601 *
2602 * This routine initializes a workqueue work item, prior to its first use.
2603 *
2604 * @param work Address of work item.
2605 * @param handler Function to invoke each time work item is processed.
2606 *
2607 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002608 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609 */
2610static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2611{
Patrik Flykt4344e272019-03-08 14:19:05 -07002612 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613}
2614
2615/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002616 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002617 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002618 * This routine submits work item @a work to be processed by workqueue
2619 * @a work_q. If the work item is already pending in the workqueue's queue
2620 * as a result of an earlier submission, this routine has no effect on the
2621 * work item. If the work item has already been processed, or is currently
2622 * being processed, its work is considered complete and the work item can be
2623 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002624 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * @warning
2626 * A submitted work item must not be modified until it has been processed
2627 * by the workqueue.
2628 *
2629 * @note Can be called by ISRs.
2630 *
2631 * @param work_q Address of workqueue.
2632 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002633 *
2634 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002635 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636 */
2637static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2638 struct k_work *work)
2639{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002640 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002641 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642 }
2643}
2644
2645/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002646 * @brief Submit a work item to a user mode workqueue
2647 *
David B. Kinder06d78352018-12-17 14:32:40 -08002648 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002649 * memory allocation is made from the caller's resource pool which is freed
2650 * once the worker thread consumes the k_work item. The workqueue
2651 * thread must have memory access to the k_work item being submitted. The caller
2652 * must have permission granted on the work_q parameter's queue object.
2653 *
2654 * Otherwise this works the same as k_work_submit_to_queue().
2655 *
2656 * @note Can be called by ISRs.
2657 *
2658 * @param work_q Address of workqueue.
2659 * @param work Address of work item.
2660 *
2661 * @retval -EBUSY if the work item was already in some workqueue
2662 * @retval -ENOMEM if no memory for thread resource pool allocation
2663 * @retval 0 Success
2664 * @req K-WORK-001
2665 */
2666static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2667 struct k_work *work)
2668{
2669 int ret = -EBUSY;
2670
2671 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2672 ret = k_queue_alloc_append(&work_q->queue, work);
2673
2674 /* Couldn't insert into the queue. Clear the pending bit
2675 * so the work item can be submitted again
2676 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002677 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002678 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2679 }
2680 }
2681
2682 return ret;
2683}
2684
2685/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002687 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688 * This routine indicates if work item @a work is pending in a workqueue's
2689 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002691 * @note Can be called by ISRs.
2692 *
2693 * @param work Address of work item.
2694 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002695 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002696 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002697 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002698static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002699{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002700 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002701}
2702
2703/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002706 * This routine starts workqueue @a work_q. The workqueue spawns its work
2707 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002708 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002709 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002710 * @param stack Pointer to work queue thread's stack space, as defined by
2711 * K_THREAD_STACK_DEFINE()
2712 * @param stack_size Size of the work queue thread's stack (in bytes), which
2713 * should either be the same constant passed to
2714 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002716 *
2717 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002718 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 */
Andrew Boie507852a2017-07-25 18:47:07 -07002720extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002721 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002722 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002724/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002725 * @brief Start a workqueue in user mode
2726 *
2727 * This works identically to k_work_q_start() except it is callable from user
2728 * mode, and the worker thread created will run in user mode.
2729 * The caller must have permissions granted on both the work_q parameter's
2730 * thread and queue objects, and the same restrictions on priority apply as
2731 * k_thread_create().
2732 *
2733 * @param work_q Address of workqueue.
2734 * @param stack Pointer to work queue thread's stack space, as defined by
2735 * K_THREAD_STACK_DEFINE()
2736 * @param stack_size Size of the work queue thread's stack (in bytes), which
2737 * should either be the same constant passed to
2738 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2739 * @param prio Priority of the work queue's thread.
2740 *
2741 * @return N/A
2742 * @req K-WORK-001
2743 */
2744extern void k_work_q_user_start(struct k_work_q *work_q,
2745 k_thread_stack_t *stack,
2746 size_t stack_size, int prio);
2747
2748/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002749 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002751 * This routine initializes a workqueue delayed work item, prior to
2752 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002753 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002754 * @param work Address of delayed work item.
2755 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002756 *
2757 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002758 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002760extern void k_delayed_work_init(struct k_delayed_work *work,
2761 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762
2763/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002764 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002766 * This routine schedules work item @a work to be processed by workqueue
2767 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002768 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002769 * Only when the countdown completes is the work item actually submitted to
2770 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002772 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002773 * counting down cancels the existing submission and restarts the
2774 * countdown using the new delay. Note that this behavior is
2775 * inherently subject to race conditions with the pre-existing
2776 * timeouts and work queue, so care must be taken to synchronize such
2777 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002779 * @warning
2780 * A delayed work item must not be modified until it has been processed
2781 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002783 * @note Can be called by ISRs.
2784 *
2785 * @param work_q Address of workqueue.
2786 * @param work Address of delayed work item.
2787 * @param delay Delay before submitting the work item (in milliseconds).
2788 *
2789 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002790 * @retval -EINVAL Work item is being processed or has completed its work.
2791 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002792 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002794extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2795 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002796 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797
2798/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002799 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002801 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002802 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002803 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002805 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002807 * @note The result of calling this on a k_delayed_work item that has
2808 * not been submitted (i.e. before the return of the
2809 * k_delayed_work_submit_to_queue() call) is undefined.
2810 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002811 * @param work Address of delayed work item.
2812 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002813 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002814 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002815 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002817extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002819/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820 * @brief Submit a work item to the system workqueue.
2821 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002822 * This routine submits work item @a work to be processed by the system
2823 * workqueue. If the work item is already pending in the workqueue's queue
2824 * as a result of an earlier submission, this routine has no effect on the
2825 * work item. If the work item has already been processed, or is currently
2826 * being processed, its work is considered complete and the work item can be
2827 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002829 * @warning
2830 * Work items submitted to the system workqueue should avoid using handlers
2831 * that block or yield since this may prevent the system workqueue from
2832 * processing other work items in a timely manner.
2833 *
2834 * @note Can be called by ISRs.
2835 *
2836 * @param work Address of work item.
2837 *
2838 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002839 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840 */
2841static inline void k_work_submit(struct k_work *work)
2842{
2843 k_work_submit_to_queue(&k_sys_work_q, work);
2844}
2845
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847 * @brief Submit a delayed work item to the system workqueue.
2848 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002849 * This routine schedules work item @a work to be processed by the system
2850 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002851 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002852 * Only when the countdown completes is the work item actually submitted to
2853 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002855 * Submitting a previously submitted delayed work item that is still
2856 * counting down cancels the existing submission and restarts the countdown
2857 * using the new delay. If the work item is currently pending on the
2858 * workqueue's queue because the countdown has completed it is too late to
2859 * resubmit the item, and resubmission fails without impacting the work item.
2860 * If the work item has already been processed, or is currently being processed,
2861 * its work is considered complete and the work item can be resubmitted.
2862 *
2863 * @warning
2864 * Work items submitted to the system workqueue should avoid using handlers
2865 * that block or yield since this may prevent the system workqueue from
2866 * processing other work items in a timely manner.
2867 *
2868 * @note Can be called by ISRs.
2869 *
2870 * @param work Address of delayed work item.
2871 * @param delay Delay before submitting the work item (in milliseconds).
2872 *
2873 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002874 * @retval -EINVAL Work item is being processed or has completed its work.
2875 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002876 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877 */
2878static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002879 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002880{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002881 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002882}
2883
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002884/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002885 * @brief Get time remaining before a delayed work gets scheduled.
2886 *
2887 * This routine computes the (approximate) time remaining before a
2888 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002889 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002890 *
2891 * @param work Delayed work item.
2892 *
2893 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002894 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002895 */
Kumar Galacc334c72017-04-21 10:55:34 -05002896static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002897{
Andy Ross52e444b2018-09-28 09:06:37 -07002898 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002899}
2900
Anas Nashif166f5192018-02-25 08:02:36 -06002901/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002902/**
Anas Nashifce78d162018-05-24 12:43:11 -05002903 * @defgroup mutex_apis Mutex APIs
2904 * @ingroup kernel_apis
2905 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002906 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002907
Anas Nashifce78d162018-05-24 12:43:11 -05002908/**
2909 * Mutex Structure
2910 * @ingroup mutex_apis
2911 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912struct k_mutex {
2913 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002914 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002915 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002916 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002919 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002920};
2921
Anas Nashifce78d162018-05-24 12:43:11 -05002922/**
2923 * @cond INTERNAL_HIDDEN
2924 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002925#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002927 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928 .owner = NULL, \
2929 .lock_count = 0, \
2930 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002931 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932 }
2933
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002934#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2935
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002936/**
Allan Stephensc98da842016-11-11 15:45:03 -05002937 * INTERNAL_HIDDEN @endcond
2938 */
2939
2940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002944 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002945 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002947 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002948 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002949 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002951 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002952 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * Upon completion, the mutex is available and does not have an owner.
2960 *
2961 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002962 *
2963 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002964 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002965 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002966__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002967
2968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * This routine locks @a mutex. If the mutex is locked by another thread,
2972 * the calling thread waits until the mutex becomes available or until
2973 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * A thread is permitted to lock a mutex it has already locked. The operation
2976 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * @param mutex Address of the mutex.
2979 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002980 * or one of the special values K_NO_WAIT and K_FOREVER.
2981 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002982 * @retval 0 Mutex locked.
2983 * @retval -EBUSY Returned without waiting.
2984 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002985 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002986 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002987__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002988
2989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * This routine unlocks @a mutex. The mutex must already be locked by the
2993 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002994 *
2995 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002997 * thread.
2998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003000 *
3001 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003002 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003003 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003004__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005
Allan Stephensc98da842016-11-11 15:45:03 -05003006/**
Anas Nashif166f5192018-02-25 08:02:36 -06003007 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003008 */
3009
3010/**
3011 * @cond INTERNAL_HIDDEN
3012 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003013
3014struct k_sem {
3015 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303016 u32_t count;
3017 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003018 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003020 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021};
3022
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003023#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003024 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003025 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003026 .count = initial_count, \
3027 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003028 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003029 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003030 }
3031
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003032#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003033
Allan Stephensc98da842016-11-11 15:45:03 -05003034/**
3035 * INTERNAL_HIDDEN @endcond
3036 */
3037
3038/**
3039 * @defgroup semaphore_apis Semaphore APIs
3040 * @ingroup kernel_apis
3041 * @{
3042 */
3043
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003044/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * @param sem Address of the semaphore.
3050 * @param initial_count Initial semaphore count.
3051 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052 *
3053 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003054 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003055 */
Andrew Boie99280232017-09-29 14:17:47 -07003056__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3057 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003058
3059/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003064 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3065 *
3066 * @param sem Address of the semaphore.
3067 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003068 * or one of the special values K_NO_WAIT and K_FOREVER.
3069 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003070 * @note When porting code from the nanokernel legacy API to the new API, be
3071 * careful with the return value of this function. The return value is the
3072 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3073 * non-zero means failure, while the nano_sem_take family returns 1 for success
3074 * and 0 for failure.
3075 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003076 * @retval 0 Semaphore taken.
3077 * @retval -EBUSY Returned without waiting.
3078 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003079 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003080 */
Andrew Boie99280232017-09-29 14:17:47 -07003081__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003082
3083/**
3084 * @brief Give a semaphore.
3085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * This routine gives @a sem, unless the semaphore is already at its maximum
3087 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003089 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003091 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003092 *
3093 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003094 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003095 */
Andrew Boie99280232017-09-29 14:17:47 -07003096__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003098/**
3099 * @brief Reset a semaphore's count to zero.
3100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003103 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003104 *
3105 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003106 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003107 */
Andrew Boie990bf162017-10-03 12:36:49 -07003108__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003109
Anas Nashif954d5502018-02-25 08:37:28 -06003110/**
3111 * @internal
3112 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003113static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114{
Patrik Flykt24d71432019-03-26 19:57:45 -06003115 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116}
3117
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003118/**
3119 * @brief Get a semaphore's count.
3120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003125 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003126 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003127 */
Andrew Boie990bf162017-10-03 12:36:49 -07003128__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003129
Anas Nashif954d5502018-02-25 08:37:28 -06003130/**
3131 * @internal
3132 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003133static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003134{
3135 return sem->count;
3136}
3137
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003138/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003142 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003143 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003145 * @param name Name of the semaphore.
3146 * @param initial_count Initial semaphore count.
3147 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003148 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003149 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003151 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003152 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303153 BUILD_ASSERT(((count_limit) != 0) && \
3154 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003155
Anas Nashif166f5192018-02-25 08:02:36 -06003156/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003157
3158/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003159 * @defgroup msgq_apis Message Queue APIs
3160 * @ingroup kernel_apis
3161 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003162 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003163
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003164/**
3165 * @brief Message Queue Structure
3166 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003167struct k_msgq {
3168 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003169 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003170 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003171 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003172 char *buffer_start;
3173 char *buffer_end;
3174 char *read_ptr;
3175 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003176 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003178 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003179 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003181/**
3182 * @cond INTERNAL_HIDDEN
3183 */
3184
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003186#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003187 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003188 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003189 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003190 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003192 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193 .read_ptr = q_buffer, \
3194 .write_ptr = q_buffer, \
3195 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003196 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003197 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003198#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003199/**
3200 * INTERNAL_HIDDEN @endcond
3201 */
3202
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003203
Andrew Boie0fe789f2018-04-12 18:35:56 -07003204#define K_MSGQ_FLAG_ALLOC BIT(0)
3205
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003206/**
3207 * @brief Message Queue Attributes
3208 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303209struct k_msgq_attrs {
3210 size_t msg_size;
3211 u32_t max_msgs;
3212 u32_t used_msgs;
3213};
3214
Allan Stephensc98da842016-11-11 15:45:03 -05003215
3216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003217 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003219 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3220 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003221 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3222 * message is similarly aligned to this boundary, @a q_msg_size must also be
3223 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * The message queue can be accessed outside the module where it is defined
3226 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003227 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003228 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003230 * @param q_name Name of the message queue.
3231 * @param q_msg_size Message size (in bytes).
3232 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003233 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003234 *
3235 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003236 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003237#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3238 static char __noinit __aligned(q_align) \
3239 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3240 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3241 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003242 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003243
Peter Mitsisd7a37502016-10-13 11:37:40 -04003244/**
3245 * @brief Initialize a message queue.
3246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * This routine initializes a message queue object, prior to its first use.
3248 *
Allan Stephensda827222016-11-09 14:23:58 -06003249 * The message queue's ring buffer must contain space for @a max_msgs messages,
3250 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3251 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3252 * that each message is similarly aligned to this boundary, @a q_msg_size
3253 * must also be a multiple of N.
3254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @param q Address of the message queue.
3256 * @param buffer Pointer to ring buffer that holds queued messages.
3257 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003258 * @param max_msgs Maximum number of messages that can be queued.
3259 *
3260 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003261 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003262 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003263void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3264 u32_t max_msgs);
3265
3266/**
3267 * @brief Initialize a message queue.
3268 *
3269 * This routine initializes a message queue object, prior to its first use,
3270 * allocating its internal ring buffer from the calling thread's resource
3271 * pool.
3272 *
3273 * Memory allocated for the ring buffer can be released by calling
3274 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3275 * all of its references.
3276 *
3277 * @param q Address of the message queue.
3278 * @param msg_size Message size (in bytes).
3279 * @param max_msgs Maximum number of messages that can be queued.
3280 *
3281 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3282 * thread's resource pool, or -EINVAL if the size parameters cause
3283 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003284 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003285 */
3286__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3287 u32_t max_msgs);
3288
3289
3290void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003291
3292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003296 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003297 * @note Can be called by ISRs.
3298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @param q Address of the message queue.
3300 * @param data Pointer to the message.
3301 * @param timeout Waiting period to add the message (in milliseconds),
3302 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003303 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003304 * @retval 0 Message sent.
3305 * @retval -ENOMSG Returned without waiting or queue purged.
3306 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003307 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003308 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003309__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003310
3311/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * This routine receives a message from message queue @a q in a "first in,
3315 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003316 *
Allan Stephensc98da842016-11-11 15:45:03 -05003317 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003319 * @param q Address of the message queue.
3320 * @param data Address of area to hold the received message.
3321 * @param timeout Waiting period to receive the message (in milliseconds),
3322 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003323 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003324 * @retval 0 Message received.
3325 * @retval -ENOMSG Returned without waiting.
3326 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003327 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003328 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003329__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003330
3331/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003332 * @brief Peek/read a message from a message queue.
3333 *
3334 * This routine reads a message from message queue @a q in a "first in,
3335 * first out" manner and leaves the message in the queue.
3336 *
3337 * @note Can be called by ISRs.
3338 *
3339 * @param q Address of the message queue.
3340 * @param data Address of area to hold the message read from the queue.
3341 *
3342 * @retval 0 Message read.
3343 * @retval -ENOMSG Returned when the queue has no message.
3344 * @req K-MSGQ-002
3345 */
3346__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3347
3348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * This routine discards all unreceived messages in a message queue's ring
3352 * buffer. Any threads that are blocked waiting to send a message to the
3353 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003356 *
3357 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003358 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003359 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003360__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361
Peter Mitsis67be2492016-10-07 11:44:34 -04003362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * This routine returns the number of unused entries in a message queue's
3366 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003368 * @param q Address of the message queue.
3369 *
3370 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003371 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003372 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003373__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3374
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303375/**
3376 * @brief Get basic attributes of a message queue.
3377 *
3378 * This routine fetches basic attributes of message queue into attr argument.
3379 *
3380 * @param q Address of the message queue.
3381 * @param attrs pointer to message queue attribute structure.
3382 *
3383 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003384 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303385 */
3386__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3387
3388
Patrik Flykt4344e272019-03-08 14:19:05 -07003389static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003390{
3391 return q->max_msgs - q->used_msgs;
3392}
3393
Peter Mitsisd7a37502016-10-13 11:37:40 -04003394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * @param q Address of the message queue.
3400 *
3401 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003402 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003403 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003404__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3405
Patrik Flykt4344e272019-03-08 14:19:05 -07003406static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003407{
3408 return q->used_msgs;
3409}
3410
Anas Nashif166f5192018-02-25 08:02:36 -06003411/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003412
3413/**
3414 * @defgroup mem_pool_apis Memory Pool APIs
3415 * @ingroup kernel_apis
3416 * @{
3417 */
3418
Andy Ross73cb9582017-05-09 10:42:39 -07003419/* Note on sizing: the use of a 20 bit field for block means that,
3420 * assuming a reasonable minimum block size of 16 bytes, we're limited
3421 * to 16M of memory managed by a single pool. Long term it would be
3422 * good to move to a variable bit size based on configuration.
3423 */
3424struct k_mem_block_id {
3425 u32_t pool : 8;
3426 u32_t level : 4;
3427 u32_t block : 20;
3428};
3429
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003432 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003433};
3434
Anas Nashif166f5192018-02-25 08:02:36 -06003435/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003436
3437/**
3438 * @defgroup mailbox_apis Mailbox APIs
3439 * @ingroup kernel_apis
3440 * @{
3441 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442
3443struct k_mbox_msg {
3444 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003445 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003446 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003447 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003448 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003449 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450 /** sender's message data buffer */
3451 void *tx_data;
3452 /** internal use only - needed for legacy API support */
3453 void *_rx_data;
3454 /** message data block descriptor */
3455 struct k_mem_block tx_block;
3456 /** source thread id */
3457 k_tid_t rx_source_thread;
3458 /** target thread id */
3459 k_tid_t tx_target_thread;
3460 /** internal use only - thread waiting on send (may be a dummy) */
3461 k_tid_t _syncing_thread;
3462#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3463 /** internal use only - semaphore used during asynchronous send */
3464 struct k_sem *_async_sem;
3465#endif
3466};
3467
3468struct k_mbox {
3469 _wait_q_t tx_msg_queue;
3470 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003471 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003473 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003475/**
3476 * @cond INTERNAL_HIDDEN
3477 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003478
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003479#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003480 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003481 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3482 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003483 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003484 }
3485
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003486#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3487
Peter Mitsis12092702016-10-14 12:57:23 -04003488/**
Allan Stephensc98da842016-11-11 15:45:03 -05003489 * INTERNAL_HIDDEN @endcond
3490 */
3491
3492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003493 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003495 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003496 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003497 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003500 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003503 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003504 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003505
Peter Mitsis12092702016-10-14 12:57:23 -04003506/**
3507 * @brief Initialize a mailbox.
3508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * This routine initializes a mailbox object, prior to its first use.
3510 *
3511 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003512 *
3513 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003514 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003515 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003516extern void k_mbox_init(struct k_mbox *mbox);
3517
Peter Mitsis12092702016-10-14 12:57:23 -04003518/**
3519 * @brief Send a mailbox message in a synchronous manner.
3520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * This routine sends a message to @a mbox and waits for a receiver to both
3522 * receive and process it. The message data may be in a buffer, in a memory
3523 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * @param mbox Address of the mailbox.
3526 * @param tx_msg Address of the transmit message descriptor.
3527 * @param timeout Waiting period for the message to be received (in
3528 * milliseconds), or one of the special values K_NO_WAIT
3529 * and K_FOREVER. Once the message has been received,
3530 * this routine waits as long as necessary for the message
3531 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003532 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003533 * @retval 0 Message sent.
3534 * @retval -ENOMSG Returned without waiting.
3535 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003536 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003537 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003538extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003539 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003540
Peter Mitsis12092702016-10-14 12:57:23 -04003541/**
3542 * @brief Send a mailbox message in an asynchronous manner.
3543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * This routine sends a message to @a mbox without waiting for a receiver
3545 * to process it. The message data may be in a buffer, in a memory pool block,
3546 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3547 * will be given when the message has been both received and completely
3548 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * @param mbox Address of the mailbox.
3551 * @param tx_msg Address of the transmit message descriptor.
3552 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003553 *
3554 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003555 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003556 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003557extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003558 struct k_sem *sem);
3559
Peter Mitsis12092702016-10-14 12:57:23 -04003560/**
3561 * @brief Receive a mailbox message.
3562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * This routine receives a message from @a mbox, then optionally retrieves
3564 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003566 * @param mbox Address of the mailbox.
3567 * @param rx_msg Address of the receive message descriptor.
3568 * @param buffer Address of the buffer to receive data, or NULL to defer data
3569 * retrieval and message disposal until later.
3570 * @param timeout Waiting period for a message to be received (in
3571 * milliseconds), or one of the special values K_NO_WAIT
3572 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003573 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003574 * @retval 0 Message received.
3575 * @retval -ENOMSG Returned without waiting.
3576 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003577 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003578 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003579extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003580 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003581
3582/**
3583 * @brief Retrieve mailbox message data into a buffer.
3584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * This routine completes the processing of a received message by retrieving
3586 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003587 *
3588 * Alternatively, this routine can be used to dispose of a received message
3589 * without retrieving its data.
3590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @param rx_msg Address of the receive message descriptor.
3592 * @param buffer Address of the buffer to receive data, or NULL to discard
3593 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003594 *
3595 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003596 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003597 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003598extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003599
3600/**
3601 * @brief Retrieve mailbox message data into a memory pool block.
3602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * This routine completes the processing of a received message by retrieving
3604 * its data into a memory pool block, then disposing of the message.
3605 * The memory pool block that results from successful retrieval must be
3606 * returned to the pool once the data has been processed, even in cases
3607 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003608 *
3609 * Alternatively, this routine can be used to dispose of a received message
3610 * without retrieving its data. In this case there is no need to return a
3611 * memory pool block to the pool.
3612 *
3613 * This routine allocates a new memory pool block for the data only if the
3614 * data is not already in one. If a new block cannot be allocated, the routine
3615 * returns a failure code and the received message is left unchanged. This
3616 * permits the caller to reattempt data retrieval at a later time or to dispose
3617 * of the received message without retrieving its data.
3618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @param rx_msg Address of a receive message descriptor.
3620 * @param pool Address of memory pool, or NULL to discard data.
3621 * @param block Address of the area to hold memory pool block info.
3622 * @param timeout Waiting period to wait for a memory pool block (in
3623 * milliseconds), or one of the special values K_NO_WAIT
3624 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003625 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003626 * @retval 0 Data retrieved.
3627 * @retval -ENOMEM Returned without waiting.
3628 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003629 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003630 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003631extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003632 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003633 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003634
Anas Nashif166f5192018-02-25 08:02:36 -06003635/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003636
3637/**
Anas Nashifce78d162018-05-24 12:43:11 -05003638 * @defgroup pipe_apis Pipe APIs
3639 * @ingroup kernel_apis
3640 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003641 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003642
Anas Nashifce78d162018-05-24 12:43:11 -05003643/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003644struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003645 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3646 size_t size; /**< Buffer size */
3647 size_t bytes_used; /**< # bytes used in buffer */
3648 size_t read_index; /**< Where in buffer to read from */
3649 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003650 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003651
3652 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003653 _wait_q_t readers; /**< Reader wait queue */
3654 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003655 } wait_q;
3656
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003657 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003658 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003659};
3660
Anas Nashifce78d162018-05-24 12:43:11 -05003661/**
3662 * @cond INTERNAL_HIDDEN
3663 */
3664#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3665
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003666#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3667 { \
3668 .buffer = pipe_buffer, \
3669 .size = pipe_buffer_size, \
3670 .bytes_used = 0, \
3671 .read_index = 0, \
3672 .write_index = 0, \
3673 .lock = {}, \
3674 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003675 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3676 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003677 }, \
3678 _OBJECT_TRACING_INIT \
3679 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680 }
3681
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003682#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3683
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003684/**
Allan Stephensc98da842016-11-11 15:45:03 -05003685 * INTERNAL_HIDDEN @endcond
3686 */
3687
3688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003689 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003692 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003693 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003695 * @param name Name of the pipe.
3696 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3697 * or zero if no ring buffer is used.
3698 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003699 *
3700 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003701 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003702#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003703 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003704 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003705 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003706 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003707
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003709 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003711 * This routine initializes a pipe object, prior to its first use.
3712 *
3713 * @param pipe Address of the pipe.
3714 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3715 * is used.
3716 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3717 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 *
3719 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003720 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003721 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003722void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3723
3724/**
3725 * @brief Release a pipe's allocated buffer
3726 *
3727 * If a pipe object was given a dynamically allocated buffer via
3728 * k_pipe_alloc_init(), this will free it. This function does nothing
3729 * if the buffer wasn't dynamically allocated.
3730 *
3731 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003732 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003733 */
3734void k_pipe_cleanup(struct k_pipe *pipe);
3735
3736/**
3737 * @brief Initialize a pipe and allocate a buffer for it
3738 *
3739 * Storage for the buffer region will be allocated from the calling thread's
3740 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3741 * or userspace is enabled and the pipe object loses all references to it.
3742 *
3743 * This function should only be called on uninitialized pipe objects.
3744 *
3745 * @param pipe Address of the pipe.
3746 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3747 * buffer is used.
3748 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003749 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003750 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003751 */
3752__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003753
3754/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003755 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003757 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003759 * @param pipe Address of the pipe.
3760 * @param data Address of data to write.
3761 * @param bytes_to_write Size of data (in bytes).
3762 * @param bytes_written Address of area to hold the number of bytes written.
3763 * @param min_xfer Minimum number of bytes to write.
3764 * @param timeout Waiting period to wait for the data to be written (in
3765 * milliseconds), or one of the special values K_NO_WAIT
3766 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003767 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003768 * @retval 0 At least @a min_xfer bytes of data were written.
3769 * @retval -EIO Returned without waiting; zero data bytes were written.
3770 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003771 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003772 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003774__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3775 size_t bytes_to_write, size_t *bytes_written,
3776 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003777
3778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003779 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003781 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003783 * @param pipe Address of the pipe.
3784 * @param data Address to place the data read from pipe.
3785 * @param bytes_to_read Maximum number of data bytes to read.
3786 * @param bytes_read Address of area to hold the number of bytes read.
3787 * @param min_xfer Minimum number of data bytes to read.
3788 * @param timeout Waiting period to wait for the data to be read (in
3789 * milliseconds), or one of the special values K_NO_WAIT
3790 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003791 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003792 * @retval 0 At least @a min_xfer bytes of data were read.
3793 * @retval -EIO Returned without waiting; zero data bytes were read.
3794 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003795 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003796 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003798__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3799 size_t bytes_to_read, size_t *bytes_read,
3800 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801
3802/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003803 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003805 * This routine writes the data contained in a memory block to @a pipe.
3806 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003809 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810 * @param block Memory block containing data to send
3811 * @param size Number of data bytes in memory block to send
3812 * @param sem Semaphore to signal upon completion (else NULL)
3813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003814 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003815 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 */
3817extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3818 size_t size, struct k_sem *sem);
3819
Anas Nashif166f5192018-02-25 08:02:36 -06003820/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821
Allan Stephensc98da842016-11-11 15:45:03 -05003822/**
3823 * @cond INTERNAL_HIDDEN
3824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003825
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003826struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003828 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003829 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003830 char *buffer;
3831 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003832 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003834 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003835};
3836
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003837#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003838 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003840 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003841 .num_blocks = slab_num_blocks, \
3842 .block_size = slab_block_size, \
3843 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003844 .free_list = NULL, \
3845 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003846 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 }
3848
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003849#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3850
3851
Peter Mitsis578f9112016-10-07 13:50:31 -04003852/**
Allan Stephensc98da842016-11-11 15:45:03 -05003853 * INTERNAL_HIDDEN @endcond
3854 */
3855
3856/**
3857 * @defgroup mem_slab_apis Memory Slab APIs
3858 * @ingroup kernel_apis
3859 * @{
3860 */
3861
3862/**
Allan Stephensda827222016-11-09 14:23:58 -06003863 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003864 *
Allan Stephensda827222016-11-09 14:23:58 -06003865 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003866 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003867 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3868 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003869 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003870 *
Allan Stephensda827222016-11-09 14:23:58 -06003871 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003872 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003873 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003874 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003876 * @param name Name of the memory slab.
3877 * @param slab_block_size Size of each memory block (in bytes).
3878 * @param slab_num_blocks Number memory blocks.
3879 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003880 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003881 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003882#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3883 char __noinit __aligned(slab_align) \
3884 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003885 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003886 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003887 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003888
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003889/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003890 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003892 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003893 *
Allan Stephensda827222016-11-09 14:23:58 -06003894 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3895 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3896 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3897 * To ensure that each memory block is similarly aligned to this boundary,
3898 * @a slab_block_size must also be a multiple of N.
3899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003900 * @param slab Address of the memory slab.
3901 * @param buffer Pointer to buffer used for the memory blocks.
3902 * @param block_size Size of each memory block (in bytes).
3903 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003904 *
3905 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003906 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003907 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003908extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003909 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003910
3911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003914 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003916 * @param slab Address of the memory slab.
3917 * @param mem Pointer to block address area.
3918 * @param timeout Maximum time to wait for operation to complete
3919 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3920 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003921 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003922 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003923 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003924 * @retval -ENOMEM Returned without waiting.
3925 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003926 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003927 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003928extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003929 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003930
3931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003934 * This routine releases a previously allocated memory block back to its
3935 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003937 * @param slab Address of the memory slab.
3938 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939 *
3940 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003941 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003943extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003944
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003948 * This routine gets the number of memory blocks that are currently
3949 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003951 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003954 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003955 */
Kumar Galacc334c72017-04-21 10:55:34 -05003956static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003957{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003958 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003959}
3960
Peter Mitsisc001aa82016-10-13 13:53:37 -04003961/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003962 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * This routine gets the number of memory blocks that are currently
3965 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003967 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003970 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003971 */
Kumar Galacc334c72017-04-21 10:55:34 -05003972static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003973{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003974 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003975}
3976
Anas Nashif166f5192018-02-25 08:02:36 -06003977/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003978
3979/**
3980 * @cond INTERNAL_HIDDEN
3981 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003982
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003983struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003984 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003985 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003986};
3987
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003988/**
Allan Stephensc98da842016-11-11 15:45:03 -05003989 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003990 */
3991
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003992/**
Allan Stephensc98da842016-11-11 15:45:03 -05003993 * @addtogroup mem_pool_apis
3994 * @{
3995 */
3996
3997/**
3998 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004000 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4001 * long. The memory pool allows blocks to be repeatedly partitioned into
4002 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004003 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004004 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004005 * If the pool is to be accessed outside the module where it is defined, it
4006 * can be declared via
4007 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004008 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004010 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004011 * @param minsz Size of the smallest blocks in the pool (in bytes).
4012 * @param maxsz Size of the largest blocks in the pool (in bytes).
4013 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004014 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004015 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004016 */
Andy Ross73cb9582017-05-09 10:42:39 -07004017#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4018 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4019 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004020 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004021 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004022 .base = { \
4023 .buf = _mpool_buf_##name, \
4024 .max_sz = maxsz, \
4025 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004026 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004027 .levels = _mpool_lvls_##name, \
4028 .flags = SYS_MEM_POOL_KERNEL \
4029 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004030 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004031
Peter Mitsis937042c2016-10-13 13:18:26 -04004032/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004033 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004035 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004037 * @param pool Address of the memory pool.
4038 * @param block Pointer to block descriptor for the allocated memory.
4039 * @param size Amount of memory to allocate (in bytes).
4040 * @param timeout Maximum time to wait for operation to complete
4041 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4042 * or K_FOREVER to wait as long as necessary.
4043 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004044 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004045 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004046 * @retval -ENOMEM Returned without waiting.
4047 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004048 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004049 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004050extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004051 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004052
4053/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004054 * @brief Allocate memory from a memory pool with malloc() semantics
4055 *
4056 * Such memory must be released using k_free().
4057 *
4058 * @param pool Address of the memory pool.
4059 * @param size Amount of memory to allocate (in bytes).
4060 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004061 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004062 */
4063extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4064
4065/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004066 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004068 * This routine releases a previously allocated memory block back to its
4069 * memory pool.
4070 *
4071 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004072 *
4073 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004074 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004075 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004076extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004077
4078/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004079 * @brief Free memory allocated from a memory pool.
4080 *
4081 * This routine releases a previously allocated memory block back to its
4082 * memory pool
4083 *
4084 * @param id Memory block identifier.
4085 *
4086 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004087 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004088 */
4089extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4090
4091/**
Anas Nashif166f5192018-02-25 08:02:36 -06004092 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004093 */
4094
4095/**
4096 * @defgroup heap_apis Heap Memory Pool APIs
4097 * @ingroup kernel_apis
4098 * @{
4099 */
4100
4101/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004102 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004104 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004105 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004107 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004109 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004110 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004111 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004112extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004113
4114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004115 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004116 *
4117 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004118 * returned must have been allocated from the heap memory pool or
4119 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004120 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004121 * If @a ptr is NULL, no operation is performed.
4122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004123 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004124 *
4125 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004126 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004127 */
4128extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004129
Allan Stephensc98da842016-11-11 15:45:03 -05004130/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004131 * @brief Allocate memory from heap, array style
4132 *
4133 * This routine provides traditional calloc() semantics. Memory is
4134 * allocated from the heap memory pool and zeroed.
4135 *
4136 * @param nmemb Number of elements in the requested array
4137 * @param size Size of each array element (in bytes).
4138 *
4139 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004140 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004141 */
4142extern void *k_calloc(size_t nmemb, size_t size);
4143
Anas Nashif166f5192018-02-25 08:02:36 -06004144/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004145
Benjamin Walshacc68c12017-01-29 18:57:45 -05004146/* polling API - PRIVATE */
4147
Benjamin Walshb0179862017-02-02 16:39:57 -05004148#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004149#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004150#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004151#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004152#endif
4153
Benjamin Walshacc68c12017-01-29 18:57:45 -05004154/* private - implementation data created as needed, per-type */
4155struct _poller {
4156 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004157 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004158};
4159
4160/* private - types bit positions */
4161enum _poll_types_bits {
4162 /* can be used to ignore an event */
4163 _POLL_TYPE_IGNORE,
4164
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004165 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004166 _POLL_TYPE_SIGNAL,
4167
4168 /* semaphore availability */
4169 _POLL_TYPE_SEM_AVAILABLE,
4170
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004171 /* queue/fifo/lifo data availability */
4172 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004173
4174 _POLL_NUM_TYPES
4175};
4176
Patrik Flykt4344e272019-03-08 14:19:05 -07004177#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004178
4179/* private - states bit positions */
4180enum _poll_states_bits {
4181 /* default state when creating event */
4182 _POLL_STATE_NOT_READY,
4183
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004184 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004185 _POLL_STATE_SIGNALED,
4186
4187 /* semaphore is available */
4188 _POLL_STATE_SEM_AVAILABLE,
4189
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004190 /* data is available to read on queue/fifo/lifo */
4191 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004192
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004193 /* queue/fifo/lifo wait was cancelled */
4194 _POLL_STATE_CANCELLED,
4195
Benjamin Walshacc68c12017-01-29 18:57:45 -05004196 _POLL_NUM_STATES
4197};
4198
Patrik Flykt4344e272019-03-08 14:19:05 -07004199#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004200
4201#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004202 (32 - (0 \
4203 + 8 /* tag */ \
4204 + _POLL_NUM_TYPES \
4205 + _POLL_NUM_STATES \
4206 + 1 /* modes */ \
4207 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004208
Benjamin Walshacc68c12017-01-29 18:57:45 -05004209/* end of polling API - PRIVATE */
4210
4211
4212/**
4213 * @defgroup poll_apis Async polling APIs
4214 * @ingroup kernel_apis
4215 * @{
4216 */
4217
4218/* Public polling API */
4219
4220/* public - values for k_poll_event.type bitfield */
4221#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004222#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4223#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4224#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004225#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004226
4227/* public - polling modes */
4228enum k_poll_modes {
4229 /* polling thread does not take ownership of objects when available */
4230 K_POLL_MODE_NOTIFY_ONLY = 0,
4231
4232 K_POLL_NUM_MODES
4233};
4234
4235/* public - values for k_poll_event.state bitfield */
4236#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004237#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4238#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4239#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004240#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004241#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004242
4243/* public - poll signal object */
4244struct k_poll_signal {
4245 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004246 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004247
4248 /*
4249 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4250 * user resets it to 0.
4251 */
4252 unsigned int signaled;
4253
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004254 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255 int result;
4256};
4257
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004258#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004259 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004260 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261 .signaled = 0, \
4262 .result = 0, \
4263 }
4264
4265struct k_poll_event {
4266 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004267 sys_dnode_t _node;
4268
4269 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004270 struct _poller *poller;
4271
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004272 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004273 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004274
Benjamin Walshacc68c12017-01-29 18:57:45 -05004275 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004276 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004277
4278 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004279 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004280
4281 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004282 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004283
4284 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004285 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004286
4287 /* per-type data */
4288 union {
4289 void *obj;
4290 struct k_poll_signal *signal;
4291 struct k_sem *sem;
4292 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004293 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004294 };
4295};
4296
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004297#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004298 { \
4299 .poller = NULL, \
4300 .type = event_type, \
4301 .state = K_POLL_STATE_NOT_READY, \
4302 .mode = event_mode, \
4303 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004304 { .obj = event_obj }, \
4305 }
4306
4307#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4308 event_tag) \
4309 { \
4310 .type = event_type, \
4311 .tag = event_tag, \
4312 .state = K_POLL_STATE_NOT_READY, \
4313 .mode = event_mode, \
4314 .unused = 0, \
4315 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004316 }
4317
4318/**
4319 * @brief Initialize one struct k_poll_event instance
4320 *
4321 * After this routine is called on a poll event, the event it ready to be
4322 * placed in an event array to be passed to k_poll().
4323 *
4324 * @param event The event to initialize.
4325 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4326 * values. Only values that apply to the same object being polled
4327 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4328 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004329 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004330 * @param obj Kernel object or poll signal.
4331 *
4332 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004333 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004334 */
4335
Kumar Galacc334c72017-04-21 10:55:34 -05004336extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004337 int mode, void *obj);
4338
4339/**
4340 * @brief Wait for one or many of multiple poll events to occur
4341 *
4342 * This routine allows a thread to wait concurrently for one or many of
4343 * multiple poll events to have occurred. Such events can be a kernel object
4344 * being available, like a semaphore, or a poll signal event.
4345 *
4346 * When an event notifies that a kernel object is available, the kernel object
4347 * is not "given" to the thread calling k_poll(): it merely signals the fact
4348 * that the object was available when the k_poll() call was in effect. Also,
4349 * all threads trying to acquire an object the regular way, i.e. by pending on
4350 * the object, have precedence over the thread polling on the object. This
4351 * means that the polling thread will never get the poll event on an object
4352 * until the object becomes available and its pend queue is empty. For this
4353 * reason, the k_poll() call is more effective when the objects being polled
4354 * only have one thread, the polling thread, trying to acquire them.
4355 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004356 * When k_poll() returns 0, the caller should loop on all the events that were
4357 * passed to k_poll() and check the state field for the values that were
4358 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004359 *
4360 * Before being reused for another call to k_poll(), the user has to reset the
4361 * state field to K_POLL_STATE_NOT_READY.
4362 *
Andrew Boie3772f772018-05-07 16:52:57 -07004363 * When called from user mode, a temporary memory allocation is required from
4364 * the caller's resource pool.
4365 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004366 * @param events An array of pointers to events to be polled for.
4367 * @param num_events The number of events in the array.
4368 * @param timeout Waiting period for an event to be ready (in milliseconds),
4369 * or one of the special values K_NO_WAIT and K_FOREVER.
4370 *
4371 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004372 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004373 * @retval -EINTR Polling has been interrupted, e.g. with
4374 * k_queue_cancel_wait(). All output events are still set and valid,
4375 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4376 * words, -EINTR status means that at least one of output events is
4377 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004378 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4379 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004380 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004381 */
4382
Andrew Boie3772f772018-05-07 16:52:57 -07004383__syscall int k_poll(struct k_poll_event *events, int num_events,
4384 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004385
4386/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004387 * @brief Initialize a poll signal object.
4388 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004389 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004390 *
4391 * @param signal A poll signal.
4392 *
4393 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004394 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004395 */
4396
Andrew Boie3772f772018-05-07 16:52:57 -07004397__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4398
4399/*
4400 * @brief Reset a poll signal object's state to unsignaled.
4401 *
4402 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004403 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004404 */
4405__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4406
Patrik Flykt4344e272019-03-08 14:19:05 -07004407static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004408{
Patrik Flykt24d71432019-03-26 19:57:45 -06004409 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004410}
4411
4412/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004413 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004414 *
4415 * @param signal A poll signal object
4416 * @param signaled An integer buffer which will be written nonzero if the
4417 * object was signaled
4418 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004419 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004420 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004421 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004422 */
4423__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4424 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004425
4426/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004427 * @brief Signal a poll signal object.
4428 *
4429 * This routine makes ready a poll signal, which is basically a poll event of
4430 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4431 * made ready to run. A @a result value can be specified.
4432 *
4433 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004434 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004435 * k_poll_signal_reset(). It thus has to be reset by the user before being
4436 * passed again to k_poll() or k_poll() will consider it being signaled, and
4437 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004438 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004439 * @note The result is stored and the 'signaled' field is set even if
4440 * this function returns an error indicating that an expiring poll was
4441 * not notified. The next k_poll() will detect the missed raise.
4442 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004443 * @param signal A poll signal.
4444 * @param result The value to store in the result field of the signal.
4445 *
4446 * @retval 0 The signal was delivered successfully.
4447 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004448 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004449 */
4450
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004451__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004452
Anas Nashif954d5502018-02-25 08:37:28 -06004453/**
4454 * @internal
4455 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004456extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004457
Anas Nashif166f5192018-02-25 08:02:36 -06004458/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004459
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004460/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004461 * @defgroup cpu_idle_apis CPU Idling APIs
4462 * @ingroup kernel_apis
4463 * @{
4464 */
4465
4466/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004467 * @brief Make the CPU idle.
4468 *
4469 * This function makes the CPU idle until an event wakes it up.
4470 *
4471 * In a regular system, the idle thread should be the only thread responsible
4472 * for making the CPU idle and triggering any type of power management.
4473 * However, in some more constrained systems, such as a single-threaded system,
4474 * the only thread would be responsible for this if needed.
4475 *
4476 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004477 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004478 */
4479extern void k_cpu_idle(void);
4480
4481/**
4482 * @brief Make the CPU idle in an atomic fashion.
4483 *
4484 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4485 * must be done atomically before making the CPU idle.
4486 *
4487 * @param key Interrupt locking key obtained from irq_lock().
4488 *
4489 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004490 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004491 */
4492extern void k_cpu_atomic_idle(unsigned int key);
4493
Anas Nashif30c3cff2019-01-22 08:18:13 -05004494/**
4495 * @}
4496 */
Anas Nashif954d5502018-02-25 08:37:28 -06004497
4498/**
4499 * @internal
4500 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004501extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004502
Patrik Flykt4344e272019-03-08 14:19:05 -07004503#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004504/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004505#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004506#else
4507
Andrew Boiecdb94d62017-04-18 15:22:05 -07004508/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004509 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004510 *
4511 * We won't have a real exception frame to determine the PC value when
4512 * the oops occurred, so print file and line number before we jump into
4513 * the fatal error handler.
4514 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004515#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004516 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Patrik Flykt4344e272019-03-08 14:19:05 -07004517 z_NanoFatalErrorHandler(reason, &_default_esf); \
Andy Ross92ce7672019-05-31 13:12:15 -07004518 k_thread_abort(k_current_get()); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004519 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004520
4521#endif /* _ARCH__EXCEPT */
4522
4523/**
4524 * @brief Fatally terminate a thread
4525 *
4526 * This should be called when a thread has encountered an unrecoverable
4527 * runtime condition and needs to terminate. What this ultimately
4528 * means is determined by the _fatal_error_handler() implementation, which
4529 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4530 *
4531 * If this is called from ISR context, the default system fatal error handler
4532 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004533 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004534 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004535#define k_oops() z_except_reason(_NANO_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004536
4537/**
4538 * @brief Fatally terminate the system
4539 *
4540 * This should be called when the Zephyr kernel has encountered an
4541 * unrecoverable runtime condition and needs to terminate. What this ultimately
4542 * means is determined by the _fatal_error_handler() implementation, which
4543 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004544 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004545 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004546#define k_panic() z_except_reason(_NANO_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004547
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004548/*
4549 * private APIs that are utilized by one or more public APIs
4550 */
4551
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004552#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004553/**
4554 * @internal
4555 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004556extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004557#else
Anas Nashif954d5502018-02-25 08:37:28 -06004558/**
4559 * @internal
4560 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004561#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004562#endif
4563
Anas Nashif954d5502018-02-25 08:37:28 -06004564/**
4565 * @internal
4566 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004567extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004568/**
4569 * @internal
4570 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004571extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004572
Andrew Boiedc5d9352017-06-02 12:56:47 -07004573/* arch/cpu.h may declare an architecture or platform-specific macro
4574 * for properly declaring stacks, compatible with MMU/MPU constraints if
4575 * enabled
4576 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004577
4578/**
4579 * @brief Obtain an extern reference to a stack
4580 *
4581 * This macro properly brings the symbol of a thread stack declared
4582 * elsewhere into scope.
4583 *
4584 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004585 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004586 */
4587#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4588
Patrik Flykt4344e272019-03-08 14:19:05 -07004589#ifdef Z_ARCH_THREAD_STACK_DEFINE
4590#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004591#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004592 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4593#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4594#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4595#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004596#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004597static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004598{
Patrik Flykt4344e272019-03-08 14:19:05 -07004599 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004600}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004601#else
4602/**
4603 * @brief Declare a toplevel thread stack memory region
4604 *
4605 * This declares a region of memory suitable for use as a thread's stack.
4606 *
4607 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4608 * 'noinit' section so that it isn't zeroed at boot
4609 *
Andrew Boie507852a2017-07-25 18:47:07 -07004610 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004611 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004612 * inside needs to be examined, examine thread->stack_info for the associated
4613 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004614 *
4615 * It is legal to precede this definition with the 'static' keyword.
4616 *
4617 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4618 * parameter of k_thread_create(), it may not be the same as the
4619 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4620 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004621 * Some arches may round the size of the usable stack region up to satisfy
4622 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4623 * size.
4624 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004625 * @param sym Thread stack symbol name
4626 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004627 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004628 */
4629#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004630 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004631
4632/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304633 * @brief Calculate size of stacks to be allocated in a stack array
4634 *
4635 * This macro calculates the size to be allocated for the stacks
4636 * inside a stack array. It accepts the indicated "size" as a parameter
4637 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4638 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4639 *
4640 * @param size Size of the stack memory region
4641 * @req K-TSTACK-001
4642 */
4643#define K_THREAD_STACK_LEN(size) (size)
4644
4645/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004646 * @brief Declare a toplevel array of thread stack memory regions
4647 *
4648 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4649 * definition for additional details and constraints.
4650 *
4651 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4652 * 'noinit' section so that it isn't zeroed at boot
4653 *
4654 * @param sym Thread stack symbol name
4655 * @param nmemb Number of stacks to declare
4656 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004657 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004658 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004659#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004660 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304661 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004662
4663/**
4664 * @brief Declare an embedded stack memory region
4665 *
4666 * Used for stacks embedded within other data structures. Use is highly
4667 * discouraged but in some cases necessary. For memory protection scenarios,
4668 * it is very important that any RAM preceding this member not be writable
4669 * by threads else a stack overflow will lead to silent corruption. In other
4670 * words, the containing data structure should live in RAM owned by the kernel.
4671 *
4672 * @param sym Thread stack symbol name
4673 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004674 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004675 */
4676#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004677 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004678
4679/**
4680 * @brief Return the size in bytes of a stack memory region
4681 *
4682 * Convenience macro for passing the desired stack size to k_thread_create()
4683 * since the underlying implementation may actually create something larger
4684 * (for instance a guard area).
4685 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004686 * The value returned here is not guaranteed to match the 'size' parameter
4687 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004688 *
4689 * @param sym Stack memory symbol
4690 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004691 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004692 */
4693#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4694
Andrew Boie575abc02019-03-19 10:42:24 -07004695
4696/**
4697 * @brief Indicate how much additional memory is reserved for stack objects
4698 *
4699 * Any given stack declaration may have additional memory in it for guard
4700 * areas or supervisor mode stacks. This macro indicates how much space
4701 * is reserved for this. The memory reserved may not be contiguous within
4702 * the stack object, and does not account for additional space used due to
4703 * enforce alignment.
4704 */
4705#define K_THREAD_STACK_RESERVED 0
4706
Andrew Boiedc5d9352017-06-02 12:56:47 -07004707/**
4708 * @brief Get a pointer to the physical stack buffer
4709 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004710 * This macro is deprecated. If a stack buffer needs to be examined, the
4711 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004712 *
4713 * @param sym Declared stack symbol name
4714 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004715 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004716 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004717static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004718{
4719 return (char *)sym;
4720}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004721
4722#endif /* _ARCH_DECLARE_STACK */
4723
Chunlin Hane9c97022017-07-07 20:29:30 +08004724/**
4725 * @defgroup mem_domain_apis Memory domain APIs
4726 * @ingroup kernel_apis
4727 * @{
4728 */
4729
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004730/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004731 * @def K_MEM_PARTITION_DEFINE
4732 * @brief Used to declare a memory partition
4733 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004734 */
4735#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4736#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4737 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004738 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004739 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004740#else
4741#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004742 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004743 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004744#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4745
Chunlin Hane9c97022017-07-07 20:29:30 +08004746/* memory partition */
4747struct k_mem_partition {
4748 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004749 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004750 /* size of memory partition */
4751 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004752#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004753 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304754 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004755#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004756};
4757
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004758/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304759 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004760struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304761#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004762 /* partitions in the domain */
4763 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304764#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004765 /* domain q */
4766 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004767 /* number of partitions in the domain */
4768 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004769};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304770
Chunlin Hane9c97022017-07-07 20:29:30 +08004771
4772/**
4773 * @brief Initialize a memory domain.
4774 *
4775 * Initialize a memory domain with given name and memory partitions.
4776 *
4777 * @param domain The memory domain to be initialized.
4778 * @param num_parts The number of array items of "parts" parameter.
4779 * @param parts An array of pointers to the memory partitions. Can be NULL
4780 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004781 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004782 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004783extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004784 struct k_mem_partition *parts[]);
4785/**
4786 * @brief Destroy a memory domain.
4787 *
4788 * Destroy a memory domain.
4789 *
4790 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004791 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004792 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004793extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4794
4795/**
4796 * @brief Add a memory partition into a memory domain.
4797 *
4798 * Add a memory partition into a memory domain.
4799 *
4800 * @param domain The memory domain to be added a memory partition.
4801 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004802 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004803 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004804extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4805 struct k_mem_partition *part);
4806
4807/**
4808 * @brief Remove a memory partition from a memory domain.
4809 *
4810 * Remove a memory partition from a memory domain.
4811 *
4812 * @param domain The memory domain to be removed a memory partition.
4813 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004814 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004815 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004816extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4817 struct k_mem_partition *part);
4818
4819/**
4820 * @brief Add a thread into a memory domain.
4821 *
4822 * Add a thread into a memory domain.
4823 *
4824 * @param domain The memory domain that the thread is going to be added into.
4825 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004826 *
4827 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004828 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004829extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4830 k_tid_t thread);
4831
4832/**
4833 * @brief Remove a thread from its memory domain.
4834 *
4835 * Remove a thread from its memory domain.
4836 *
4837 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004838 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004839 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004840extern void k_mem_domain_remove_thread(k_tid_t thread);
4841
Anas Nashif166f5192018-02-25 08:02:36 -06004842/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004843
Andrew Boie756f9072017-10-10 16:01:49 -07004844/**
4845 * @brief Emit a character buffer to the console device
4846 *
4847 * @param c String of characters to print
4848 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004849 *
4850 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004851 */
4852__syscall void k_str_out(char *c, size_t n);
4853
Andy Rosse7172672018-01-24 15:48:32 -08004854/**
4855 * @brief Start a numbered CPU on a MP-capable system
4856
4857 * This starts and initializes a specific CPU. The main thread on
4858 * startup is running on CPU zero, other processors are numbered
4859 * sequentially. On return from this function, the CPU is known to
4860 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004861 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004862 * with the provided key will work to enable them.
4863 *
4864 * Normally, in SMP mode this function will be called by the kernel
4865 * initialization and should not be used as a user API. But it is
4866 * defined here for special-purpose apps which want Zephyr running on
4867 * one core and to use others for design-specific processing.
4868 *
4869 * @param cpu_num Integer number of the CPU
4870 * @param stack Stack memory for the CPU
4871 * @param sz Stack buffer size, in bytes
4872 * @param fn Function to begin running on the CPU. First argument is
4873 * an irq_unlock() key.
4874 * @param arg Untyped argument to be passed to "fn"
4875 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004876extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004877 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004878
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004879#ifdef __cplusplus
4880}
4881#endif
4882
Anas Nashifb6304e62018-07-04 08:03:03 -05004883#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004884#include <syscalls/kernel.h>
4885
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004886#endif /* !_ASMLANGUAGE */
4887
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004888#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */