blob: 3cd937963ebe6cb5cf4fa4c55dad806332f4028e [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
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400349 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700350 * 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 *
Anas Nashif240c5162019-06-10 12:25:50 -04001046 * @note
1047 * @rststar
1048 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1049 * configuration.
1050 * @endrststar
1051 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001052 * @param thread A thread on which to set the deadline
1053 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001054 *
1055 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001056 */
1057__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1058#endif
1059
Andy Rossab46b1b2019-01-30 15:00:42 -08001060#ifdef CONFIG_SCHED_CPU_MASK
1061/**
1062 * @brief Sets all CPU enable masks to zero
1063 *
1064 * After this returns, the thread will no longer be schedulable on any
1065 * CPUs. The thread must not be currently runnable.
1066 *
Anas Nashif240c5162019-06-10 12:25:50 -04001067 * @note
1068 * @rststar
1069 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1070 * configuration.
1071 * @endrststar
1072 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001073 * @param thread Thread to operate upon
1074 * @return Zero on success, otherwise error code
1075 */
1076int k_thread_cpu_mask_clear(k_tid_t thread);
1077
1078/**
1079 * @brief Sets all CPU enable masks to one
1080 *
1081 * After this returns, the thread will be schedulable on any CPU. The
1082 * thread must not be currently runnable.
1083 *
Anas Nashif240c5162019-06-10 12:25:50 -04001084 * @note
1085 * @rststar
1086 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1087 * configuration.
1088 * @endrststar
1089 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001090 * @param thread Thread to operate upon
1091 * @return Zero on success, otherwise error code
1092 */
1093int k_thread_cpu_mask_enable_all(k_tid_t thread);
1094
1095/**
1096 * @brief Enable thread to run on specified CPU
1097 *
1098 * The thread must not be currently runnable.
1099 *
Anas Nashif240c5162019-06-10 12:25:50 -04001100 * @note
1101 * @rststar
1102 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1103 * configuration.
1104 * @endrststar
1105 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001106 * @param thread Thread to operate upon
1107 * @param cpu CPU index
1108 * @return Zero on success, otherwise error code
1109 */
1110int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1111
1112/**
1113 * @brief Prevent thread to run on specified CPU
1114 *
1115 * The thread must not be currently runnable.
1116 *
Anas Nashif240c5162019-06-10 12:25:50 -04001117 * @note
1118 * @rststar
1119 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1120 * configuration.
1121 * @endrststar
1122 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001123 * @param thread Thread to operate upon
1124 * @param cpu CPU index
1125 * @return Zero on success, otherwise error code
1126 */
1127int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1128#endif
1129
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * This routine prevents the kernel scheduler from making @a thread the
1134 * current thread. All other internal operations on @a thread are still
1135 * performed; for example, any timeout it is waiting on keeps ticking,
1136 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001138 * If @a thread is already suspended, the routine has no effect.
1139 *
1140 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141 *
1142 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001143 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001144 */
Andrew Boie468190a2017-09-29 14:00:48 -07001145__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146
1147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001148 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * This routine allows the kernel scheduler to make @a thread the current
1151 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001153 * If @a thread is not currently suspended, the routine has no effect.
1154 *
1155 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001156 *
1157 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001158 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 */
Andrew Boie468190a2017-09-29 14:00:48 -07001160__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001165 * This routine specifies how the scheduler will perform time slicing of
1166 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * To enable time slicing, @a slice must be non-zero. The scheduler
1169 * ensures that no thread runs for more than the specified time limit
1170 * before other threads of that priority are given a chance to execute.
1171 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001172 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175 * execute. Once the scheduler selects a thread for execution, there is no
1176 * minimum guaranteed time the thread will execute before threads of greater or
1177 * equal priority are scheduled.
1178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 * for execution, this routine has no effect; the thread is immediately
1181 * rescheduled after the slice period expires.
1182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * To disable timeslicing, set both @a slice and @a prio to zero.
1184 *
1185 * @param slice Maximum time slice length (in milliseconds).
1186 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001187 *
1188 * @return N/A
1189 */
Kumar Galacc334c72017-04-21 10:55:34 -05001190extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001191
Anas Nashif166f5192018-02-25 08:02:36 -06001192/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001193
1194/**
1195 * @addtogroup isr_apis
1196 * @{
1197 */
1198
1199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001200 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001201 *
Allan Stephensc98da842016-11-11 15:45:03 -05001202 * This routine allows the caller to customize its actions, depending on
1203 * whether it is a thread or an ISR.
1204 *
1205 * @note Can be called by ISRs.
1206 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001207 * @return false if invoked by a thread.
1208 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001210extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001211
Benjamin Walsh445830d2016-11-10 15:54:27 -05001212/**
1213 * @brief Determine if code is running in a preemptible thread.
1214 *
Allan Stephensc98da842016-11-11 15:45:03 -05001215 * This routine allows the caller to customize its actions, depending on
1216 * whether it can be preempted by another thread. The routine returns a 'true'
1217 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001218 *
Allan Stephensc98da842016-11-11 15:45:03 -05001219 * - The code is running in a thread, not at ISR.
1220 * - The thread's priority is in the preemptible range.
1221 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001222 *
Allan Stephensc98da842016-11-11 15:45:03 -05001223 * @note Can be called by ISRs.
1224 *
1225 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001226 * @return Non-zero if invoked by a preemptible thread.
1227 */
Andrew Boie468190a2017-09-29 14:00:48 -07001228__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001229
Allan Stephensc98da842016-11-11 15:45:03 -05001230/**
Anas Nashif166f5192018-02-25 08:02:36 -06001231 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001232 */
1233
1234/**
1235 * @addtogroup thread_apis
1236 * @{
1237 */
1238
1239/**
1240 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001241 *
Allan Stephensc98da842016-11-11 15:45:03 -05001242 * This routine prevents the current thread from being preempted by another
1243 * thread by instructing the scheduler to treat it as a cooperative thread.
1244 * If the thread subsequently performs an operation that makes it unready,
1245 * it will be context switched out in the normal manner. When the thread
1246 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001247 *
Allan Stephensc98da842016-11-11 15:45:03 -05001248 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001249 *
Allan Stephensc98da842016-11-11 15:45:03 -05001250 * @note k_sched_lock() and k_sched_unlock() should normally be used
1251 * when the operation being performed can be safely interrupted by ISRs.
1252 * However, if the amount of processing involved is very small, better
1253 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001254 *
1255 * @return N/A
1256 */
1257extern void k_sched_lock(void);
1258
Allan Stephensc98da842016-11-11 15:45:03 -05001259/**
1260 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001261 *
Allan Stephensc98da842016-11-11 15:45:03 -05001262 * This routine reverses the effect of a previous call to k_sched_lock().
1263 * A thread must call the routine once for each time it called k_sched_lock()
1264 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001265 *
1266 * @return N/A
1267 */
1268extern void k_sched_unlock(void);
1269
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001271 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001273 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * Custom data is not used by the kernel itself, and is freely available
1276 * for a thread to use as it sees fit. It can be used as a framework
1277 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
1281 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001282 *
1283 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 */
Andrew Boie468190a2017-09-29 14:00:48 -07001285__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286
1287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001293 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001294 */
Andrew Boie468190a2017-09-29 14:00:48 -07001295__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296
1297/**
Anas Nashif57554052018-03-03 02:31:05 -06001298 * @brief Set current thread name
1299 *
1300 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1301 * tracing and debugging.
1302 *
1303 */
1304__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1305
1306/**
1307 * @brief Get thread name
1308 *
1309 * Get the name of a thread
1310 *
1311 * @param thread_id Thread ID
1312 *
1313 */
1314__syscall const char *k_thread_name_get(k_tid_t thread_id);
1315
1316/**
Andy Rosscfe62032018-09-29 07:34:55 -07001317 * @}
1318 */
1319
1320/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001321 * @addtogroup clock_apis
1322 * @{
1323 */
1324
1325/**
1326 * @brief Generate null timeout delay.
1327 *
1328 * This macro generates a timeout delay that that instructs a kernel API
1329 * not to wait if the requested operation cannot be performed immediately.
1330 *
1331 * @return Timeout delay value.
1332 */
1333#define K_NO_WAIT 0
1334
1335/**
1336 * @brief Generate timeout delay from milliseconds.
1337 *
1338 * This macro generates a timeout delay that that instructs a kernel API
1339 * to wait up to @a ms milliseconds to perform the requested operation.
1340 *
1341 * @param ms Duration in milliseconds.
1342 *
1343 * @return Timeout delay value.
1344 */
Johan Hedberg14471692016-11-13 10:52:15 +02001345#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001346
1347/**
1348 * @brief Generate timeout delay from seconds.
1349 *
1350 * This macro generates a timeout delay that that instructs a kernel API
1351 * to wait up to @a s seconds to perform the requested operation.
1352 *
1353 * @param s Duration in seconds.
1354 *
1355 * @return Timeout delay value.
1356 */
Johan Hedberg14471692016-11-13 10:52:15 +02001357#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001358
1359/**
1360 * @brief Generate timeout delay from minutes.
1361 *
1362 * This macro generates a timeout delay that that instructs a kernel API
1363 * to wait up to @a m minutes to perform the requested operation.
1364 *
1365 * @param m Duration in minutes.
1366 *
1367 * @return Timeout delay value.
1368 */
Johan Hedberg14471692016-11-13 10:52:15 +02001369#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001370
1371/**
1372 * @brief Generate timeout delay from hours.
1373 *
1374 * This macro generates a timeout delay that that instructs a kernel API
1375 * to wait up to @a h hours to perform the requested operation.
1376 *
1377 * @param h Duration in hours.
1378 *
1379 * @return Timeout delay value.
1380 */
Johan Hedberg14471692016-11-13 10:52:15 +02001381#define K_HOURS(h) K_MINUTES((h) * 60)
1382
Allan Stephensc98da842016-11-11 15:45:03 -05001383/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001384 * @brief Generate infinite timeout delay.
1385 *
1386 * This macro generates a timeout delay that that instructs a kernel API
1387 * to wait as long as necessary to perform the requested operation.
1388 *
1389 * @return Timeout delay value.
1390 */
1391#define K_FOREVER (-1)
1392
1393/**
Anas Nashif166f5192018-02-25 08:02:36 -06001394 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001395 */
1396
1397/**
Allan Stephensc98da842016-11-11 15:45:03 -05001398 * @cond INTERNAL_HIDDEN
1399 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001400
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401struct k_timer {
1402 /*
1403 * _timeout structure must be first here if we want to use
1404 * dynamic timer allocation. timeout.node is used in the double-linked
1405 * list of free timers
1406 */
1407 struct _timeout timeout;
1408
Allan Stephens45bfa372016-10-12 12:39:42 -05001409 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410 _wait_q_t wait_q;
1411
1412 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001413 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414
1415 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001416 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417
1418 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001419 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001420
Allan Stephens45bfa372016-10-12 12:39:42 -05001421 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001422 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001424 /* user-specific data, also used to support legacy features */
1425 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001427 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428};
1429
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001430#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001432 .timeout = { \
1433 .node = {},\
1434 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001435 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001436 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001437 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001438 .expiry_fn = expiry, \
1439 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001440 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001441 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001442 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001443 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444 }
1445
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001446#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001447
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001448/**
Allan Stephensc98da842016-11-11 15:45:03 -05001449 * INTERNAL_HIDDEN @endcond
1450 */
1451
1452/**
1453 * @defgroup timer_apis Timer APIs
1454 * @ingroup kernel_apis
1455 * @{
1456 */
1457
1458/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001459 * @typedef k_timer_expiry_t
1460 * @brief Timer expiry function type.
1461 *
1462 * A timer's expiry function is executed by the system clock interrupt handler
1463 * each time the timer expires. The expiry function is optional, and is only
1464 * invoked if the timer has been initialized with one.
1465 *
1466 * @param timer Address of timer.
1467 *
1468 * @return N/A
1469 */
1470typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1471
1472/**
1473 * @typedef k_timer_stop_t
1474 * @brief Timer stop function type.
1475 *
1476 * A timer's stop function is executed if the timer is stopped prematurely.
1477 * The function runs in the context of the thread that stops the timer.
1478 * The stop function is optional, and is only invoked if the timer has been
1479 * initialized with one.
1480 *
1481 * @param timer Address of timer.
1482 *
1483 * @return N/A
1484 */
1485typedef void (*k_timer_stop_t)(struct k_timer *timer);
1486
1487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001491 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001492 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001493 *
1494 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @param expiry_fn Function to invoke each time the timer expires.
1496 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001498#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001499 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001500 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501
Allan Stephens45bfa372016-10-12 12:39:42 -05001502/**
1503 * @brief Initialize a timer.
1504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001506 *
1507 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001508 * @param expiry_fn Function to invoke each time the timer expires.
1509 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001510 *
1511 * @return N/A
1512 */
1513extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001514 k_timer_expiry_t expiry_fn,
1515 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001516
Allan Stephens45bfa372016-10-12 12:39:42 -05001517/**
1518 * @brief Start a timer.
1519 *
1520 * This routine starts a timer, and resets its status to zero. The timer
1521 * begins counting down using the specified duration and period values.
1522 *
1523 * Attempting to start a timer that is already running is permitted.
1524 * The timer's status is reset to zero and the timer begins counting down
1525 * using the new duration and period values.
1526 *
1527 * @param timer Address of timer.
1528 * @param duration Initial timer duration (in milliseconds).
1529 * @param period Timer period (in milliseconds).
1530 *
1531 * @return N/A
1532 */
Andrew Boiea354d492017-09-29 16:22:28 -07001533__syscall void k_timer_start(struct k_timer *timer,
1534 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001535
1536/**
1537 * @brief Stop a timer.
1538 *
1539 * This routine stops a running timer prematurely. The timer's stop function,
1540 * if one exists, is invoked by the caller.
1541 *
1542 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001543 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001544 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001545 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1546 * if @a k_timer_stop is to be called from ISRs.
1547 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001548 * @param timer Address of timer.
1549 *
1550 * @return N/A
1551 */
Andrew Boiea354d492017-09-29 16:22:28 -07001552__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001553
1554/**
1555 * @brief Read timer status.
1556 *
1557 * This routine reads the timer's status, which indicates the number of times
1558 * it has expired since its status was last read.
1559 *
1560 * Calling this routine resets the timer's status to zero.
1561 *
1562 * @param timer Address of timer.
1563 *
1564 * @return Timer status.
1565 */
Andrew Boiea354d492017-09-29 16:22:28 -07001566__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001567
1568/**
1569 * @brief Synchronize thread to timer expiration.
1570 *
1571 * This routine blocks the calling thread until the timer's status is non-zero
1572 * (indicating that it has expired at least once since it was last examined)
1573 * or the timer is stopped. If the timer status is already non-zero,
1574 * or the timer is already stopped, the caller continues without waiting.
1575 *
1576 * Calling this routine resets the timer's status to zero.
1577 *
1578 * This routine must not be used by interrupt handlers, since they are not
1579 * allowed to block.
1580 *
1581 * @param timer Address of timer.
1582 *
1583 * @return Timer status.
1584 */
Andrew Boiea354d492017-09-29 16:22:28 -07001585__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001586
Andy Ross52e444b2018-09-28 09:06:37 -07001587extern s32_t z_timeout_remaining(struct _timeout *timeout);
1588
Allan Stephens45bfa372016-10-12 12:39:42 -05001589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001591 *
1592 * This routine computes the (approximate) time remaining before a running
1593 * timer next expires. If the timer is not running, it returns zero.
1594 *
1595 * @param timer Address of timer.
1596 *
1597 * @return Remaining time (in milliseconds).
1598 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001599__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001600
Patrik Flykt4344e272019-03-08 14:19:05 -07001601static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001602{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001603 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1604 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001605}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001606
Allan Stephensc98da842016-11-11 15:45:03 -05001607/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001608 * @brief Associate user-specific data with a timer.
1609 *
1610 * This routine records the @a user_data with the @a timer, to be retrieved
1611 * later.
1612 *
1613 * It can be used e.g. in a timer handler shared across multiple subsystems to
1614 * retrieve data specific to the subsystem this timer is associated with.
1615 *
1616 * @param timer Address of timer.
1617 * @param user_data User data to associate with the timer.
1618 *
1619 * @return N/A
1620 */
Andrew Boiea354d492017-09-29 16:22:28 -07001621__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1622
Anas Nashif954d5502018-02-25 08:37:28 -06001623/**
1624 * @internal
1625 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001626static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001627 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001628{
1629 timer->user_data = user_data;
1630}
1631
1632/**
1633 * @brief Retrieve the user-specific data from a timer.
1634 *
1635 * @param timer Address of timer.
1636 *
1637 * @return The user data.
1638 */
Andrew Boiea354d492017-09-29 16:22:28 -07001639__syscall void *k_timer_user_data_get(struct k_timer *timer);
1640
Patrik Flykt4344e272019-03-08 14:19:05 -07001641static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001642{
1643 return timer->user_data;
1644}
1645
Anas Nashif166f5192018-02-25 08:02:36 -06001646/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001647
Allan Stephensc98da842016-11-11 15:45:03 -05001648/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001649 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001650 * @{
1651 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001652
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001654 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * This routine returns the elapsed time since the system booted,
1657 * in milliseconds.
1658 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001659 * @note
1660 * @rststar
1661 * While this function returns time in milliseconds, it does
1662 * not mean it has millisecond resolution. The actual resolution depends on
1663 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the
1664 * default setting of 100, system time is updated in increments of 10ms.
1665 * @endrststar
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001666 *
1667 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001668 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001669__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001670
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001671/**
1672 * @brief Enable clock always on in tickless kernel
1673 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001674 * This routine enables keeping the clock running (that is, it always
1675 * keeps an active timer interrupt scheduled) when there are no timer
1676 * events programmed in tickless kernel scheduling. This is necessary
1677 * if the clock is used to track passage of time (e.g. via
1678 * k_uptime_get_32()), otherwise the internal hardware counter may
1679 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001680 *
1681 * @retval prev_status Previous status of always on flag
1682 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001683int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001684
1685/**
1686 * @brief Disable clock always on in tickless kernel
1687 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001688 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001689 * there are no timer events programmed in tickless kernel
1690 * scheduling. To save power, this routine should be called
1691 * immediately when clock is not used to track time.
1692 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001693void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001694
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001698 * This routine returns the lower 32-bits of the elapsed time since the system
1699 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * This routine can be more efficient than k_uptime_get(), as it reduces the
1702 * need for interrupt locking and 64-bit math. However, the 32-bit result
1703 * cannot hold a system uptime time larger than approximately 50 days, so the
1704 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001706 * @note
1707 * @rststar
1708 * While this function returns time in milliseconds, it does
1709 * not mean it has millisecond resolution. The actual resolution depends on
1710 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the
1711 * default setting of 100, system time is updated in increments of 10ms.
1712 * @endrststar
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001713 *
1714 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001716__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001717
1718/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001719 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * This routine computes the elapsed time between the current system uptime
1722 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001724 * @param reftime Pointer to a reference time, which is updated to the current
1725 * uptime upon return.
1726 *
1727 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001728 */
Andy Ross987c0e52018-09-27 16:50:00 -07001729static inline s64_t k_uptime_delta(s64_t *reftime)
1730{
1731 s64_t uptime, delta;
1732
1733 uptime = k_uptime_get();
1734 delta = uptime - *reftime;
1735 *reftime = uptime;
1736
1737 return delta;
1738}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001739
1740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * This routine computes the elapsed time between the current system uptime
1744 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001746 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1747 * need for interrupt locking and 64-bit math. However, the 32-bit result
1748 * cannot hold an elapsed time larger than approximately 50 days, so the
1749 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @param reftime Pointer to a reference time, which is updated to the current
1752 * uptime upon return.
1753 *
1754 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001755 */
Andy Ross987c0e52018-09-27 16:50:00 -07001756static inline u32_t k_uptime_delta_32(s64_t *reftime)
1757{
1758 return (u32_t)k_uptime_delta(reftime);
1759}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001760
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001761/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001764 * This routine returns the current time, as measured by the system's hardware
1765 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001769#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001770
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001771/**
Anas Nashif166f5192018-02-25 08:02:36 -06001772 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001773 */
1774
Allan Stephensc98da842016-11-11 15:45:03 -05001775/**
1776 * @cond INTERNAL_HIDDEN
1777 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001778
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001779struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001780 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001781 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001782 union {
1783 _wait_q_t wait_q;
1784
1785 _POLL_EVENT;
1786 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001787
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001788 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001789};
1790
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001791#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001792 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001793 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001794 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001795 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001796 _OBJECT_TRACING_INIT \
1797 }
1798
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001799#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1800
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001801extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1802
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001803/**
1804 * INTERNAL_HIDDEN @endcond
1805 */
1806
1807/**
1808 * @defgroup queue_apis Queue APIs
1809 * @ingroup kernel_apis
1810 * @{
1811 */
1812
1813/**
1814 * @brief Initialize a queue.
1815 *
1816 * This routine initializes a queue object, prior to its first use.
1817 *
1818 * @param queue Address of the queue.
1819 *
1820 * @return N/A
1821 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001822__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001823
1824/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001825 * @brief Cancel waiting on a queue.
1826 *
1827 * This routine causes first thread pending on @a queue, if any, to
1828 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001829 * If the queue is being waited on by k_poll(), it will return with
1830 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1831 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001832 *
1833 * @note Can be called by ISRs.
1834 *
1835 * @param queue Address of the queue.
1836 *
1837 * @return N/A
1838 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001839__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001840
1841/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001842 * @brief Append an element to the end of a queue.
1843 *
1844 * This routine appends a data item to @a queue. A queue data item must be
1845 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1846 * reserved for the kernel's use.
1847 *
1848 * @note Can be called by ISRs.
1849 *
1850 * @param queue Address of the queue.
1851 * @param data Address of the data item.
1852 *
1853 * @return N/A
1854 */
1855extern void k_queue_append(struct k_queue *queue, void *data);
1856
1857/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001858 * @brief Append an element to a queue.
1859 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001860 * This routine appends a data item to @a queue. There is an implicit memory
1861 * allocation to create an additional temporary bookkeeping data structure from
1862 * the calling thread's resource pool, which is automatically freed when the
1863 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001864 *
1865 * @note Can be called by ISRs.
1866 *
1867 * @param queue Address of the queue.
1868 * @param data Address of the data item.
1869 *
1870 * @retval 0 on success
1871 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1872 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301873__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001874
1875/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001876 * @brief Prepend an element to a queue.
1877 *
1878 * This routine prepends a data item to @a queue. A queue data item must be
1879 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1880 * reserved for the kernel's use.
1881 *
1882 * @note Can be called by ISRs.
1883 *
1884 * @param queue Address of the queue.
1885 * @param data Address of the data item.
1886 *
1887 * @return N/A
1888 */
1889extern void k_queue_prepend(struct k_queue *queue, void *data);
1890
1891/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001892 * @brief Prepend an element to a queue.
1893 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001894 * This routine prepends a data item to @a queue. There is an implicit memory
1895 * allocation to create an additional temporary bookkeeping data structure from
1896 * the calling thread's resource pool, which is automatically freed when the
1897 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001898 *
1899 * @note Can be called by ISRs.
1900 *
1901 * @param queue Address of the queue.
1902 * @param data Address of the data item.
1903 *
1904 * @retval 0 on success
1905 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1906 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301907__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001908
1909/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001910 * @brief Inserts an element to a queue.
1911 *
1912 * This routine inserts a data item to @a queue after previous item. A queue
1913 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1914 * item are reserved for the kernel's use.
1915 *
1916 * @note Can be called by ISRs.
1917 *
1918 * @param queue Address of the queue.
1919 * @param prev Address of the previous data item.
1920 * @param data Address of the data item.
1921 *
1922 * @return N/A
1923 */
1924extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1925
1926/**
1927 * @brief Atomically append a list of elements to a queue.
1928 *
1929 * This routine adds a list of data items to @a queue in one operation.
1930 * The data items must be in a singly-linked list, with the first 32 bits
1931 * in each data item pointing to the next data item; the list must be
1932 * NULL-terminated.
1933 *
1934 * @note Can be called by ISRs.
1935 *
1936 * @param queue Address of the queue.
1937 * @param head Pointer to first node in singly-linked list.
1938 * @param tail Pointer to last node in singly-linked list.
1939 *
1940 * @return N/A
1941 */
1942extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1943
1944/**
1945 * @brief Atomically add a list of elements to a queue.
1946 *
1947 * This routine adds a list of data items to @a queue in one operation.
1948 * The data items must be in a singly-linked list implemented using a
1949 * sys_slist_t object. Upon completion, the original list is empty.
1950 *
1951 * @note Can be called by ISRs.
1952 *
1953 * @param queue Address of the queue.
1954 * @param list Pointer to sys_slist_t object.
1955 *
1956 * @return N/A
1957 */
1958extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1959
1960/**
1961 * @brief Get an element from a queue.
1962 *
1963 * This routine removes first data item from @a queue. The first 32 bits of the
1964 * data item are reserved for the kernel's use.
1965 *
1966 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1967 *
1968 * @param queue Address of the queue.
1969 * @param timeout Waiting period to obtain a data item (in milliseconds),
1970 * or one of the special values K_NO_WAIT and K_FOREVER.
1971 *
1972 * @return Address of the data item if successful; NULL if returned
1973 * without waiting, or waiting period timed out.
1974 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001975__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001976
1977/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001978 * @brief Remove an element from a queue.
1979 *
1980 * This routine removes data item from @a queue. The first 32 bits of the
1981 * data item are reserved for the kernel's use. Removing elements from k_queue
1982 * rely on sys_slist_find_and_remove which is not a constant time operation.
1983 *
1984 * @note Can be called by ISRs
1985 *
1986 * @param queue Address of the queue.
1987 * @param data Address of the data item.
1988 *
1989 * @return true if data item was removed
1990 */
1991static inline bool k_queue_remove(struct k_queue *queue, void *data)
1992{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001993 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001994}
1995
1996/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001997 * @brief Append an element to a queue only if it's not present already.
1998 *
1999 * This routine appends data item to @a queue. The first 32 bits of the
2000 * data item are reserved for the kernel's use. Appending elements to k_queue
2001 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2002 *
2003 * @note Can be called by ISRs
2004 *
2005 * @param queue Address of the queue.
2006 * @param data Address of the data item.
2007 *
2008 * @return true if data item was added, false if not
2009 */
2010static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2011{
2012 sys_sfnode_t *test;
2013
2014 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2015 if (test == (sys_sfnode_t *) data) {
2016 return false;
2017 }
2018 }
2019
2020 k_queue_append(queue, data);
2021 return true;
2022}
2023
2024/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002025 * @brief Query a queue to see if it has data available.
2026 *
2027 * Note that the data might be already gone by the time this function returns
2028 * if other threads are also trying to read from the queue.
2029 *
2030 * @note Can be called by ISRs.
2031 *
2032 * @param queue Address of the queue.
2033 *
2034 * @return Non-zero if the queue is empty.
2035 * @return 0 if data is available.
2036 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002037__syscall int k_queue_is_empty(struct k_queue *queue);
2038
Patrik Flykt4344e272019-03-08 14:19:05 -07002039static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002040{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002041 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002042}
2043
2044/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002045 * @brief Peek element at the head of queue.
2046 *
2047 * Return element from the head of queue without removing it.
2048 *
2049 * @param queue Address of the queue.
2050 *
2051 * @return Head element, or NULL if queue is empty.
2052 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002053__syscall void *k_queue_peek_head(struct k_queue *queue);
2054
Patrik Flykt4344e272019-03-08 14:19:05 -07002055static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002056{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002057 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002058}
2059
2060/**
2061 * @brief Peek element at the tail of queue.
2062 *
2063 * Return element from the tail of queue without removing it.
2064 *
2065 * @param queue Address of the queue.
2066 *
2067 * @return Tail element, or NULL if queue is empty.
2068 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002069__syscall void *k_queue_peek_tail(struct k_queue *queue);
2070
Patrik Flykt4344e272019-03-08 14:19:05 -07002071static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002072{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002073 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002074}
2075
2076/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002077 * @brief Statically define and initialize a queue.
2078 *
2079 * The queue can be accessed outside the module where it is defined using:
2080 *
2081 * @code extern struct k_queue <name>; @endcode
2082 *
2083 * @param name Name of the queue.
2084 */
2085#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002086 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002087 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002088
Anas Nashif166f5192018-02-25 08:02:36 -06002089/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002090
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002091struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002092 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002093};
2094
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002095/**
2096 * @cond INTERNAL_HIDDEN
2097 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002098#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002099 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002100 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002101 }
2102
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002103#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002104
Allan Stephensc98da842016-11-11 15:45:03 -05002105/**
2106 * INTERNAL_HIDDEN @endcond
2107 */
2108
2109/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002110 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002111 * @ingroup kernel_apis
2112 * @{
2113 */
2114
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002116 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002118 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002120 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002121 *
2122 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002123 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002125#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002126 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127
2128/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002129 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002130 *
2131 * This routine causes first thread pending on @a fifo, if any, to
2132 * return from k_fifo_get() call with NULL value (as if timeout
2133 * expired).
2134 *
2135 * @note Can be called by ISRs.
2136 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002137 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002138 *
2139 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002140 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002141 */
2142#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002143 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002144
2145/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002146 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002147 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2150 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002152 * @note Can be called by ISRs.
2153 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002154 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002155 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156 *
2157 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002158 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002159 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002160#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002161 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162
2163/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002164 * @brief Add an element to a FIFO queue.
2165 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002166 * This routine adds a data item to @a fifo. There is an implicit memory
2167 * allocation to create an additional temporary bookkeeping data structure from
2168 * the calling thread's resource pool, which is automatically freed when the
2169 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002170 *
2171 * @note Can be called by ISRs.
2172 *
2173 * @param fifo Address of the FIFO.
2174 * @param data Address of the data item.
2175 *
2176 * @retval 0 on success
2177 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002178 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002179 */
2180#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002181 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002182
2183/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002184 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002186 * This routine adds a list of data items to @a fifo in one operation.
2187 * The data items must be in a singly-linked list, with the first 32 bits
2188 * each data item pointing to the next data item; the list must be
2189 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002191 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002193 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002194 * @param head Pointer to first node in singly-linked list.
2195 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002196 *
2197 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002198 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002199 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002200#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002201 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002202
2203/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002206 * This routine adds a list of data items to @a fifo in one operation.
2207 * The data items must be in a singly-linked list implemented using a
2208 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002209 * and must be re-initialized via sys_slist_init().
2210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002211 * @note Can be called by ISRs.
2212 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002214 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215 *
2216 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002217 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002219#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002220 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221
2222/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002223 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002225 * This routine removes a data item from @a fifo in a "first in, first out"
2226 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002228 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2229 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002230 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 * or one of the special values K_NO_WAIT and K_FOREVER.
2233 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002234 * @return Address of the data item if successful; NULL if returned
2235 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002236 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002237 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002238#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002239 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002242 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002243 *
2244 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002245 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002246 *
2247 * @note Can be called by ISRs.
2248 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002249 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002250 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002251 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002252 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002253 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002254 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002255#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002256 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002257
2258/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002259 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002260 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302262 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002263 * on each iteration of processing, a head container will be peeked,
2264 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002265 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002266 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002267 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002268 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002270 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002271 */
2272#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002273 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002274
2275/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002276 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002277 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002278 * Return element from the tail of FIFO queue (without removing it). A usecase
2279 * for this is if elements of the FIFO queue are themselves containers. Then
2280 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002281 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002282 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002283 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002284 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002285 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002286 */
2287#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002288 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002289
2290/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002291 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002293 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002295 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002297 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002298 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002301 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002302 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303
Anas Nashif166f5192018-02-25 08:02:36 -06002304/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002305
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002307 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308};
2309
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002310/**
2311 * @cond INTERNAL_HIDDEN
2312 */
2313
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002314#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002315 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002316 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002317 }
2318
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002319#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2320
Allan Stephensc98da842016-11-11 15:45:03 -05002321/**
2322 * INTERNAL_HIDDEN @endcond
2323 */
2324
2325/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002326 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002327 * @ingroup kernel_apis
2328 * @{
2329 */
2330
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002332 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002334 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002335 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002336 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 *
2338 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002339 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002341#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002342 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343
2344/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002345 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002347 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2349 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * @note Can be called by ISRs.
2352 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002353 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
2356 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002357 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002358 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002359#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002360 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361
2362/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002363 * @brief Add an element to a LIFO queue.
2364 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002365 * This routine adds a data item to @a lifo. There is an implicit memory
2366 * allocation to create an additional temporary bookkeeping data structure from
2367 * the calling thread's resource pool, which is automatically freed when the
2368 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002369 *
2370 * @note Can be called by ISRs.
2371 *
2372 * @param lifo Address of the LIFO.
2373 * @param data Address of the data item.
2374 *
2375 * @retval 0 on success
2376 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002377 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002378 */
2379#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002380 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002381
2382/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002383 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 * This routine removes a data item from @a lifo in a "last in, first out"
2386 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002388 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2389 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002390 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 * or one of the special values K_NO_WAIT and K_FOREVER.
2393 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002394 * @return Address of the data item if successful; NULL if returned
2395 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002396 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002397 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002398#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002399 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002400
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002402 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002404 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002405 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002406 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002409 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002412 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002413 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414
Anas Nashif166f5192018-02-25 08:02:36 -06002415/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002416
2417/**
2418 * @cond INTERNAL_HIDDEN
2419 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302420#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002421
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002422typedef uintptr_t stack_data_t;
2423
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424struct k_stack {
2425 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002426 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002427 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002429 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002430 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431};
2432
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002433#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002434 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002435 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002436 .base = stack_buffer, \
2437 .next = stack_buffer, \
2438 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002439 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002440 }
2441
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002442#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2443
Allan Stephensc98da842016-11-11 15:45:03 -05002444/**
2445 * INTERNAL_HIDDEN @endcond
2446 */
2447
2448/**
2449 * @defgroup stack_apis Stack APIs
2450 * @ingroup kernel_apis
2451 * @{
2452 */
2453
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454/**
2455 * @brief Initialize a stack.
2456 *
2457 * This routine initializes a stack object, prior to its first use.
2458 *
2459 * @param stack Address of the stack.
2460 * @param buffer Address of array used to hold stacked values.
2461 * @param num_entries Maximum number of values that can be stacked.
2462 *
2463 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002464 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 */
Andrew Boief3bee952018-05-02 17:44:39 -07002466void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002467 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002468
2469
2470/**
2471 * @brief Initialize a stack.
2472 *
2473 * This routine initializes a stack object, prior to its first use. Internal
2474 * buffers will be allocated from the calling thread's resource pool.
2475 * This memory will be released if k_stack_cleanup() is called, or
2476 * userspace is enabled and the stack object loses all references to it.
2477 *
2478 * @param stack Address of the stack.
2479 * @param num_entries Maximum number of values that can be stacked.
2480 *
2481 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002482 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002483 */
2484
Adithya Baglody28080d32018-10-15 11:48:51 +05302485__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2486 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002487
2488/**
2489 * @brief Release a stack's allocated buffer
2490 *
2491 * If a stack object was given a dynamically allocated buffer via
2492 * k_stack_alloc_init(), this will free it. This function does nothing
2493 * if the buffer wasn't dynamically allocated.
2494 *
2495 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002496 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002497 */
2498void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499
2500/**
2501 * @brief Push an element onto a stack.
2502 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002503 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 *
2505 * @note Can be called by ISRs.
2506 *
2507 * @param stack Address of the stack.
2508 * @param data Value to push onto the stack.
2509 *
2510 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002511 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002513__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514
2515/**
2516 * @brief Pop an element from a stack.
2517 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002518 * This routine removes a stack_data_t value from @a stack in a "last in,
2519 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 *
2521 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2522 *
2523 * @param stack Address of the stack.
2524 * @param data Address of area to hold the value popped from the stack.
2525 * @param timeout Waiting period to obtain a value (in milliseconds),
2526 * or one of the special values K_NO_WAIT and K_FOREVER.
2527 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002528 * @retval 0 Element popped from stack.
2529 * @retval -EBUSY Returned without waiting.
2530 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002531 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002533__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002538 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002540 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002542 * @param name Name of the stack.
2543 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002544 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002545 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002546#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002547 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002548 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002549 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002550 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002551 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552
Anas Nashif166f5192018-02-25 08:02:36 -06002553/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002554
Allan Stephens6bba9b02016-11-16 14:56:54 -05002555struct k_work;
2556
Allan Stephensc98da842016-11-11 15:45:03 -05002557/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002558 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002559 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560 */
2561
Allan Stephens6bba9b02016-11-16 14:56:54 -05002562/**
2563 * @typedef k_work_handler_t
2564 * @brief Work item handler function type.
2565 *
2566 * A work item's handler function is executed by a workqueue's thread
2567 * when the work item is processed by the workqueue.
2568 *
2569 * @param work Address of the work item.
2570 *
2571 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002572 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 */
2574typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575
2576/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002577 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002579
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002581 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002582 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583};
2584
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002586 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587};
2588
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002590 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591 k_work_handler_t handler;
2592 atomic_t flags[1];
2593};
2594
Allan Stephens6bba9b02016-11-16 14:56:54 -05002595struct k_delayed_work {
2596 struct k_work work;
2597 struct _timeout timeout;
2598 struct k_work_q *work_q;
2599};
2600
2601extern struct k_work_q k_sys_work_q;
2602
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002604 * INTERNAL_HIDDEN @endcond
2605 */
2606
Patrik Flykt4344e272019-03-08 14:19:05 -07002607#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002608 { \
2609 ._reserved = NULL, \
2610 .handler = work_handler, \
2611 .flags = { 0 } \
2612 }
2613
Patrik Flykt4344e272019-03-08 14:19:05 -07002614#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002615
Allan Stephens6bba9b02016-11-16 14:56:54 -05002616/**
2617 * @brief Initialize a statically-defined work item.
2618 *
2619 * This macro can be used to initialize a statically-defined workqueue work
2620 * item, prior to its first use. For example,
2621 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002622 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002623 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002624 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002626 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002628#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002629 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630
2631/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002632 * @brief Initialize a work item.
2633 *
2634 * This routine initializes a workqueue work item, prior to its first use.
2635 *
2636 * @param work Address of work item.
2637 * @param handler Function to invoke each time work item is processed.
2638 *
2639 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002640 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641 */
2642static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2643{
Patrik Flykt4344e272019-03-08 14:19:05 -07002644 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645}
2646
2647/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002648 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002649 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002650 * This routine submits work item @a work to be processed by workqueue
2651 * @a work_q. If the work item is already pending in the workqueue's queue
2652 * as a result of an earlier submission, this routine has no effect on the
2653 * work item. If the work item has already been processed, or is currently
2654 * being processed, its work is considered complete and the work item can be
2655 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002656 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002657 * @warning
2658 * A submitted work item must not be modified until it has been processed
2659 * by the workqueue.
2660 *
2661 * @note Can be called by ISRs.
2662 *
2663 * @param work_q Address of workqueue.
2664 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002665 *
2666 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002667 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668 */
2669static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2670 struct k_work *work)
2671{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002672 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002673 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674 }
2675}
2676
2677/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002678 * @brief Submit a work item to a user mode workqueue
2679 *
David B. Kinder06d78352018-12-17 14:32:40 -08002680 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002681 * memory allocation is made from the caller's resource pool which is freed
2682 * once the worker thread consumes the k_work item. The workqueue
2683 * thread must have memory access to the k_work item being submitted. The caller
2684 * must have permission granted on the work_q parameter's queue object.
2685 *
2686 * Otherwise this works the same as k_work_submit_to_queue().
2687 *
2688 * @note Can be called by ISRs.
2689 *
2690 * @param work_q Address of workqueue.
2691 * @param work Address of work item.
2692 *
2693 * @retval -EBUSY if the work item was already in some workqueue
2694 * @retval -ENOMEM if no memory for thread resource pool allocation
2695 * @retval 0 Success
2696 * @req K-WORK-001
2697 */
2698static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2699 struct k_work *work)
2700{
2701 int ret = -EBUSY;
2702
2703 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2704 ret = k_queue_alloc_append(&work_q->queue, work);
2705
2706 /* Couldn't insert into the queue. Clear the pending bit
2707 * so the work item can be submitted again
2708 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002709 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002710 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2711 }
2712 }
2713
2714 return ret;
2715}
2716
2717/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002719 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720 * This routine indicates if work item @a work is pending in a workqueue's
2721 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002722 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002723 * @note Can be called by ISRs.
2724 *
2725 * @param work Address of work item.
2726 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002727 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002728 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002729 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002730static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002731{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002732 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002733}
2734
2735/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002736 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002738 * This routine starts workqueue @a work_q. The workqueue spawns its work
2739 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002741 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002742 * @param stack Pointer to work queue thread's stack space, as defined by
2743 * K_THREAD_STACK_DEFINE()
2744 * @param stack_size Size of the work queue thread's stack (in bytes), which
2745 * should either be the same constant passed to
2746 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002747 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002748 *
2749 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002750 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751 */
Andrew Boie507852a2017-07-25 18:47:07 -07002752extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002753 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002754 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002756/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002757 * @brief Start a workqueue in user mode
2758 *
2759 * This works identically to k_work_q_start() except it is callable from user
2760 * mode, and the worker thread created will run in user mode.
2761 * The caller must have permissions granted on both the work_q parameter's
2762 * thread and queue objects, and the same restrictions on priority apply as
2763 * k_thread_create().
2764 *
2765 * @param work_q Address of workqueue.
2766 * @param stack Pointer to work queue thread's stack space, as defined by
2767 * K_THREAD_STACK_DEFINE()
2768 * @param stack_size Size of the work queue thread's stack (in bytes), which
2769 * should either be the same constant passed to
2770 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2771 * @param prio Priority of the work queue's thread.
2772 *
2773 * @return N/A
2774 * @req K-WORK-001
2775 */
2776extern void k_work_q_user_start(struct k_work_q *work_q,
2777 k_thread_stack_t *stack,
2778 size_t stack_size, int prio);
2779
2780/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002781 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002783 * This routine initializes a workqueue delayed work item, prior to
2784 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002785 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002786 * @param work Address of delayed work item.
2787 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002788 *
2789 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002790 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002792extern void k_delayed_work_init(struct k_delayed_work *work,
2793 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794
2795/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002796 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002798 * This routine schedules work item @a work to be processed by workqueue
2799 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002800 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002801 * Only when the countdown completes is the work item actually submitted to
2802 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002804 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002805 * counting down cancels the existing submission and restarts the
2806 * countdown using the new delay. Note that this behavior is
2807 * inherently subject to race conditions with the pre-existing
2808 * timeouts and work queue, so care must be taken to synchronize such
2809 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002810 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002811 * @warning
2812 * A delayed work item must not be modified until it has been processed
2813 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002814 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002815 * @note Can be called by ISRs.
2816 *
2817 * @param work_q Address of workqueue.
2818 * @param work Address of delayed work item.
2819 * @param delay Delay before submitting the work item (in milliseconds).
2820 *
2821 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002822 * @retval -EINVAL Work item is being processed or has completed its work.
2823 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002824 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002825 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002826extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2827 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002828 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829
2830/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002831 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002833 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002834 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002835 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002837 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002839 * @note The result of calling this on a k_delayed_work item that has
2840 * not been submitted (i.e. before the return of the
2841 * k_delayed_work_submit_to_queue() call) is undefined.
2842 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002843 * @param work Address of delayed work item.
2844 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002845 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002846 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002847 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002849extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002850
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002852 * @brief Submit a work item to the system workqueue.
2853 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002854 * This routine submits work item @a work to be processed by the system
2855 * workqueue. If the work item is already pending in the workqueue's queue
2856 * as a result of an earlier submission, this routine has no effect on the
2857 * work item. If the work item has already been processed, or is currently
2858 * being processed, its work is considered complete and the work item can be
2859 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002861 * @warning
2862 * Work items submitted to the system workqueue should avoid using handlers
2863 * that block or yield since this may prevent the system workqueue from
2864 * processing other work items in a timely manner.
2865 *
2866 * @note Can be called by ISRs.
2867 *
2868 * @param work Address of work item.
2869 *
2870 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002871 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872 */
2873static inline void k_work_submit(struct k_work *work)
2874{
2875 k_work_submit_to_queue(&k_sys_work_q, work);
2876}
2877
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879 * @brief Submit a delayed work item to the system workqueue.
2880 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002881 * This routine schedules work item @a work to be processed by the system
2882 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002883 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002884 * Only when the countdown completes is the work item actually submitted to
2885 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002886 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002887 * Submitting a previously submitted delayed work item that is still
2888 * counting down cancels the existing submission and restarts the countdown
2889 * using the new delay. If the work item is currently pending on the
2890 * workqueue's queue because the countdown has completed it is too late to
2891 * resubmit the item, and resubmission fails without impacting the work item.
2892 * If the work item has already been processed, or is currently being processed,
2893 * its work is considered complete and the work item can be resubmitted.
2894 *
2895 * @warning
2896 * Work items submitted to the system workqueue should avoid using handlers
2897 * that block or yield since this may prevent the system workqueue from
2898 * processing other work items in a timely manner.
2899 *
2900 * @note Can be called by ISRs.
2901 *
2902 * @param work Address of delayed work item.
2903 * @param delay Delay before submitting the work item (in milliseconds).
2904 *
2905 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002906 * @retval -EINVAL Work item is being processed or has completed its work.
2907 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002908 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002909 */
2910static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002911 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002913 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914}
2915
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002917 * @brief Get time remaining before a delayed work gets scheduled.
2918 *
2919 * This routine computes the (approximate) time remaining before a
2920 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002921 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002922 *
2923 * @param work Delayed work item.
2924 *
2925 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002926 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002927 */
Kumar Galacc334c72017-04-21 10:55:34 -05002928static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002929{
Andy Ross52e444b2018-09-28 09:06:37 -07002930 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002931}
2932
Anas Nashif166f5192018-02-25 08:02:36 -06002933/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002934/**
Anas Nashifce78d162018-05-24 12:43:11 -05002935 * @defgroup mutex_apis Mutex APIs
2936 * @ingroup kernel_apis
2937 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002938 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002939
Anas Nashifce78d162018-05-24 12:43:11 -05002940/**
2941 * Mutex Structure
2942 * @ingroup mutex_apis
2943 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944struct k_mutex {
2945 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002946 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002947 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002948 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002951 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002952};
2953
Anas Nashifce78d162018-05-24 12:43:11 -05002954/**
2955 * @cond INTERNAL_HIDDEN
2956 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002957#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002958 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002959 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960 .owner = NULL, \
2961 .lock_count = 0, \
2962 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002963 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002964 }
2965
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002966#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2967
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002968/**
Allan Stephensc98da842016-11-11 15:45:03 -05002969 * INTERNAL_HIDDEN @endcond
2970 */
2971
2972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002977 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002980 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002981 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002983 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002984 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * Upon completion, the mutex is available and does not have an owner.
2992 *
2993 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002994 *
2995 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002996 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002997 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002998__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002999
3000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003003 * This routine locks @a mutex. If the mutex is locked by another thread,
3004 * the calling thread waits until the mutex becomes available or until
3005 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * A thread is permitted to lock a mutex it has already locked. The operation
3008 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @param mutex Address of the mutex.
3011 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003012 * or one of the special values K_NO_WAIT and K_FOREVER.
3013 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003014 * @retval 0 Mutex locked.
3015 * @retval -EBUSY Returned without waiting.
3016 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003017 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003018 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003019__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003020
3021/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * This routine unlocks @a mutex. The mutex must already be locked by the
3025 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003026 *
3027 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003029 * thread.
3030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003032 *
3033 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003034 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003035 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003036__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037
Allan Stephensc98da842016-11-11 15:45:03 -05003038/**
Anas Nashif166f5192018-02-25 08:02:36 -06003039 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003040 */
3041
3042/**
3043 * @cond INTERNAL_HIDDEN
3044 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045
3046struct k_sem {
3047 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303048 u32_t count;
3049 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003050 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003051
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003052 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053};
3054
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003055#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003056 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003057 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003058 .count = initial_count, \
3059 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003060 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003061 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003062 }
3063
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003064#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003065
Allan Stephensc98da842016-11-11 15:45:03 -05003066/**
3067 * INTERNAL_HIDDEN @endcond
3068 */
3069
3070/**
3071 * @defgroup semaphore_apis Semaphore APIs
3072 * @ingroup kernel_apis
3073 * @{
3074 */
3075
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003076/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003077 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003081 * @param sem Address of the semaphore.
3082 * @param initial_count Initial semaphore count.
3083 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003084 *
3085 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003086 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003087 */
Andrew Boie99280232017-09-29 14:17:47 -07003088__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3089 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003090
3091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003094 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3097 *
3098 * @param sem Address of the semaphore.
3099 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003100 * or one of the special values K_NO_WAIT and K_FOREVER.
3101 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003102 * @note When porting code from the nanokernel legacy API to the new API, be
3103 * careful with the return value of this function. The return value is the
3104 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3105 * non-zero means failure, while the nano_sem_take family returns 1 for success
3106 * and 0 for failure.
3107 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003108 * @retval 0 Semaphore taken.
3109 * @retval -EBUSY Returned without waiting.
3110 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003111 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003112 */
Andrew Boie99280232017-09-29 14:17:47 -07003113__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003114
3115/**
3116 * @brief Give a semaphore.
3117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * This routine gives @a sem, unless the semaphore is already at its maximum
3119 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * @note Can be called by ISRs.
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 *
3125 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003126 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003127 */
Andrew Boie99280232017-09-29 14:17:47 -07003128__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003130/**
3131 * @brief Reset a semaphore's count to zero.
3132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003136 *
3137 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003138 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003139 */
Andrew Boie990bf162017-10-03 12:36:49 -07003140__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003141
Anas Nashif954d5502018-02-25 08:37:28 -06003142/**
3143 * @internal
3144 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003145static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146{
Patrik Flykt24d71432019-03-26 19:57:45 -06003147 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003148}
3149
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003150/**
3151 * @brief Get a semaphore's count.
3152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003153 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003158 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003159 */
Andrew Boie990bf162017-10-03 12:36:49 -07003160__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003161
Anas Nashif954d5502018-02-25 08:37:28 -06003162/**
3163 * @internal
3164 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003165static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166{
3167 return sem->count;
3168}
3169
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003171 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003173 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003174 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003175 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * @param name Name of the semaphore.
3178 * @param initial_count Initial semaphore count.
3179 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003180 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003183 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003184 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303185 BUILD_ASSERT(((count_limit) != 0) && \
3186 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003187
Anas Nashif166f5192018-02-25 08:02:36 -06003188/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003189
3190/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003191 * @defgroup msgq_apis Message Queue APIs
3192 * @ingroup kernel_apis
3193 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003194 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003195
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003196/**
3197 * @brief Message Queue Structure
3198 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003199struct k_msgq {
3200 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003201 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003202 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003203 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204 char *buffer_start;
3205 char *buffer_end;
3206 char *read_ptr;
3207 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003208 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003210 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003211 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003213/**
3214 * @cond INTERNAL_HIDDEN
3215 */
3216
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003218#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003220 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003221 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003222 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003224 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225 .read_ptr = q_buffer, \
3226 .write_ptr = q_buffer, \
3227 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003228 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003230#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003231/**
3232 * INTERNAL_HIDDEN @endcond
3233 */
3234
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003235
Andrew Boie0fe789f2018-04-12 18:35:56 -07003236#define K_MSGQ_FLAG_ALLOC BIT(0)
3237
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003238/**
3239 * @brief Message Queue Attributes
3240 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303241struct k_msgq_attrs {
3242 size_t msg_size;
3243 u32_t max_msgs;
3244 u32_t used_msgs;
3245};
3246
Allan Stephensc98da842016-11-11 15:45:03 -05003247
3248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3252 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003253 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3254 * message is similarly aligned to this boundary, @a q_msg_size must also be
3255 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * The message queue can be accessed outside the module where it is defined
3258 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003260 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * @param q_name Name of the message queue.
3263 * @param q_msg_size Message size (in bytes).
3264 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003265 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003266 *
3267 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003268 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003269#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3270 static char __noinit __aligned(q_align) \
3271 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3272 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3273 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003274 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275
Peter Mitsisd7a37502016-10-13 11:37:40 -04003276/**
3277 * @brief Initialize a message queue.
3278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * This routine initializes a message queue object, prior to its first use.
3280 *
Allan Stephensda827222016-11-09 14:23:58 -06003281 * The message queue's ring buffer must contain space for @a max_msgs messages,
3282 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3283 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3284 * that each message is similarly aligned to this boundary, @a q_msg_size
3285 * must also be a multiple of N.
3286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * @param q Address of the message queue.
3288 * @param buffer Pointer to ring buffer that holds queued messages.
3289 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003290 * @param max_msgs Maximum number of messages that can be queued.
3291 *
3292 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003293 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003294 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003295void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3296 u32_t max_msgs);
3297
3298/**
3299 * @brief Initialize a message queue.
3300 *
3301 * This routine initializes a message queue object, prior to its first use,
3302 * allocating its internal ring buffer from the calling thread's resource
3303 * pool.
3304 *
3305 * Memory allocated for the ring buffer can be released by calling
3306 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3307 * all of its references.
3308 *
3309 * @param q Address of the message queue.
3310 * @param msg_size Message size (in bytes).
3311 * @param max_msgs Maximum number of messages that can be queued.
3312 *
3313 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3314 * thread's resource pool, or -EINVAL if the size parameters cause
3315 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003316 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003317 */
3318__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3319 u32_t max_msgs);
3320
3321
3322void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003323
3324/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003325 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003328 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003329 * @note Can be called by ISRs.
3330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * @param q Address of the message queue.
3332 * @param data Pointer to the message.
3333 * @param timeout Waiting period to add the message (in milliseconds),
3334 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003335 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003336 * @retval 0 Message sent.
3337 * @retval -ENOMSG Returned without waiting or queue purged.
3338 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003339 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003340 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003341__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003342
3343/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * This routine receives a message from message queue @a q in a "first in,
3347 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003348 *
Allan Stephensc98da842016-11-11 15:45:03 -05003349 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @param q Address of the message queue.
3352 * @param data Address of area to hold the received message.
3353 * @param timeout Waiting period to receive the message (in milliseconds),
3354 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003355 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003356 * @retval 0 Message received.
3357 * @retval -ENOMSG Returned without waiting.
3358 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003359 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003361__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003362
3363/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003364 * @brief Peek/read a message from a message queue.
3365 *
3366 * This routine reads a message from message queue @a q in a "first in,
3367 * first out" manner and leaves the message in the queue.
3368 *
3369 * @note Can be called by ISRs.
3370 *
3371 * @param q Address of the message queue.
3372 * @param data Address of area to hold the message read from the queue.
3373 *
3374 * @retval 0 Message read.
3375 * @retval -ENOMSG Returned when the queue has no message.
3376 * @req K-MSGQ-002
3377 */
3378__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3379
3380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * This routine discards all unreceived messages in a message queue's ring
3384 * buffer. Any threads that are blocked waiting to send a message to the
3385 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003388 *
3389 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003390 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003391 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003392__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003393
Peter Mitsis67be2492016-10-07 11:44:34 -04003394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * This routine returns the number of unused entries in a message queue's
3398 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * @param q Address of the message queue.
3401 *
3402 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003403 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003404 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003405__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3406
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303407/**
3408 * @brief Get basic attributes of a message queue.
3409 *
3410 * This routine fetches basic attributes of message queue into attr argument.
3411 *
3412 * @param q Address of the message queue.
3413 * @param attrs pointer to message queue attribute structure.
3414 *
3415 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003416 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303417 */
3418__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3419
3420
Patrik Flykt4344e272019-03-08 14:19:05 -07003421static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003422{
3423 return q->max_msgs - q->used_msgs;
3424}
3425
Peter Mitsisd7a37502016-10-13 11:37:40 -04003426/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003427 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003431 * @param q Address of the message queue.
3432 *
3433 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003434 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003435 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003436__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3437
Patrik Flykt4344e272019-03-08 14:19:05 -07003438static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439{
3440 return q->used_msgs;
3441}
3442
Anas Nashif166f5192018-02-25 08:02:36 -06003443/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003444
3445/**
3446 * @defgroup mem_pool_apis Memory Pool APIs
3447 * @ingroup kernel_apis
3448 * @{
3449 */
3450
Andy Ross73cb9582017-05-09 10:42:39 -07003451/* Note on sizing: the use of a 20 bit field for block means that,
3452 * assuming a reasonable minimum block size of 16 bytes, we're limited
3453 * to 16M of memory managed by a single pool. Long term it would be
3454 * good to move to a variable bit size based on configuration.
3455 */
3456struct k_mem_block_id {
3457 u32_t pool : 8;
3458 u32_t level : 4;
3459 u32_t block : 20;
3460};
3461
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003462struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003463 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003464 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465};
3466
Anas Nashif166f5192018-02-25 08:02:36 -06003467/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003468
3469/**
3470 * @defgroup mailbox_apis Mailbox APIs
3471 * @ingroup kernel_apis
3472 * @{
3473 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474
3475struct k_mbox_msg {
3476 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003477 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003478 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003479 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003480 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003481 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482 /** sender's message data buffer */
3483 void *tx_data;
3484 /** internal use only - needed for legacy API support */
3485 void *_rx_data;
3486 /** message data block descriptor */
3487 struct k_mem_block tx_block;
3488 /** source thread id */
3489 k_tid_t rx_source_thread;
3490 /** target thread id */
3491 k_tid_t tx_target_thread;
3492 /** internal use only - thread waiting on send (may be a dummy) */
3493 k_tid_t _syncing_thread;
3494#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3495 /** internal use only - semaphore used during asynchronous send */
3496 struct k_sem *_async_sem;
3497#endif
3498};
3499
3500struct k_mbox {
3501 _wait_q_t tx_msg_queue;
3502 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003503 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003505 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003507/**
3508 * @cond INTERNAL_HIDDEN
3509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003511#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003513 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3514 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003515 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003516 }
3517
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003518#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3519
Peter Mitsis12092702016-10-14 12:57:23 -04003520/**
Allan Stephensc98da842016-11-11 15:45:03 -05003521 * INTERNAL_HIDDEN @endcond
3522 */
3523
3524/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003527 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003528 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003529 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003532 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003534#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003535 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003536 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003537
Peter Mitsis12092702016-10-14 12:57:23 -04003538/**
3539 * @brief Initialize a mailbox.
3540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * This routine initializes a mailbox object, prior to its first use.
3542 *
3543 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003544 *
3545 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003546 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003547 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003548extern void k_mbox_init(struct k_mbox *mbox);
3549
Peter Mitsis12092702016-10-14 12:57:23 -04003550/**
3551 * @brief Send a mailbox message in a synchronous manner.
3552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * This routine sends a message to @a mbox and waits for a receiver to both
3554 * receive and process it. The message data may be in a buffer, in a memory
3555 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * @param mbox Address of the mailbox.
3558 * @param tx_msg Address of the transmit message descriptor.
3559 * @param timeout Waiting period for the message to be received (in
3560 * milliseconds), or one of the special values K_NO_WAIT
3561 * and K_FOREVER. Once the message has been received,
3562 * this routine waits as long as necessary for the message
3563 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003564 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003565 * @retval 0 Message sent.
3566 * @retval -ENOMSG Returned without waiting.
3567 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003568 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003569 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003570extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003571 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003572
Peter Mitsis12092702016-10-14 12:57:23 -04003573/**
3574 * @brief Send a mailbox message in an asynchronous manner.
3575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003576 * This routine sends a message to @a mbox without waiting for a receiver
3577 * to process it. The message data may be in a buffer, in a memory pool block,
3578 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3579 * will be given when the message has been both received and completely
3580 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003582 * @param mbox Address of the mailbox.
3583 * @param tx_msg Address of the transmit message descriptor.
3584 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003585 *
3586 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003587 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003588 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003589extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590 struct k_sem *sem);
3591
Peter Mitsis12092702016-10-14 12:57:23 -04003592/**
3593 * @brief Receive a mailbox message.
3594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * This routine receives a message from @a mbox, then optionally retrieves
3596 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * @param mbox Address of the mailbox.
3599 * @param rx_msg Address of the receive message descriptor.
3600 * @param buffer Address of the buffer to receive data, or NULL to defer data
3601 * retrieval and message disposal until later.
3602 * @param timeout Waiting period for a message to be received (in
3603 * milliseconds), or one of the special values K_NO_WAIT
3604 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003605 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003606 * @retval 0 Message received.
3607 * @retval -ENOMSG Returned without waiting.
3608 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003609 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003610 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003611extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003612 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003613
3614/**
3615 * @brief Retrieve mailbox message data into a buffer.
3616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * This routine completes the processing of a received message by retrieving
3618 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003619 *
3620 * Alternatively, this routine can be used to dispose of a received message
3621 * without retrieving its data.
3622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * @param rx_msg Address of the receive message descriptor.
3624 * @param buffer Address of the buffer to receive data, or NULL to discard
3625 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003626 *
3627 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003628 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003629 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003630extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003631
3632/**
3633 * @brief Retrieve mailbox message data into a memory pool block.
3634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * This routine completes the processing of a received message by retrieving
3636 * its data into a memory pool block, then disposing of the message.
3637 * The memory pool block that results from successful retrieval must be
3638 * returned to the pool once the data has been processed, even in cases
3639 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003640 *
3641 * Alternatively, this routine can be used to dispose of a received message
3642 * without retrieving its data. In this case there is no need to return a
3643 * memory pool block to the pool.
3644 *
3645 * This routine allocates a new memory pool block for the data only if the
3646 * data is not already in one. If a new block cannot be allocated, the routine
3647 * returns a failure code and the received message is left unchanged. This
3648 * permits the caller to reattempt data retrieval at a later time or to dispose
3649 * of the received message without retrieving its data.
3650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @param rx_msg Address of a receive message descriptor.
3652 * @param pool Address of memory pool, or NULL to discard data.
3653 * @param block Address of the area to hold memory pool block info.
3654 * @param timeout Waiting period to wait for a memory pool block (in
3655 * milliseconds), or one of the special values K_NO_WAIT
3656 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003657 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003658 * @retval 0 Data retrieved.
3659 * @retval -ENOMEM Returned without waiting.
3660 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003661 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003662 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003663extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003664 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003665 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003666
Anas Nashif166f5192018-02-25 08:02:36 -06003667/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003668
3669/**
Anas Nashifce78d162018-05-24 12:43:11 -05003670 * @defgroup pipe_apis Pipe APIs
3671 * @ingroup kernel_apis
3672 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003673 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674
Anas Nashifce78d162018-05-24 12:43:11 -05003675/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003676struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003677 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3678 size_t size; /**< Buffer size */
3679 size_t bytes_used; /**< # bytes used in buffer */
3680 size_t read_index; /**< Where in buffer to read from */
3681 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003682 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003683
3684 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003685 _wait_q_t readers; /**< Reader wait queue */
3686 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687 } wait_q;
3688
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003689 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003690 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003691};
3692
Anas Nashifce78d162018-05-24 12:43:11 -05003693/**
3694 * @cond INTERNAL_HIDDEN
3695 */
3696#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3697
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003698#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3699 { \
3700 .buffer = pipe_buffer, \
3701 .size = pipe_buffer_size, \
3702 .bytes_used = 0, \
3703 .read_index = 0, \
3704 .write_index = 0, \
3705 .lock = {}, \
3706 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003707 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3708 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003709 }, \
3710 _OBJECT_TRACING_INIT \
3711 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003712 }
3713
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003714#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3715
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003716/**
Allan Stephensc98da842016-11-11 15:45:03 -05003717 * INTERNAL_HIDDEN @endcond
3718 */
3719
3720/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003723 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003724 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003725 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003727 * @param name Name of the pipe.
3728 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3729 * or zero if no ring buffer is used.
3730 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003731 *
3732 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003733 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003734#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003735 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003736 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003737 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003738 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003743 * This routine initializes a pipe object, prior to its first use.
3744 *
3745 * @param pipe Address of the pipe.
3746 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3747 * is used.
3748 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3749 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750 *
3751 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003752 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003753 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003754void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3755
3756/**
3757 * @brief Release a pipe's allocated buffer
3758 *
3759 * If a pipe object was given a dynamically allocated buffer via
3760 * k_pipe_alloc_init(), this will free it. This function does nothing
3761 * if the buffer wasn't dynamically allocated.
3762 *
3763 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003764 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003765 */
3766void k_pipe_cleanup(struct k_pipe *pipe);
3767
3768/**
3769 * @brief Initialize a pipe and allocate a buffer for it
3770 *
3771 * Storage for the buffer region will be allocated from the calling thread's
3772 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3773 * or userspace is enabled and the pipe object loses all references to it.
3774 *
3775 * This function should only be called on uninitialized pipe objects.
3776 *
3777 * @param pipe Address of the pipe.
3778 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3779 * buffer is used.
3780 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003781 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003782 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003783 */
3784__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785
3786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * @param pipe Address of the pipe.
3792 * @param data Address of data to write.
3793 * @param bytes_to_write Size of data (in bytes).
3794 * @param bytes_written Address of area to hold the number of bytes written.
3795 * @param min_xfer Minimum number of bytes to write.
3796 * @param timeout Waiting period to wait for the data to be written (in
3797 * milliseconds), or one of the special values K_NO_WAIT
3798 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003800 * @retval 0 At least @a min_xfer bytes of data were written.
3801 * @retval -EIO Returned without waiting; zero data bytes were written.
3802 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003803 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003804 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003805 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003806__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3807 size_t bytes_to_write, size_t *bytes_written,
3808 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809
3810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003811 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 * @param pipe Address of the pipe.
3816 * @param data Address to place the data read from pipe.
3817 * @param bytes_to_read Maximum number of data bytes to read.
3818 * @param bytes_read Address of area to hold the number of bytes read.
3819 * @param min_xfer Minimum number of data bytes to read.
3820 * @param timeout Waiting period to wait for the data to be read (in
3821 * milliseconds), or one of the special values K_NO_WAIT
3822 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003823 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003824 * @retval 0 At least @a min_xfer bytes of data were read.
3825 * @retval -EIO Returned without waiting; zero data bytes were read.
3826 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003827 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003828 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003830__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3831 size_t bytes_to_read, size_t *bytes_read,
3832 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833
3834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003835 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * This routine writes the data contained in a memory block to @a pipe.
3838 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003841 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003842 * @param block Memory block containing data to send
3843 * @param size Number of data bytes in memory block to send
3844 * @param sem Semaphore to signal upon completion (else NULL)
3845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003847 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003848 */
3849extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3850 size_t size, struct k_sem *sem);
3851
Anas Nashif166f5192018-02-25 08:02:36 -06003852/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003853
Allan Stephensc98da842016-11-11 15:45:03 -05003854/**
3855 * @cond INTERNAL_HIDDEN
3856 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003858struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003859 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003860 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003861 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003862 char *buffer;
3863 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003864 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003865
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003866 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867};
3868
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003869#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003870 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003871 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003872 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003873 .num_blocks = slab_num_blocks, \
3874 .block_size = slab_block_size, \
3875 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003876 .free_list = NULL, \
3877 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003878 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003879 }
3880
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003881#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3882
3883
Peter Mitsis578f9112016-10-07 13:50:31 -04003884/**
Allan Stephensc98da842016-11-11 15:45:03 -05003885 * INTERNAL_HIDDEN @endcond
3886 */
3887
3888/**
3889 * @defgroup mem_slab_apis Memory Slab APIs
3890 * @ingroup kernel_apis
3891 * @{
3892 */
3893
3894/**
Allan Stephensda827222016-11-09 14:23:58 -06003895 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003896 *
Allan Stephensda827222016-11-09 14:23:58 -06003897 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003898 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003899 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3900 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003901 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003902 *
Allan Stephensda827222016-11-09 14:23:58 -06003903 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003904 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003905 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003906 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003908 * @param name Name of the memory slab.
3909 * @param slab_block_size Size of each memory block (in bytes).
3910 * @param slab_num_blocks Number memory blocks.
3911 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003912 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003913 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003914#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3915 char __noinit __aligned(slab_align) \
3916 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003917 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003918 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003919 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003920
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003921/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003922 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003925 *
Allan Stephensda827222016-11-09 14:23:58 -06003926 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3927 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3928 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3929 * To ensure that each memory block is similarly aligned to this boundary,
3930 * @a slab_block_size must also be a multiple of N.
3931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * @param slab Address of the memory slab.
3933 * @param buffer Pointer to buffer used for the memory blocks.
3934 * @param block_size Size of each memory block (in bytes).
3935 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003936 *
3937 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003938 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003940extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003941 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942
3943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003944 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003948 * @param slab Address of the memory slab.
3949 * @param mem Pointer to block address area.
3950 * @param timeout Maximum time to wait for operation to complete
3951 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3952 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003953 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003954 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003956 * @retval -ENOMEM Returned without waiting.
3957 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003958 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003960extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003961 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003962
3963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003966 * This routine releases a previously allocated memory block back to its
3967 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @param slab Address of the memory slab.
3970 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003971 *
3972 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003973 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003974 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003975extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003980 * This routine gets the number of memory blocks that are currently
3981 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003985 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003986 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003987 */
Kumar Galacc334c72017-04-21 10:55:34 -05003988static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003989{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003990 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003991}
3992
Peter Mitsisc001aa82016-10-13 13:53:37 -04003993/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003994 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * This routine gets the number of memory blocks that are currently
3997 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004002 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004003 */
Kumar Galacc334c72017-04-21 10:55:34 -05004004static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004005{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004006 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004007}
4008
Anas Nashif166f5192018-02-25 08:02:36 -06004009/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004010
4011/**
4012 * @cond INTERNAL_HIDDEN
4013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004014
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004015struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004016 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004017 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004018};
4019
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004020/**
Allan Stephensc98da842016-11-11 15:45:03 -05004021 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004022 */
4023
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004024/**
Allan Stephensc98da842016-11-11 15:45:03 -05004025 * @addtogroup mem_pool_apis
4026 * @{
4027 */
4028
4029/**
4030 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4033 * long. The memory pool allows blocks to be repeatedly partitioned into
4034 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004035 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004036 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004037 * If the pool is to be accessed outside the module where it is defined, it
4038 * can be declared via
4039 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004040 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004042 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004043 * @param minsz Size of the smallest blocks in the pool (in bytes).
4044 * @param maxsz Size of the largest blocks in the pool (in bytes).
4045 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004046 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004047 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004048 */
Andy Ross73cb9582017-05-09 10:42:39 -07004049#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4050 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4051 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004052 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004053 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004054 .base = { \
4055 .buf = _mpool_buf_##name, \
4056 .max_sz = maxsz, \
4057 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004058 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004059 .levels = _mpool_lvls_##name, \
4060 .flags = SYS_MEM_POOL_KERNEL \
4061 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004062 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004063
Peter Mitsis937042c2016-10-13 13:18:26 -04004064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004065 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004067 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004069 * @param pool Address of the memory pool.
4070 * @param block Pointer to block descriptor for the allocated memory.
4071 * @param size Amount of memory to allocate (in bytes).
4072 * @param timeout Maximum time to wait for operation to complete
4073 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4074 * or K_FOREVER to wait as long as necessary.
4075 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004076 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004077 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004078 * @retval -ENOMEM Returned without waiting.
4079 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004080 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004081 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004082extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004083 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004084
4085/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004086 * @brief Allocate memory from a memory pool with malloc() semantics
4087 *
4088 * Such memory must be released using k_free().
4089 *
4090 * @param pool Address of the memory pool.
4091 * @param size Amount of memory to allocate (in bytes).
4092 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004093 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004094 */
4095extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4096
4097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004100 * This routine releases a previously allocated memory block back to its
4101 * memory pool.
4102 *
4103 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004104 *
4105 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004106 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004107 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004108extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004109
4110/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004111 * @brief Free memory allocated from a memory pool.
4112 *
4113 * This routine releases a previously allocated memory block back to its
4114 * memory pool
4115 *
4116 * @param id Memory block identifier.
4117 *
4118 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004119 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004120 */
4121extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4122
4123/**
Anas Nashif166f5192018-02-25 08:02:36 -06004124 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004125 */
4126
4127/**
4128 * @defgroup heap_apis Heap Memory Pool APIs
4129 * @ingroup kernel_apis
4130 * @{
4131 */
4132
4133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004134 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004136 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004137 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004139 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004141 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004142 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004143 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004144extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004145
4146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004147 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004148 *
4149 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004150 * returned must have been allocated from the heap memory pool or
4151 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004152 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004153 * If @a ptr is NULL, no operation is performed.
4154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004155 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004156 *
4157 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004158 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004159 */
4160extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004161
Allan Stephensc98da842016-11-11 15:45:03 -05004162/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004163 * @brief Allocate memory from heap, array style
4164 *
4165 * This routine provides traditional calloc() semantics. Memory is
4166 * allocated from the heap memory pool and zeroed.
4167 *
4168 * @param nmemb Number of elements in the requested array
4169 * @param size Size of each array element (in bytes).
4170 *
4171 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004172 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004173 */
4174extern void *k_calloc(size_t nmemb, size_t size);
4175
Anas Nashif166f5192018-02-25 08:02:36 -06004176/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004177
Benjamin Walshacc68c12017-01-29 18:57:45 -05004178/* polling API - PRIVATE */
4179
Benjamin Walshb0179862017-02-02 16:39:57 -05004180#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004181#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004182#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004183#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004184#endif
4185
Benjamin Walshacc68c12017-01-29 18:57:45 -05004186/* private - implementation data created as needed, per-type */
4187struct _poller {
4188 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004189 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004190};
4191
4192/* private - types bit positions */
4193enum _poll_types_bits {
4194 /* can be used to ignore an event */
4195 _POLL_TYPE_IGNORE,
4196
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004197 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004198 _POLL_TYPE_SIGNAL,
4199
4200 /* semaphore availability */
4201 _POLL_TYPE_SEM_AVAILABLE,
4202
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004203 /* queue/fifo/lifo data availability */
4204 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004205
4206 _POLL_NUM_TYPES
4207};
4208
Patrik Flykt4344e272019-03-08 14:19:05 -07004209#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004210
4211/* private - states bit positions */
4212enum _poll_states_bits {
4213 /* default state when creating event */
4214 _POLL_STATE_NOT_READY,
4215
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004216 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004217 _POLL_STATE_SIGNALED,
4218
4219 /* semaphore is available */
4220 _POLL_STATE_SEM_AVAILABLE,
4221
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004222 /* data is available to read on queue/fifo/lifo */
4223 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004224
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004225 /* queue/fifo/lifo wait was cancelled */
4226 _POLL_STATE_CANCELLED,
4227
Benjamin Walshacc68c12017-01-29 18:57:45 -05004228 _POLL_NUM_STATES
4229};
4230
Patrik Flykt4344e272019-03-08 14:19:05 -07004231#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004232
4233#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004234 (32 - (0 \
4235 + 8 /* tag */ \
4236 + _POLL_NUM_TYPES \
4237 + _POLL_NUM_STATES \
4238 + 1 /* modes */ \
4239 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004240
Benjamin Walshacc68c12017-01-29 18:57:45 -05004241/* end of polling API - PRIVATE */
4242
4243
4244/**
4245 * @defgroup poll_apis Async polling APIs
4246 * @ingroup kernel_apis
4247 * @{
4248 */
4249
4250/* Public polling API */
4251
4252/* public - values for k_poll_event.type bitfield */
4253#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004254#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4255#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4256#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004257#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004258
4259/* public - polling modes */
4260enum k_poll_modes {
4261 /* polling thread does not take ownership of objects when available */
4262 K_POLL_MODE_NOTIFY_ONLY = 0,
4263
4264 K_POLL_NUM_MODES
4265};
4266
4267/* public - values for k_poll_event.state bitfield */
4268#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004269#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4270#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4271#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004272#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004273#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004274
4275/* public - poll signal object */
4276struct k_poll_signal {
4277 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004278 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004279
4280 /*
4281 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4282 * user resets it to 0.
4283 */
4284 unsigned int signaled;
4285
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004286 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004287 int result;
4288};
4289
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004290#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004291 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004292 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293 .signaled = 0, \
4294 .result = 0, \
4295 }
4296
4297struct k_poll_event {
4298 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004299 sys_dnode_t _node;
4300
4301 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004302 struct _poller *poller;
4303
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004304 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004305 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004306
Benjamin Walshacc68c12017-01-29 18:57:45 -05004307 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004308 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004309
4310 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004311 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312
4313 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004314 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004315
4316 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004317 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004318
4319 /* per-type data */
4320 union {
4321 void *obj;
4322 struct k_poll_signal *signal;
4323 struct k_sem *sem;
4324 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004325 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004326 };
4327};
4328
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004329#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004330 { \
4331 .poller = NULL, \
4332 .type = event_type, \
4333 .state = K_POLL_STATE_NOT_READY, \
4334 .mode = event_mode, \
4335 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004336 { .obj = event_obj }, \
4337 }
4338
4339#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4340 event_tag) \
4341 { \
4342 .type = event_type, \
4343 .tag = event_tag, \
4344 .state = K_POLL_STATE_NOT_READY, \
4345 .mode = event_mode, \
4346 .unused = 0, \
4347 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004348 }
4349
4350/**
4351 * @brief Initialize one struct k_poll_event instance
4352 *
4353 * After this routine is called on a poll event, the event it ready to be
4354 * placed in an event array to be passed to k_poll().
4355 *
4356 * @param event The event to initialize.
4357 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4358 * values. Only values that apply to the same object being polled
4359 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4360 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004361 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362 * @param obj Kernel object or poll signal.
4363 *
4364 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004365 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004366 */
4367
Kumar Galacc334c72017-04-21 10:55:34 -05004368extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004369 int mode, void *obj);
4370
4371/**
4372 * @brief Wait for one or many of multiple poll events to occur
4373 *
4374 * This routine allows a thread to wait concurrently for one or many of
4375 * multiple poll events to have occurred. Such events can be a kernel object
4376 * being available, like a semaphore, or a poll signal event.
4377 *
4378 * When an event notifies that a kernel object is available, the kernel object
4379 * is not "given" to the thread calling k_poll(): it merely signals the fact
4380 * that the object was available when the k_poll() call was in effect. Also,
4381 * all threads trying to acquire an object the regular way, i.e. by pending on
4382 * the object, have precedence over the thread polling on the object. This
4383 * means that the polling thread will never get the poll event on an object
4384 * until the object becomes available and its pend queue is empty. For this
4385 * reason, the k_poll() call is more effective when the objects being polled
4386 * only have one thread, the polling thread, trying to acquire them.
4387 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004388 * When k_poll() returns 0, the caller should loop on all the events that were
4389 * passed to k_poll() and check the state field for the values that were
4390 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004391 *
4392 * Before being reused for another call to k_poll(), the user has to reset the
4393 * state field to K_POLL_STATE_NOT_READY.
4394 *
Andrew Boie3772f772018-05-07 16:52:57 -07004395 * When called from user mode, a temporary memory allocation is required from
4396 * the caller's resource pool.
4397 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004398 * @param events An array of pointers to events to be polled for.
4399 * @param num_events The number of events in the array.
4400 * @param timeout Waiting period for an event to be ready (in milliseconds),
4401 * or one of the special values K_NO_WAIT and K_FOREVER.
4402 *
4403 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004404 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004405 * @retval -EINTR Polling has been interrupted, e.g. with
4406 * k_queue_cancel_wait(). All output events are still set and valid,
4407 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4408 * words, -EINTR status means that at least one of output events is
4409 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004410 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4411 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004412 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004413 */
4414
Andrew Boie3772f772018-05-07 16:52:57 -07004415__syscall int k_poll(struct k_poll_event *events, int num_events,
4416 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004417
4418/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004419 * @brief Initialize a poll signal object.
4420 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004421 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004422 *
4423 * @param signal A poll signal.
4424 *
4425 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004426 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004427 */
4428
Andrew Boie3772f772018-05-07 16:52:57 -07004429__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4430
4431/*
4432 * @brief Reset a poll signal object's state to unsignaled.
4433 *
4434 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004435 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004436 */
4437__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4438
Patrik Flykt4344e272019-03-08 14:19:05 -07004439static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004440{
Patrik Flykt24d71432019-03-26 19:57:45 -06004441 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004442}
4443
4444/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004445 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004446 *
4447 * @param signal A poll signal object
4448 * @param signaled An integer buffer which will be written nonzero if the
4449 * object was signaled
4450 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004451 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004452 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004453 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004454 */
4455__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4456 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004457
4458/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004459 * @brief Signal a poll signal object.
4460 *
4461 * This routine makes ready a poll signal, which is basically a poll event of
4462 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4463 * made ready to run. A @a result value can be specified.
4464 *
4465 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004466 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004467 * k_poll_signal_reset(). It thus has to be reset by the user before being
4468 * passed again to k_poll() or k_poll() will consider it being signaled, and
4469 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004470 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004471 * @note The result is stored and the 'signaled' field is set even if
4472 * this function returns an error indicating that an expiring poll was
4473 * not notified. The next k_poll() will detect the missed raise.
4474 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004475 * @param signal A poll signal.
4476 * @param result The value to store in the result field of the signal.
4477 *
4478 * @retval 0 The signal was delivered successfully.
4479 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004480 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004481 */
4482
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004483__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004484
Anas Nashif954d5502018-02-25 08:37:28 -06004485/**
4486 * @internal
4487 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004488extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004489
Anas Nashif166f5192018-02-25 08:02:36 -06004490/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004491
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004492/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004493 * @defgroup cpu_idle_apis CPU Idling APIs
4494 * @ingroup kernel_apis
4495 * @{
4496 */
4497
4498/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004499 * @brief Make the CPU idle.
4500 *
4501 * This function makes the CPU idle until an event wakes it up.
4502 *
4503 * In a regular system, the idle thread should be the only thread responsible
4504 * for making the CPU idle and triggering any type of power management.
4505 * However, in some more constrained systems, such as a single-threaded system,
4506 * the only thread would be responsible for this if needed.
4507 *
4508 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004509 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004510 */
4511extern void k_cpu_idle(void);
4512
4513/**
4514 * @brief Make the CPU idle in an atomic fashion.
4515 *
4516 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4517 * must be done atomically before making the CPU idle.
4518 *
4519 * @param key Interrupt locking key obtained from irq_lock().
4520 *
4521 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004522 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004523 */
4524extern void k_cpu_atomic_idle(unsigned int key);
4525
Anas Nashif30c3cff2019-01-22 08:18:13 -05004526/**
4527 * @}
4528 */
Anas Nashif954d5502018-02-25 08:37:28 -06004529
4530/**
4531 * @internal
4532 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004533extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004534
Patrik Flykt4344e272019-03-08 14:19:05 -07004535#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004536/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004537#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004538#else
4539
Andrew Boiecdb94d62017-04-18 15:22:05 -07004540/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004541 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004542 *
4543 * We won't have a real exception frame to determine the PC value when
4544 * the oops occurred, so print file and line number before we jump into
4545 * the fatal error handler.
4546 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004547#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004548 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Patrik Flykt4344e272019-03-08 14:19:05 -07004549 z_NanoFatalErrorHandler(reason, &_default_esf); \
Andy Ross92ce7672019-05-31 13:12:15 -07004550 k_thread_abort(k_current_get()); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004551 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004552
4553#endif /* _ARCH__EXCEPT */
4554
4555/**
4556 * @brief Fatally terminate a thread
4557 *
4558 * This should be called when a thread has encountered an unrecoverable
4559 * runtime condition and needs to terminate. What this ultimately
4560 * means is determined by the _fatal_error_handler() implementation, which
4561 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4562 *
4563 * If this is called from ISR context, the default system fatal error handler
4564 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004565 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004566 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004567#define k_oops() z_except_reason(_NANO_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004568
4569/**
4570 * @brief Fatally terminate the system
4571 *
4572 * This should be called when the Zephyr kernel has encountered an
4573 * unrecoverable runtime condition and needs to terminate. What this ultimately
4574 * means is determined by the _fatal_error_handler() implementation, which
4575 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004576 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004577 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004578#define k_panic() z_except_reason(_NANO_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004579
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004580/*
4581 * private APIs that are utilized by one or more public APIs
4582 */
4583
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004584#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004585/**
4586 * @internal
4587 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004588extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004589#else
Anas Nashif954d5502018-02-25 08:37:28 -06004590/**
4591 * @internal
4592 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004593#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004594#endif
4595
Anas Nashif954d5502018-02-25 08:37:28 -06004596/**
4597 * @internal
4598 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004599extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004600/**
4601 * @internal
4602 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004603extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004604
Andrew Boiedc5d9352017-06-02 12:56:47 -07004605/* arch/cpu.h may declare an architecture or platform-specific macro
4606 * for properly declaring stacks, compatible with MMU/MPU constraints if
4607 * enabled
4608 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004609
4610/**
4611 * @brief Obtain an extern reference to a stack
4612 *
4613 * This macro properly brings the symbol of a thread stack declared
4614 * elsewhere into scope.
4615 *
4616 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004617 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004618 */
4619#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4620
Patrik Flykt4344e272019-03-08 14:19:05 -07004621#ifdef Z_ARCH_THREAD_STACK_DEFINE
4622#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004623#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004624 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4625#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4626#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4627#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004628#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004629static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004630{
Patrik Flykt4344e272019-03-08 14:19:05 -07004631 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004632}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004633#else
4634/**
4635 * @brief Declare a toplevel thread stack memory region
4636 *
4637 * This declares a region of memory suitable for use as a thread's stack.
4638 *
4639 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4640 * 'noinit' section so that it isn't zeroed at boot
4641 *
Andrew Boie507852a2017-07-25 18:47:07 -07004642 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004643 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004644 * inside needs to be examined, examine thread->stack_info for the associated
4645 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004646 *
4647 * It is legal to precede this definition with the 'static' keyword.
4648 *
4649 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4650 * parameter of k_thread_create(), it may not be the same as the
4651 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4652 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004653 * Some arches may round the size of the usable stack region up to satisfy
4654 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4655 * size.
4656 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004657 * @param sym Thread stack symbol name
4658 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004659 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004660 */
4661#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004662 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004663
4664/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304665 * @brief Calculate size of stacks to be allocated in a stack array
4666 *
4667 * This macro calculates the size to be allocated for the stacks
4668 * inside a stack array. It accepts the indicated "size" as a parameter
4669 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4670 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4671 *
4672 * @param size Size of the stack memory region
4673 * @req K-TSTACK-001
4674 */
4675#define K_THREAD_STACK_LEN(size) (size)
4676
4677/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004678 * @brief Declare a toplevel array of thread stack memory regions
4679 *
4680 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4681 * definition for additional details and constraints.
4682 *
4683 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4684 * 'noinit' section so that it isn't zeroed at boot
4685 *
4686 * @param sym Thread stack symbol name
4687 * @param nmemb Number of stacks to declare
4688 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004689 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004690 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004691#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004692 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304693 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004694
4695/**
4696 * @brief Declare an embedded stack memory region
4697 *
4698 * Used for stacks embedded within other data structures. Use is highly
4699 * discouraged but in some cases necessary. For memory protection scenarios,
4700 * it is very important that any RAM preceding this member not be writable
4701 * by threads else a stack overflow will lead to silent corruption. In other
4702 * words, the containing data structure should live in RAM owned by the kernel.
4703 *
4704 * @param sym Thread stack symbol name
4705 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004706 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004707 */
4708#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004709 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004710
4711/**
4712 * @brief Return the size in bytes of a stack memory region
4713 *
4714 * Convenience macro for passing the desired stack size to k_thread_create()
4715 * since the underlying implementation may actually create something larger
4716 * (for instance a guard area).
4717 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004718 * The value returned here is not guaranteed to match the 'size' parameter
4719 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004720 *
4721 * @param sym Stack memory symbol
4722 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004723 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004724 */
4725#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4726
Andrew Boie575abc02019-03-19 10:42:24 -07004727
4728/**
4729 * @brief Indicate how much additional memory is reserved for stack objects
4730 *
4731 * Any given stack declaration may have additional memory in it for guard
4732 * areas or supervisor mode stacks. This macro indicates how much space
4733 * is reserved for this. The memory reserved may not be contiguous within
4734 * the stack object, and does not account for additional space used due to
4735 * enforce alignment.
4736 */
4737#define K_THREAD_STACK_RESERVED 0
4738
Andrew Boiedc5d9352017-06-02 12:56:47 -07004739/**
4740 * @brief Get a pointer to the physical stack buffer
4741 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004742 * This macro is deprecated. If a stack buffer needs to be examined, the
4743 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004744 *
4745 * @param sym Declared stack symbol name
4746 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004747 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004748 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004749static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004750{
4751 return (char *)sym;
4752}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004753
4754#endif /* _ARCH_DECLARE_STACK */
4755
Chunlin Hane9c97022017-07-07 20:29:30 +08004756/**
4757 * @defgroup mem_domain_apis Memory domain APIs
4758 * @ingroup kernel_apis
4759 * @{
4760 */
4761
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004762/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004763 * @def K_MEM_PARTITION_DEFINE
4764 * @brief Used to declare a memory partition
4765 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004766 */
4767#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4768#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4769 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004770 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004771 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004772#else
4773#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004774 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004775 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004776#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4777
Chunlin Hane9c97022017-07-07 20:29:30 +08004778/* memory partition */
4779struct k_mem_partition {
4780 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004781 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004782 /* size of memory partition */
4783 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004784#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004785 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304786 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004787#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004788};
4789
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004790/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304791 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004792struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304793#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004794 /* partitions in the domain */
4795 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304796#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004797 /* domain q */
4798 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004799 /* number of partitions in the domain */
4800 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004801};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304802
Chunlin Hane9c97022017-07-07 20:29:30 +08004803
4804/**
4805 * @brief Initialize a memory domain.
4806 *
4807 * Initialize a memory domain with given name and memory partitions.
4808 *
4809 * @param domain The memory domain to be initialized.
4810 * @param num_parts The number of array items of "parts" parameter.
4811 * @param parts An array of pointers to the memory partitions. Can be NULL
4812 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004813 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004814 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004815extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004816 struct k_mem_partition *parts[]);
4817/**
4818 * @brief Destroy a memory domain.
4819 *
4820 * Destroy a memory domain.
4821 *
4822 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004823 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004824 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004825extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4826
4827/**
4828 * @brief Add a memory partition into a memory domain.
4829 *
4830 * Add a memory partition into a memory domain.
4831 *
4832 * @param domain The memory domain to be added a memory partition.
4833 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004834 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004835 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004836extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4837 struct k_mem_partition *part);
4838
4839/**
4840 * @brief Remove a memory partition from a memory domain.
4841 *
4842 * Remove a memory partition from a memory domain.
4843 *
4844 * @param domain The memory domain to be removed a memory partition.
4845 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004846 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004847 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004848extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4849 struct k_mem_partition *part);
4850
4851/**
4852 * @brief Add a thread into a memory domain.
4853 *
4854 * Add a thread into a memory domain.
4855 *
4856 * @param domain The memory domain that the thread is going to be added into.
4857 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004858 *
4859 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004860 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004861extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4862 k_tid_t thread);
4863
4864/**
4865 * @brief Remove a thread from its memory domain.
4866 *
4867 * Remove a thread from its memory domain.
4868 *
4869 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004870 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004871 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004872extern void k_mem_domain_remove_thread(k_tid_t thread);
4873
Anas Nashif166f5192018-02-25 08:02:36 -06004874/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004875
Andrew Boie756f9072017-10-10 16:01:49 -07004876/**
4877 * @brief Emit a character buffer to the console device
4878 *
4879 * @param c String of characters to print
4880 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004881 *
4882 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004883 */
4884__syscall void k_str_out(char *c, size_t n);
4885
Andy Rosse7172672018-01-24 15:48:32 -08004886/**
4887 * @brief Start a numbered CPU on a MP-capable system
4888
4889 * This starts and initializes a specific CPU. The main thread on
4890 * startup is running on CPU zero, other processors are numbered
4891 * sequentially. On return from this function, the CPU is known to
4892 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004893 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004894 * with the provided key will work to enable them.
4895 *
4896 * Normally, in SMP mode this function will be called by the kernel
4897 * initialization and should not be used as a user API. But it is
4898 * defined here for special-purpose apps which want Zephyr running on
4899 * one core and to use others for design-specific processing.
4900 *
4901 * @param cpu_num Integer number of the CPU
4902 * @param stack Stack memory for the CPU
4903 * @param sz Stack buffer size, in bytes
4904 * @param fn Function to begin running on the CPU. First argument is
4905 * an irq_unlock() key.
4906 * @param arg Untyped argument to be passed to "fn"
4907 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004908extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004909 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004910
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02004911
4912/**
4913 * @brief Disable preservation of floating point context information.
4914 *
4915 * This routine informs the kernel that the specified thread
4916 * will no longer be using the floating point registers.
4917 *
4918 * @warning
4919 * Some architectures apply restrictions on how the disabling of floating
4920 * point preservation may be requested, see z_arch_float_disable.
4921 *
4922 * @warning
4923 * This routine should only be used to disable floating point support for
4924 * a thread that currently has such support enabled.
4925 *
4926 * @param thread ID of thread.
4927 *
4928 * @retval 0 On success.
4929 * @retval -ENOSYS If the floating point disabling is not implemented.
4930 * -EINVAL If the floating point disabling could not be performed.
4931 */
4932__syscall int k_float_disable(struct k_thread *thread);
4933
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004934#ifdef __cplusplus
4935}
4936#endif
4937
Anas Nashifb6304e62018-07-04 08:03:03 -05004938#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004939#include <syscalls/kernel.h>
4940
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004941#endif /* !_ASMLANGUAGE */
4942
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004943#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */