blob: b42c45e77e0540842866a9a561730e08699940f4 [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;
Wentong Wu5611e922019-06-20 23:51:27 +0800132struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400133
Andrew Boie5bd891d2017-09-27 12:59:28 -0700134/* This enumeration needs to be kept in sync with the lists of kernel objects
135 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
136 * function in kernel/userspace.c
137 */
Andrew Boie945af952017-08-22 13:15:23 -0700138enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700139 K_OBJ_ANY,
140
Leandro Pereirac2003672018-04-04 13:50:32 -0700141 /** @cond
142 * Doxygen should ignore this build-time generated include file
143 * when genrating API documentation. Enumeration values are
144 * generated during build by gen_kobject_list.py. It includes
145 * basic kernel objects (e.g. pipes and mutexes) and driver types.
146 */
147#include <kobj-types-enum.h>
148 /** @endcond
149 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700150
Andrew Boie945af952017-08-22 13:15:23 -0700151 K_OBJ_LAST
152};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500153/**
154 * @defgroup usermode_apis User Mode APIs
155 * @ingroup kernel_apis
156 * @{
157 */
Andrew Boie945af952017-08-22 13:15:23 -0700158
159#ifdef CONFIG_USERSPACE
160/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700161 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700162struct _k_object {
163 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700164 u8_t perms[CONFIG_MAX_THREAD_BYTES];
165 u8_t type;
166 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700167 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700168} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700169
Andrew Boie877f82e2017-10-17 11:20:22 -0700170struct _k_object_assignment {
171 struct k_thread *thread;
172 void * const *objects;
173};
174
175/**
176 * @brief Grant a static thread access to a list of kernel objects
177 *
178 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
179 * a set of kernel objects. These objects do not need to be in an initialized
180 * state. The permissions will be granted when the threads are initialized
181 * in the early boot sequence.
182 *
183 * All arguments beyond the first must be pointers to kernel objects.
184 *
185 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
186 */
187#define K_THREAD_ACCESS_GRANT(name_, ...) \
188 static void * const _CONCAT(_object_list_, name_)[] = \
189 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400190 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
191 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700192 { (&_k_thread_obj_ ## name_), \
193 (_CONCAT(_object_list_, name_)) }
194
Andrew Boie945af952017-08-22 13:15:23 -0700195#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700196#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700197#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700198
199/**
200 * Lookup a kernel object and init its metadata if it exists
201 *
202 * Calling this on an object will make it usable from userspace.
203 * Intended to be called as the last statement in kernel object init
204 * functions.
205 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500206 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700207 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700208void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700209#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700210
211#define K_THREAD_ACCESS_GRANT(thread, ...)
212
Anas Nashif954d5502018-02-25 08:37:28 -0600213/**
214 * @internal
215 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700216static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700217{
218 ARG_UNUSED(obj);
219}
220
Anas Nashif954d5502018-02-25 08:37:28 -0600221/**
222 * @internal
223 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700224static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700225 struct k_thread *thread)
226{
227 ARG_UNUSED(object);
228 ARG_UNUSED(thread);
229}
230
Anas Nashif954d5502018-02-25 08:37:28 -0600231/**
232 * @internal
233 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700234static inline void k_object_access_revoke(void *object,
235 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700236{
237 ARG_UNUSED(object);
238 ARG_UNUSED(thread);
239}
240
Andrew Boiee9cfc542018-04-13 13:15:28 -0700241/**
242 * @internal
243 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700244static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700245{
246 ARG_UNUSED(object);
247}
248
Andrew Boie41bab6e2017-10-14 14:42:23 -0700249static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700250{
251 ARG_UNUSED(object);
252}
253#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700254
255/**
256 * grant a thread access to a kernel object
257 *
258 * The thread will be granted access to the object if the caller is from
259 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700260 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700261 *
262 * @param object Address of kernel object
263 * @param thread Thread to grant access to the object
264 */
Andrew Boie743e4682017-10-04 12:25:50 -0700265__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700266
Andrew Boiea89bf012017-10-09 14:47:55 -0700267/**
268 * grant a thread access to a kernel object
269 *
270 * The thread will lose access to the object if the caller is from
271 * supervisor mode, or the caller is from user mode AND has permissions
272 * on both the object and the thread whose access is being revoked.
273 *
274 * @param object Address of kernel object
275 * @param thread Thread to remove access to the object
276 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700277void k_object_access_revoke(void *object, struct k_thread *thread);
278
279
280__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700281
282/**
283 * grant all present and future threads access to an object
284 *
285 * If the caller is from supervisor mode, or the caller is from user mode and
286 * have sufficient permissions on the object, then that object will have
287 * permissions granted to it for *all* current and future threads running in
288 * the system, effectively becoming a public kernel object.
289 *
290 * Use of this API should be avoided on systems that are running untrusted code
291 * as it is possible for such code to derive the addresses of kernel objects
292 * and perform unwanted operations on them.
293 *
Andrew Boie04caa672017-10-13 13:57:07 -0700294 * It is not possible to revoke permissions on public objects; once public,
295 * any thread may use it.
296 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700297 * @param object Address of kernel object
298 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700299void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700300
Andrew Boie31bdfc02017-11-08 16:38:03 -0800301/**
302 * Allocate a kernel object of a designated type
303 *
304 * This will instantiate at runtime a kernel object of the specified type,
305 * returning a pointer to it. The object will be returned in an uninitialized
306 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700307 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800308 *
309 * Currently, allocation of thread stacks is not supported.
310 *
311 * @param otype Requested kernel object type
312 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
313 * available
314 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700315__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800316
Andrew Boie97bf0012018-04-24 17:01:37 -0700317#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800318/**
319 * Free a kernel object previously allocated with k_object_alloc()
320 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700321 * This will return memory for a kernel object back to resource pool it was
322 * allocated from. Care must be exercised that the object will not be used
323 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800324 *
325 * @param obj Pointer to the kernel object memory address.
326 */
327void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700328#else
Patrik Flykt4344e272019-03-08 14:19:05 -0700329static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700330{
Kumar Gala85699f72018-05-17 09:26:03 -0500331 ARG_UNUSED(otype);
332
Andrew Boie97bf0012018-04-24 17:01:37 -0700333 return NULL;
334}
335
336static inline void k_obj_free(void *obj)
337{
338 ARG_UNUSED(obj);
339}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800340#endif /* CONFIG_DYNAMIC_OBJECTS */
341
Anas Nashif4bcb2942019-01-23 23:06:29 -0500342/** @} */
343
Andrew Boiebca15da2017-10-15 14:17:48 -0700344/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700345 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700346 *
347 * The purpose of this data type is to clearly distinguish between the
348 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
349 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400350 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700351 * stack buffer region with guard areas that trigger a MPU or MMU fault
352 * if written to.
353 *
354 * APIs that want to work with the buffer inside should continue to use
355 * char *.
356 *
357 * Stacks should always be created with K_THREAD_STACK_DEFINE().
358 */
359struct __packed _k_thread_stack_element {
360 char data;
361};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700362typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700363
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700364/**
365 * @typedef k_thread_entry_t
366 * @brief Thread entry point function type.
367 *
368 * A thread's entry point function is invoked when the thread starts executing.
369 * Up to 3 argument values can be passed to the function.
370 *
371 * The thread terminates execution permanently if the entry point function
372 * returns. The thread is responsible for releasing any shared resources
373 * it may own (such as mutexes and dynamically allocated memory), prior to
374 * returning.
375 *
376 * @param p1 First argument.
377 * @param p2 Second argument.
378 * @param p3 Third argument.
379 *
380 * @return N/A
381 */
382typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700383
384#ifdef CONFIG_THREAD_MONITOR
385struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700386 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700387 void *parameter1;
388 void *parameter2;
389 void *parameter3;
390};
391#endif
392
393/* can be used for creating 'dummy' threads, e.g. for pending on objects */
394struct _thread_base {
395
396 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700397 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600398 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700399 struct rbnode qnode_rb;
400 };
401
Andy Ross1acd8c22018-05-03 14:51:49 -0700402 /* wait queue on which the thread is pended (needed only for
403 * trees, not dumb lists)
404 */
405 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700406
407 /* user facing 'thread options'; values defined in include/kernel.h */
408 u8_t user_options;
409
410 /* thread state */
411 u8_t thread_state;
412
413 /*
414 * scheduler lock count and thread priority
415 *
416 * These two fields control the preemptibility of a thread.
417 *
418 * When the scheduler is locked, sched_locked is decremented, which
419 * means that the scheduler is locked for values from 0xff to 0x01. A
420 * thread is coop if its prio is negative, thus 0x80 to 0xff when
421 * looked at the value as unsigned.
422 *
423 * By putting them end-to-end, this means that a thread is
424 * non-preemptible if the bundled value is greater than or equal to
425 * 0x0080.
426 */
427 union {
428 struct {
429#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
430 u8_t sched_locked;
431 s8_t prio;
432#else /* LITTLE and PDP */
433 s8_t prio;
434 u8_t sched_locked;
435#endif
436 };
437 u16_t preempt;
438 };
439
Andy Ross4a2e50f2018-05-15 11:06:25 -0700440#ifdef CONFIG_SCHED_DEADLINE
441 int prio_deadline;
442#endif
443
Andy Ross1acd8c22018-05-03 14:51:49 -0700444 u32_t order_key;
445
Andy Ross2724fd12018-01-29 14:55:20 -0800446#ifdef CONFIG_SMP
447 /* True for the per-CPU idle threads */
448 u8_t is_idle;
449
Andy Ross2724fd12018-01-29 14:55:20 -0800450 /* CPU index on which thread was last run */
451 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700452
453 /* Recursive count of irq_lock() calls */
454 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800455
456#endif
457
458#ifdef CONFIG_SCHED_CPU_MASK
459 /* "May run on" bits for each CPU */
460 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800461#endif
462
Andrew Boie73abd322017-04-04 13:19:13 -0700463 /* data returned by APIs */
464 void *swap_data;
465
466#ifdef CONFIG_SYS_CLOCK_EXISTS
467 /* this thread's entry in a timeout queue */
468 struct _timeout timeout;
469#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700470};
471
472typedef struct _thread_base _thread_base_t;
473
474#if defined(CONFIG_THREAD_STACK_INFO)
475/* Contains the stack information of a thread */
476struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700477 /* Stack start - Represents the start address of the thread-writable
478 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700479 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400480 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700481
482 /* Stack Size - Thread writable stack buffer size. Represents
483 * the size of the actual area, starting from the start member,
484 * that should be writable by the thread
485 */
Andrew Boie73abd322017-04-04 13:19:13 -0700486 u32_t size;
487};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700488
489typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700490#endif /* CONFIG_THREAD_STACK_INFO */
491
Chunlin Hane9c97022017-07-07 20:29:30 +0800492#if defined(CONFIG_USERSPACE)
493struct _mem_domain_info {
494 /* memory domain queue node */
495 sys_dnode_t mem_domain_q_node;
496 /* memory domain of the thread */
497 struct k_mem_domain *mem_domain;
498};
499
500#endif /* CONFIG_USERSPACE */
501
Daniel Leungfc182432018-08-16 15:42:28 -0700502#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
503struct _thread_userspace_local_data {
504 int errno_var;
505};
506#endif
507
Anas Nashifce78d162018-05-24 12:43:11 -0500508/**
509 * @ingroup thread_apis
510 * Thread Structure
511 */
Andrew Boie73abd322017-04-04 13:19:13 -0700512struct k_thread {
513
514 struct _thread_base base;
515
Anas Nashifce78d162018-05-24 12:43:11 -0500516 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700517 struct _callee_saved callee_saved;
518
Anas Nashifce78d162018-05-24 12:43:11 -0500519 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700520 void *init_data;
521
Anas Nashifce78d162018-05-24 12:43:11 -0500522 /**
523 * abort function
524 * @req K-THREAD-002
525 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700526 void (*fn_abort)(void);
527
528#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500529 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700530 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700531
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700533 struct k_thread *next_thread;
534#endif
535
Anas Nashif57554052018-03-03 02:31:05 -0600536#if defined(CONFIG_THREAD_NAME)
537 /* Thread name */
538 const char *name;
539#endif
540
Andrew Boie73abd322017-04-04 13:19:13 -0700541#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500542 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700543 void *custom_data;
544#endif
545
Daniel Leungfc182432018-08-16 15:42:28 -0700546#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
547 struct _thread_userspace_local_data *userspace_local_data;
548#endif
549
Andrew Boie73abd322017-04-04 13:19:13 -0700550#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700551#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500552 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700553 int errno_var;
554#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700555#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700556
557#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500558 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700559 struct _thread_stack_info stack_info;
560#endif /* CONFIG_THREAD_STACK_INFO */
561
Chunlin Hane9c97022017-07-07 20:29:30 +0800562#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500563 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800564 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700566 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800567#endif /* CONFIG_USERSPACE */
568
Andy Ross042d8ec2017-12-09 08:37:20 -0800569#if defined(CONFIG_USE_SWITCH)
570 /* When using __switch() a few previously arch-specific items
571 * become part of the core OS
572 */
573
Patrik Flykt4344e272019-03-08 14:19:05 -0700574 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800575 int swap_retval;
576
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600577 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800578 void *switch_handle;
579#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500580 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700581 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800582
Anas Nashifce78d162018-05-24 12:43:11 -0500583 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700584 struct _thread_arch arch;
585};
586
587typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400588typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400589
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400590enum execution_context_types {
591 K_ISR = 0,
592 K_COOP_THREAD,
593 K_PREEMPT_THREAD,
594};
595
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500597 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100598 * @{
599 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530600typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
601 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100602
603/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530604 * @brief Iterate over all the threads in the system.
605 *
606 * This routine iterates over all the threads in the system and
607 * calls the user_cb function for each thread.
608 *
609 * @param user_cb Pointer to the user callback function.
610 * @param user_data Pointer to user data.
611 *
612 * @note CONFIG_THREAD_MONITOR must be set for this function
613 * to be effective. Also this API uses irq_lock to protect the
614 * _kernel.threads list which means creation of new threads and
615 * terminations of existing threads are blocked until this
616 * API returns.
617 *
618 * @return N/A
619 */
620extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
621
Anas Nashif166f5192018-02-25 08:02:36 -0600622/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100623
624/**
Allan Stephensc98da842016-11-11 15:45:03 -0500625 * @defgroup thread_apis Thread APIs
626 * @ingroup kernel_apis
627 * @{
628 */
629
Benjamin Walshed240f22017-01-22 13:05:08 -0500630#endif /* !_ASMLANGUAGE */
631
632
633/*
634 * Thread user options. May be needed by assembly code. Common part uses low
635 * bits, arch-specific use high bits.
636 */
637
Anas Nashifa541e932018-05-24 11:19:16 -0500638/**
639 * @brief system thread that must not abort
640 * @req K-THREAD-000
641 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700642#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500643
644#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500645/**
646 * @brief thread uses floating point registers
647 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700648#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500649#endif
650
Anas Nashifa541e932018-05-24 11:19:16 -0500651/**
652 * @brief user mode thread
653 *
654 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700655 * has additional restrictions
656 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700657#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700658
Anas Nashifa541e932018-05-24 11:19:16 -0500659/**
660 * @brief Inherit Permissions
661 *
662 * @details
663 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700664 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
665 * is not enabled.
666 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700667#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700668
Benjamin Walshed240f22017-01-22 13:05:08 -0500669#ifdef CONFIG_X86
670/* x86 Bitmask definitions for threads user options */
671
672#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
673/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700674#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500675#endif
676#endif
677
678/* end - thread options */
679
680#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700682 * @brief Create a thread.
683 *
684 * This routine initializes a thread, then schedules it for execution.
685 *
686 * The new thread may be scheduled for immediate execution or a delayed start.
687 * If the newly spawned thread does not have a delayed start the kernel
688 * scheduler may preempt the current thread to allow the new thread to
689 * execute.
690 *
691 * Thread options are architecture-specific, and can include K_ESSENTIAL,
692 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
693 * them using "|" (the logical OR operator).
694 *
695 * Historically, users often would use the beginning of the stack memory region
696 * to store the struct k_thread data, although corruption will occur if the
697 * stack overflows this region and stack protection features may not detect this
698 * situation.
699 *
700 * @param new_thread Pointer to uninitialized struct k_thread
701 * @param stack Pointer to the stack space.
702 * @param stack_size Stack size in bytes.
703 * @param entry Thread entry function.
704 * @param p1 1st entry point parameter.
705 * @param p2 2nd entry point parameter.
706 * @param p3 3rd entry point parameter.
707 * @param prio Thread priority.
708 * @param options Thread options.
709 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
710 *
711 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400712 *
713 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700714 */
Andrew Boie662c3452017-10-02 10:51:18 -0700715__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700716 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700717 size_t stack_size,
718 k_thread_entry_t entry,
719 void *p1, void *p2, void *p3,
720 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700721
Andrew Boie3f091b52017-08-30 14:34:14 -0700722/**
723 * @brief Drop a thread's privileges permanently to user mode
724 *
725 * @param entry Function to start executing from
726 * @param p1 1st entry point parameter
727 * @param p2 2nd entry point parameter
728 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400729 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700730 */
731extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
732 void *p1, void *p2,
733 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700734
Andrew Boied26cf2d2017-03-30 13:07:02 -0700735/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530736 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700737 *
738 * This is a convenience function. For the provided thread, grant access to
739 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700740 *
741 * The thread object must be initialized (i.e. running). The objects don't
742 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530743 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700744 *
745 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530746 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400747 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700748 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530749#define k_thread_access_grant(thread, ...) \
750 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700751
752/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700753 * @brief Assign a resource memory pool to a thread
754 *
755 * By default, threads have no resource pool assigned unless their parent
756 * thread has a resource pool, in which case it is inherited. Multiple
757 * threads may be assigned to the same memory pool.
758 *
759 * Changing a thread's resource pool will not migrate allocations from the
760 * previous pool.
761 *
762 * @param thread Target thread to assign a memory pool for resource requests,
763 * or NULL if the thread should no longer have a memory pool.
764 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400765 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700766 */
767static inline void k_thread_resource_pool_assign(struct k_thread *thread,
768 struct k_mem_pool *pool)
769{
770 thread->resource_pool = pool;
771}
772
773#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
774/**
775 * @brief Assign the system heap as a thread's resource pool
776 *
777 * Similar to k_thread_resource_pool_assign(), but the thread will use
778 * the kernel heap to draw memory.
779 *
780 * Use with caution, as a malicious thread could perform DoS attacks on the
781 * kernel heap.
782 *
783 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400784 *
785 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700786 */
787void k_thread_system_pool_assign(struct k_thread *thread);
788#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
789
790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500791 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700793 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700795 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200797 * @return Zero if the requested time has elapsed or the number of milliseconds
798 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700800__syscall s32_t k_sleep(s32_t ms);
801
802/**
803 * @brief Put the current thread to sleep with microsecond resolution.
804 *
805 * This function is unlikely to work as expected without kernel tuning.
806 * In particular, because the lower bound on the duration of a sleep is
807 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
808 * to achieve the resolution desired. The implications of doing this must
809 * be understood before attempting to use k_usleep(). Use with caution.
810 *
811 * @param us Number of microseconds to sleep.
812 *
813 * @return Zero if the requested time has elapsed or the number of microseconds
814 * left to sleep, if thread was woken up by \ref k_wakeup call.
815 */
816__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817
818/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
821 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 * @return N/A
825 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800826__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827
828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
835 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400836 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 */
Andrew Boie468190a2017-09-29 14:00:48 -0700838__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839
840/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * If @a thread is not currently sleeping, the routine has no effect.
846 *
847 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
849 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400850 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 */
Andrew Boie468190a2017-09-29 14:00:48 -0700852__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853
854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400858 *
859 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700861__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862
863/**
Allan Stephensc98da842016-11-11 15:45:03 -0500864 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * This routine permanently stops execution of @a thread. The thread is taken
867 * off all kernel queues it is part of (i.e. the ready queue, the timeout
868 * queue, or a kernel object wait queue). However, any kernel resources the
869 * thread might currently own (such as mutexes or memory blocks) are not
870 * released. It is the responsibility of the caller of this routine to ensure
871 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
875 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400876 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 */
Andrew Boie468190a2017-09-29 14:00:48 -0700878__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400879
Andrew Boie7d627c52017-08-30 11:01:56 -0700880
881/**
882 * @brief Start an inactive thread
883 *
884 * If a thread was created with K_FOREVER in the delay parameter, it will
885 * not be added to the scheduling queue until this function is called
886 * on it.
887 *
888 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400889 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700890 */
Andrew Boie468190a2017-09-29 14:00:48 -0700891__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700892
Allan Stephensc98da842016-11-11 15:45:03 -0500893/**
894 * @cond INTERNAL_HIDDEN
895 */
896
Benjamin Walshd211a522016-12-06 11:44:01 -0500897/* timeout has timed out and is not on _timeout_q anymore */
898#define _EXPIRED (-2)
899
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400900struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700901 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700902 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400903 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700904 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500905 void *init_p1;
906 void *init_p2;
907 void *init_p3;
908 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500909 u32_t init_options;
910 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500911 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600912 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913};
914
Andrew Boied26cf2d2017-03-30 13:07:02 -0700915#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400916 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600917 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500918 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700919 .init_thread = (thread), \
920 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500921 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700922 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400923 .init_p1 = (void *)p1, \
924 .init_p2 = (void *)p2, \
925 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500926 .init_prio = (prio), \
927 .init_options = (options), \
928 .init_delay = (delay), \
929 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600930 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400931 }
932
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400933/**
Allan Stephensc98da842016-11-11 15:45:03 -0500934 * INTERNAL_HIDDEN @endcond
935 */
936
937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * @brief Statically define and initialize a thread.
939 *
940 * The thread may be scheduled for immediate execution or a delayed start.
941 *
942 * Thread options are architecture-specific, and can include K_ESSENTIAL,
943 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
944 * them using "|" (the logical OR operator).
945 *
946 * The ID of the thread can be accessed using:
947 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500948 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 *
950 * @param name Name of the thread.
951 * @param stack_size Stack size in bytes.
952 * @param entry Thread entry function.
953 * @param p1 1st entry point parameter.
954 * @param p2 2nd entry point parameter.
955 * @param p3 3rd entry point parameter.
956 * @param prio Thread priority.
957 * @param options Thread options.
958 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400959 *
Anas Nashif47420d02018-05-24 14:20:56 -0400960 * @req K-THREAD-010
961 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400962 * @internal It has been observed that the x86 compiler by default aligns
963 * these _static_thread_data structures to 32-byte boundaries, thereby
964 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400965 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400966 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500967#define K_THREAD_DEFINE(name, stack_size, \
968 entry, p1, p2, p3, \
969 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700970 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400971 struct k_thread _k_thread_obj_##name; \
972 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700973 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
974 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500975 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600976 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700977 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400978
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500980 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500984 * @param thread ID of thread whose priority is needed.
985 *
986 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400987 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700989__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990
991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
996 * Rescheduling can occur immediately depending on the priority @a thread is
997 * set to:
998 *
999 * - If its priority is raised above the priority of the caller of this
1000 * function, and the caller is preemptible, @a thread will be scheduled in.
1001 *
1002 * - If the caller operates on itself, it lowers its priority below that of
1003 * other threads in the system, and the caller is preemptible, the thread of
1004 * highest priority will be scheduled in.
1005 *
1006 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1007 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1008 * highest priority.
1009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011 * @param prio New priority.
1012 *
1013 * @warning Changing the priority of a thread currently involved in mutex
1014 * priority inheritance may result in undefined behavior.
1015 *
1016 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001017 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018 */
Andrew Boie468190a2017-09-29 14:00:48 -07001019__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020
Andy Ross4a2e50f2018-05-15 11:06:25 -07001021
1022#ifdef CONFIG_SCHED_DEADLINE
1023/**
1024 * @brief Set deadline expiration time for scheduler
1025 *
1026 * This sets the "deadline" expiration as a time delta from the
1027 * current time, in the same units used by k_cycle_get_32(). The
1028 * scheduler (when deadline scheduling is enabled) will choose the
1029 * next expiring thread when selecting between threads at the same
1030 * static priority. Threads at different priorities will be scheduled
1031 * according to their static priority.
1032 *
1033 * @note Deadlines that are negative (i.e. in the past) are still seen
1034 * as higher priority than others, even if the thread has "finished"
1035 * its work. If you don't want it scheduled anymore, you have to
1036 * reset the deadline into the future, block/pend the thread, or
1037 * modify its priority with k_thread_priority_set().
1038 *
1039 * @note Despite the API naming, the scheduler makes no guarantees the
1040 * the thread WILL be scheduled within that deadline, nor does it take
1041 * extra metadata (like e.g. the "runtime" and "period" parameters in
1042 * Linux sched_setattr()) that allows the kernel to validate the
1043 * scheduling for achievability. Such features could be implemented
1044 * above this call, which is simply input to the priority selection
1045 * logic.
1046 *
Anas Nashif240c5162019-06-10 12:25:50 -04001047 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001048 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001049 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1050 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001051 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001052 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001053 * @param thread A thread on which to set the deadline
1054 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001055 *
1056 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001057 */
1058__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1059#endif
1060
Andy Rossab46b1b2019-01-30 15:00:42 -08001061#ifdef CONFIG_SCHED_CPU_MASK
1062/**
1063 * @brief Sets all CPU enable masks to zero
1064 *
1065 * After this returns, the thread will no longer be schedulable on any
1066 * CPUs. The thread must not be currently runnable.
1067 *
Anas Nashif240c5162019-06-10 12:25:50 -04001068 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001069 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001070 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1071 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001072 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001073 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001074 * @param thread Thread to operate upon
1075 * @return Zero on success, otherwise error code
1076 */
1077int k_thread_cpu_mask_clear(k_tid_t thread);
1078
1079/**
1080 * @brief Sets all CPU enable masks to one
1081 *
1082 * After this returns, the thread will be schedulable on any CPU. The
1083 * thread must not be currently runnable.
1084 *
Anas Nashif240c5162019-06-10 12:25:50 -04001085 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001086 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001087 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1088 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001089 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001090 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001091 * @param thread Thread to operate upon
1092 * @return Zero on success, otherwise error code
1093 */
1094int k_thread_cpu_mask_enable_all(k_tid_t thread);
1095
1096/**
1097 * @brief Enable thread to run on specified CPU
1098 *
1099 * The thread must not be currently runnable.
1100 *
Anas Nashif240c5162019-06-10 12:25:50 -04001101 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001102 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001103 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1104 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001105 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001106 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001107 * @param thread Thread to operate upon
1108 * @param cpu CPU index
1109 * @return Zero on success, otherwise error code
1110 */
1111int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1112
1113/**
1114 * @brief Prevent thread to run on specified CPU
1115 *
1116 * The thread must not be currently runnable.
1117 *
Anas Nashif240c5162019-06-10 12:25:50 -04001118 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001119 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001120 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1121 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001122 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001123 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001124 * @param thread Thread to operate upon
1125 * @param cpu CPU index
1126 * @return Zero on success, otherwise error code
1127 */
1128int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1129#endif
1130
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * This routine prevents the kernel scheduler from making @a thread the
1135 * current thread. All other internal operations on @a thread are still
1136 * performed; for example, any timeout it is waiting on keeps ticking,
1137 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * If @a thread is already suspended, the routine has no effect.
1140 *
1141 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142 *
1143 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001144 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 */
Andrew Boie468190a2017-09-29 14:00:48 -07001146__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147
1148/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001149 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * This routine allows the kernel scheduler to make @a thread the current
1152 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * If @a thread is not currently suspended, the routine has no effect.
1155 *
1156 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
1158 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001159 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160 */
Andrew Boie468190a2017-09-29 14:00:48 -07001161__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001162
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001166 * This routine specifies how the scheduler will perform time slicing of
1167 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * To enable time slicing, @a slice must be non-zero. The scheduler
1170 * ensures that no thread runs for more than the specified time limit
1171 * before other threads of that priority are given a chance to execute.
1172 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001173 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001175 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001176 * execute. Once the scheduler selects a thread for execution, there is no
1177 * minimum guaranteed time the thread will execute before threads of greater or
1178 * equal priority are scheduled.
1179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 * for execution, this routine has no effect; the thread is immediately
1182 * rescheduled after the slice period expires.
1183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001184 * To disable timeslicing, set both @a slice and @a prio to zero.
1185 *
1186 * @param slice Maximum time slice length (in milliseconds).
1187 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001188 *
1189 * @return N/A
1190 */
Kumar Galacc334c72017-04-21 10:55:34 -05001191extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001192
Anas Nashif166f5192018-02-25 08:02:36 -06001193/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001194
1195/**
1196 * @addtogroup isr_apis
1197 * @{
1198 */
1199
1200/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001201 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202 *
Allan Stephensc98da842016-11-11 15:45:03 -05001203 * This routine allows the caller to customize its actions, depending on
1204 * whether it is a thread or an ISR.
1205 *
1206 * @note Can be called by ISRs.
1207 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001208 * @return false if invoked by a thread.
1209 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001210 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001211extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001212
Benjamin Walsh445830d2016-11-10 15:54:27 -05001213/**
1214 * @brief Determine if code is running in a preemptible thread.
1215 *
Allan Stephensc98da842016-11-11 15:45:03 -05001216 * This routine allows the caller to customize its actions, depending on
1217 * whether it can be preempted by another thread. The routine returns a 'true'
1218 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001219 *
Allan Stephensc98da842016-11-11 15:45:03 -05001220 * - The code is running in a thread, not at ISR.
1221 * - The thread's priority is in the preemptible range.
1222 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001223 *
Allan Stephensc98da842016-11-11 15:45:03 -05001224 * @note Can be called by ISRs.
1225 *
1226 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001227 * @return Non-zero if invoked by a preemptible thread.
1228 */
Andrew Boie468190a2017-09-29 14:00:48 -07001229__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001230
Allan Stephensc98da842016-11-11 15:45:03 -05001231/**
Anas Nashif166f5192018-02-25 08:02:36 -06001232 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001233 */
1234
1235/**
1236 * @addtogroup thread_apis
1237 * @{
1238 */
1239
1240/**
1241 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001242 *
Allan Stephensc98da842016-11-11 15:45:03 -05001243 * This routine prevents the current thread from being preempted by another
1244 * thread by instructing the scheduler to treat it as a cooperative thread.
1245 * If the thread subsequently performs an operation that makes it unready,
1246 * it will be context switched out in the normal manner. When the thread
1247 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001248 *
Allan Stephensc98da842016-11-11 15:45:03 -05001249 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001250 *
Allan Stephensc98da842016-11-11 15:45:03 -05001251 * @note k_sched_lock() and k_sched_unlock() should normally be used
1252 * when the operation being performed can be safely interrupted by ISRs.
1253 * However, if the amount of processing involved is very small, better
1254 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001255 *
1256 * @return N/A
1257 */
1258extern void k_sched_lock(void);
1259
Allan Stephensc98da842016-11-11 15:45:03 -05001260/**
1261 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001262 *
Allan Stephensc98da842016-11-11 15:45:03 -05001263 * This routine reverses the effect of a previous call to k_sched_lock().
1264 * A thread must call the routine once for each time it called k_sched_lock()
1265 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001266 *
1267 * @return N/A
1268 */
1269extern void k_sched_unlock(void);
1270
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001271/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001274 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001276 * Custom data is not used by the kernel itself, and is freely available
1277 * for a thread to use as it sees fit. It can be used as a framework
1278 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001280 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001281 *
1282 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001283 *
1284 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001285 */
Andrew Boie468190a2017-09-29 14:00:48 -07001286__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001287
1288/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001289 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001291 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001294 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 */
Andrew Boie468190a2017-09-29 14:00:48 -07001296__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001297
1298/**
Anas Nashif57554052018-03-03 02:31:05 -06001299 * @brief Set current thread name
1300 *
1301 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1302 * tracing and debugging.
1303 *
1304 */
1305__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1306
1307/**
1308 * @brief Get thread name
1309 *
1310 * Get the name of a thread
1311 *
1312 * @param thread_id Thread ID
1313 *
1314 */
1315__syscall const char *k_thread_name_get(k_tid_t thread_id);
1316
1317/**
Andy Rosscfe62032018-09-29 07:34:55 -07001318 * @}
1319 */
1320
1321/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001322 * @addtogroup clock_apis
1323 * @{
1324 */
1325
1326/**
1327 * @brief Generate null timeout delay.
1328 *
1329 * This macro generates a timeout delay that that instructs a kernel API
1330 * not to wait if the requested operation cannot be performed immediately.
1331 *
1332 * @return Timeout delay value.
1333 */
1334#define K_NO_WAIT 0
1335
1336/**
1337 * @brief Generate timeout delay from milliseconds.
1338 *
1339 * This macro generates a timeout delay that that instructs a kernel API
1340 * to wait up to @a ms milliseconds to perform the requested operation.
1341 *
1342 * @param ms Duration in milliseconds.
1343 *
1344 * @return Timeout delay value.
1345 */
Johan Hedberg14471692016-11-13 10:52:15 +02001346#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001347
1348/**
1349 * @brief Generate timeout delay from seconds.
1350 *
1351 * This macro generates a timeout delay that that instructs a kernel API
1352 * to wait up to @a s seconds to perform the requested operation.
1353 *
1354 * @param s Duration in seconds.
1355 *
1356 * @return Timeout delay value.
1357 */
Johan Hedberg14471692016-11-13 10:52:15 +02001358#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001359
1360/**
1361 * @brief Generate timeout delay from minutes.
1362 *
1363 * This macro generates a timeout delay that that instructs a kernel API
1364 * to wait up to @a m minutes to perform the requested operation.
1365 *
1366 * @param m Duration in minutes.
1367 *
1368 * @return Timeout delay value.
1369 */
Johan Hedberg14471692016-11-13 10:52:15 +02001370#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001371
1372/**
1373 * @brief Generate timeout delay from hours.
1374 *
1375 * This macro generates a timeout delay that that instructs a kernel API
1376 * to wait up to @a h hours to perform the requested operation.
1377 *
1378 * @param h Duration in hours.
1379 *
1380 * @return Timeout delay value.
1381 */
Johan Hedberg14471692016-11-13 10:52:15 +02001382#define K_HOURS(h) K_MINUTES((h) * 60)
1383
Allan Stephensc98da842016-11-11 15:45:03 -05001384/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001385 * @brief Generate infinite timeout delay.
1386 *
1387 * This macro generates a timeout delay that that instructs a kernel API
1388 * to wait as long as necessary to perform the requested operation.
1389 *
1390 * @return Timeout delay value.
1391 */
1392#define K_FOREVER (-1)
1393
1394/**
Anas Nashif166f5192018-02-25 08:02:36 -06001395 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001396 */
1397
1398/**
Allan Stephensc98da842016-11-11 15:45:03 -05001399 * @cond INTERNAL_HIDDEN
1400 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001401
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402struct k_timer {
1403 /*
1404 * _timeout structure must be first here if we want to use
1405 * dynamic timer allocation. timeout.node is used in the double-linked
1406 * list of free timers
1407 */
1408 struct _timeout timeout;
1409
Allan Stephens45bfa372016-10-12 12:39:42 -05001410 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411 _wait_q_t wait_q;
1412
1413 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001414 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415
1416 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001417 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001418
1419 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001420 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001421
Allan Stephens45bfa372016-10-12 12:39:42 -05001422 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001423 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001424
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001425 /* user-specific data, also used to support legacy features */
1426 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001427
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001428 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429};
1430
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001431#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001432 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001433 .timeout = { \
1434 .node = {},\
1435 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001436 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001437 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001438 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001439 .expiry_fn = expiry, \
1440 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001441 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001442 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001443 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001444 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445 }
1446
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001447#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001448
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001449/**
Allan Stephensc98da842016-11-11 15:45:03 -05001450 * INTERNAL_HIDDEN @endcond
1451 */
1452
1453/**
1454 * @defgroup timer_apis Timer APIs
1455 * @ingroup kernel_apis
1456 * @{
1457 */
1458
1459/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001460 * @typedef k_timer_expiry_t
1461 * @brief Timer expiry function type.
1462 *
1463 * A timer's expiry function is executed by the system clock interrupt handler
1464 * each time the timer expires. The expiry function is optional, and is only
1465 * invoked if the timer has been initialized with one.
1466 *
1467 * @param timer Address of timer.
1468 *
1469 * @return N/A
1470 */
1471typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1472
1473/**
1474 * @typedef k_timer_stop_t
1475 * @brief Timer stop function type.
1476 *
1477 * A timer's stop function is executed if the timer is stopped prematurely.
1478 * The function runs in the context of the thread that stops the timer.
1479 * The stop function is optional, and is only invoked if the timer has been
1480 * initialized with one.
1481 *
1482 * @param timer Address of timer.
1483 *
1484 * @return N/A
1485 */
1486typedef void (*k_timer_stop_t)(struct k_timer *timer);
1487
1488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001493 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 *
1495 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001496 * @param expiry_fn Function to invoke each time the timer expires.
1497 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001499#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001500 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001501 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502
Allan Stephens45bfa372016-10-12 12:39:42 -05001503/**
1504 * @brief Initialize a timer.
1505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001506 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001507 *
1508 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001509 * @param expiry_fn Function to invoke each time the timer expires.
1510 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001511 *
1512 * @return N/A
1513 */
1514extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001515 k_timer_expiry_t expiry_fn,
1516 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001517
Allan Stephens45bfa372016-10-12 12:39:42 -05001518/**
1519 * @brief Start a timer.
1520 *
1521 * This routine starts a timer, and resets its status to zero. The timer
1522 * begins counting down using the specified duration and period values.
1523 *
1524 * Attempting to start a timer that is already running is permitted.
1525 * The timer's status is reset to zero and the timer begins counting down
1526 * using the new duration and period values.
1527 *
1528 * @param timer Address of timer.
1529 * @param duration Initial timer duration (in milliseconds).
1530 * @param period Timer period (in milliseconds).
1531 *
1532 * @return N/A
1533 */
Andrew Boiea354d492017-09-29 16:22:28 -07001534__syscall void k_timer_start(struct k_timer *timer,
1535 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001536
1537/**
1538 * @brief Stop a timer.
1539 *
1540 * This routine stops a running timer prematurely. The timer's stop function,
1541 * if one exists, is invoked by the caller.
1542 *
1543 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001545 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001546 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1547 * if @a k_timer_stop is to be called from ISRs.
1548 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001549 * @param timer Address of timer.
1550 *
1551 * @return N/A
1552 */
Andrew Boiea354d492017-09-29 16:22:28 -07001553__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001554
1555/**
1556 * @brief Read timer status.
1557 *
1558 * This routine reads the timer's status, which indicates the number of times
1559 * it has expired since its status was last read.
1560 *
1561 * Calling this routine resets the timer's status to zero.
1562 *
1563 * @param timer Address of timer.
1564 *
1565 * @return Timer status.
1566 */
Andrew Boiea354d492017-09-29 16:22:28 -07001567__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001568
1569/**
1570 * @brief Synchronize thread to timer expiration.
1571 *
1572 * This routine blocks the calling thread until the timer's status is non-zero
1573 * (indicating that it has expired at least once since it was last examined)
1574 * or the timer is stopped. If the timer status is already non-zero,
1575 * or the timer is already stopped, the caller continues without waiting.
1576 *
1577 * Calling this routine resets the timer's status to zero.
1578 *
1579 * This routine must not be used by interrupt handlers, since they are not
1580 * allowed to block.
1581 *
1582 * @param timer Address of timer.
1583 *
1584 * @return Timer status.
1585 */
Andrew Boiea354d492017-09-29 16:22:28 -07001586__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001587
Andy Ross52e444b2018-09-28 09:06:37 -07001588extern s32_t z_timeout_remaining(struct _timeout *timeout);
1589
Allan Stephens45bfa372016-10-12 12:39:42 -05001590/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001592 *
1593 * This routine computes the (approximate) time remaining before a running
1594 * timer next expires. If the timer is not running, it returns zero.
1595 *
1596 * @param timer Address of timer.
1597 *
1598 * @return Remaining time (in milliseconds).
1599 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001600__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001601
Patrik Flykt4344e272019-03-08 14:19:05 -07001602static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001603{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001604 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1605 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001606}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001607
Allan Stephensc98da842016-11-11 15:45:03 -05001608/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001609 * @brief Associate user-specific data with a timer.
1610 *
1611 * This routine records the @a user_data with the @a timer, to be retrieved
1612 * later.
1613 *
1614 * It can be used e.g. in a timer handler shared across multiple subsystems to
1615 * retrieve data specific to the subsystem this timer is associated with.
1616 *
1617 * @param timer Address of timer.
1618 * @param user_data User data to associate with the timer.
1619 *
1620 * @return N/A
1621 */
Andrew Boiea354d492017-09-29 16:22:28 -07001622__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1623
Anas Nashif954d5502018-02-25 08:37:28 -06001624/**
1625 * @internal
1626 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001627static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001628 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001629{
1630 timer->user_data = user_data;
1631}
1632
1633/**
1634 * @brief Retrieve the user-specific data from a timer.
1635 *
1636 * @param timer Address of timer.
1637 *
1638 * @return The user data.
1639 */
Andrew Boiea354d492017-09-29 16:22:28 -07001640__syscall void *k_timer_user_data_get(struct k_timer *timer);
1641
Patrik Flykt4344e272019-03-08 14:19:05 -07001642static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001643{
1644 return timer->user_data;
1645}
1646
Anas Nashif166f5192018-02-25 08:02:36 -06001647/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001648
Allan Stephensc98da842016-11-11 15:45:03 -05001649/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001650 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001651 * @{
1652 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001653
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657 * This routine returns the elapsed time since the system booted,
1658 * in milliseconds.
1659 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001660 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001661 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001662 * While this function returns time in milliseconds, it does
1663 * not mean it has millisecond resolution. The actual resolution depends on
1664 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the
1665 * default setting of 100, system time is updated in increments of 10ms.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001666 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001667 *
1668 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001669 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001670__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001671
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001672/**
1673 * @brief Enable clock always on in tickless kernel
1674 *
Andy Ross1db9f182019-06-25 10:09:45 -07001675 * Deprecated. This does nothing (it was always just a hint). This
1676 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1677 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001678 *
1679 * @retval prev_status Previous status of always on flag
1680 */
Andy Ross1db9f182019-06-25 10:09:45 -07001681/* LCOV_EXCL_START */
1682__deprecated static inline int k_enable_sys_clock_always_on(void)
1683{
1684 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1685 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1686
1687 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1688}
1689/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001690
1691/**
1692 * @brief Disable clock always on in tickless kernel
1693 *
Andy Ross1db9f182019-06-25 10:09:45 -07001694 * Deprecated. This does nothing (it was always just a hint). This
1695 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1696 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001697 */
Andy Ross1db9f182019-06-25 10:09:45 -07001698/* LCOV_EXCL_START */
1699__deprecated static inline void k_disable_sys_clock_always_on(void)
1700{
1701 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1702 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1703}
1704/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001705
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001707 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * This routine returns the lower 32-bits of the elapsed time since the system
1710 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * This routine can be more efficient than k_uptime_get(), as it reduces the
1713 * need for interrupt locking and 64-bit math. However, the 32-bit result
1714 * cannot hold a system uptime time larger than approximately 50 days, so the
1715 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001717 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001718 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001719 * While this function returns time in milliseconds, it does
1720 * not mean it has millisecond resolution. The actual resolution depends on
1721 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the
1722 * default setting of 100, system time is updated in increments of 10ms.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001723 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001724 *
1725 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001726 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001727__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001728
1729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * This routine computes the elapsed time between the current system uptime
1733 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @param reftime Pointer to a reference time, which is updated to the current
1736 * uptime upon return.
1737 *
1738 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001739 */
Andy Ross987c0e52018-09-27 16:50:00 -07001740static inline s64_t k_uptime_delta(s64_t *reftime)
1741{
1742 s64_t uptime, delta;
1743
1744 uptime = k_uptime_get();
1745 delta = uptime - *reftime;
1746 *reftime = uptime;
1747
1748 return delta;
1749}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001750
1751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754 * This routine computes the elapsed time between the current system uptime
1755 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1758 * need for interrupt locking and 64-bit math. However, the 32-bit result
1759 * cannot hold an elapsed time larger than approximately 50 days, so the
1760 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * @param reftime Pointer to a reference time, which is updated to the current
1763 * uptime upon return.
1764 *
1765 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001766 */
Andy Ross987c0e52018-09-27 16:50:00 -07001767static inline u32_t k_uptime_delta_32(s64_t *reftime)
1768{
1769 return (u32_t)k_uptime_delta(reftime);
1770}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001771
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * This routine returns the current time, as measured by the system's hardware
1776 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001779 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001780#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001781
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001782/**
Anas Nashif166f5192018-02-25 08:02:36 -06001783 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001784 */
1785
Allan Stephensc98da842016-11-11 15:45:03 -05001786/**
1787 * @cond INTERNAL_HIDDEN
1788 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001789
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001790struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001791 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001792 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001793 union {
1794 _wait_q_t wait_q;
1795
1796 _POLL_EVENT;
1797 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001798
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001799 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001800};
1801
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001802#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001803 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001804 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001805 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001806 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001807 _OBJECT_TRACING_INIT \
1808 }
1809
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001810#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1811
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001812extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1813
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001814/**
1815 * INTERNAL_HIDDEN @endcond
1816 */
1817
1818/**
1819 * @defgroup queue_apis Queue APIs
1820 * @ingroup kernel_apis
1821 * @{
1822 */
1823
1824/**
1825 * @brief Initialize a queue.
1826 *
1827 * This routine initializes a queue object, prior to its first use.
1828 *
1829 * @param queue Address of the queue.
1830 *
1831 * @return N/A
1832 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001833__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001834
1835/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001836 * @brief Cancel waiting on a queue.
1837 *
1838 * This routine causes first thread pending on @a queue, if any, to
1839 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001840 * If the queue is being waited on by k_poll(), it will return with
1841 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1842 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001843 *
1844 * @note Can be called by ISRs.
1845 *
1846 * @param queue Address of the queue.
1847 *
1848 * @return N/A
1849 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001850__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001851
1852/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001853 * @brief Append an element to the end of a queue.
1854 *
1855 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001856 * aligned on a word boundary, and the first word of the item is reserved
1857 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001858 *
1859 * @note Can be called by ISRs.
1860 *
1861 * @param queue Address of the queue.
1862 * @param data Address of the data item.
1863 *
1864 * @return N/A
1865 */
1866extern void k_queue_append(struct k_queue *queue, void *data);
1867
1868/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001869 * @brief Append an element to a queue.
1870 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001871 * This routine appends a data item to @a queue. There is an implicit memory
1872 * allocation to create an additional temporary bookkeeping data structure from
1873 * the calling thread's resource pool, which is automatically freed when the
1874 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001875 *
1876 * @note Can be called by ISRs.
1877 *
1878 * @param queue Address of the queue.
1879 * @param data Address of the data item.
1880 *
1881 * @retval 0 on success
1882 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1883 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301884__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001885
1886/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001887 * @brief Prepend an element to a queue.
1888 *
1889 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001890 * aligned on a word boundary, and the first word of the item is reserved
1891 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001892 *
1893 * @note Can be called by ISRs.
1894 *
1895 * @param queue Address of the queue.
1896 * @param data Address of the data item.
1897 *
1898 * @return N/A
1899 */
1900extern void k_queue_prepend(struct k_queue *queue, void *data);
1901
1902/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001903 * @brief Prepend an element to a queue.
1904 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001905 * This routine prepends a data item to @a queue. There is an implicit memory
1906 * allocation to create an additional temporary bookkeeping data structure from
1907 * the calling thread's resource pool, which is automatically freed when the
1908 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001909 *
1910 * @note Can be called by ISRs.
1911 *
1912 * @param queue Address of the queue.
1913 * @param data Address of the data item.
1914 *
1915 * @retval 0 on success
1916 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1917 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301918__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001919
1920/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001921 * @brief Inserts an element to a queue.
1922 *
1923 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001924 * data item must be aligned on a word boundary, and the first word of
1925 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001926 *
1927 * @note Can be called by ISRs.
1928 *
1929 * @param queue Address of the queue.
1930 * @param prev Address of the previous data item.
1931 * @param data Address of the data item.
1932 *
1933 * @return N/A
1934 */
1935extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1936
1937/**
1938 * @brief Atomically append a list of elements to a queue.
1939 *
1940 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001941 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001942 * in each data item pointing to the next data item; the list must be
1943 * NULL-terminated.
1944 *
1945 * @note Can be called by ISRs.
1946 *
1947 * @param queue Address of the queue.
1948 * @param head Pointer to first node in singly-linked list.
1949 * @param tail Pointer to last node in singly-linked list.
1950 *
1951 * @return N/A
1952 */
1953extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1954
1955/**
1956 * @brief Atomically add a list of elements to a queue.
1957 *
1958 * This routine adds a list of data items to @a queue in one operation.
1959 * The data items must be in a singly-linked list implemented using a
1960 * sys_slist_t object. Upon completion, the original list is empty.
1961 *
1962 * @note Can be called by ISRs.
1963 *
1964 * @param queue Address of the queue.
1965 * @param list Pointer to sys_slist_t object.
1966 *
1967 * @return N/A
1968 */
1969extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1970
1971/**
1972 * @brief Get an element from a queue.
1973 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001974 * This routine removes first data item from @a queue. The first word of the
1975 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001976 *
1977 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1978 *
1979 * @param queue Address of the queue.
1980 * @param timeout Waiting period to obtain a data item (in milliseconds),
1981 * or one of the special values K_NO_WAIT and K_FOREVER.
1982 *
1983 * @return Address of the data item if successful; NULL if returned
1984 * without waiting, or waiting period timed out.
1985 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001986__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001987
1988/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001989 * @brief Remove an element from a queue.
1990 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001991 * This routine removes data item from @a queue. The first word of the
1992 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001993 * rely on sys_slist_find_and_remove which is not a constant time operation.
1994 *
1995 * @note Can be called by ISRs
1996 *
1997 * @param queue Address of the queue.
1998 * @param data Address of the data item.
1999 *
2000 * @return true if data item was removed
2001 */
2002static inline bool k_queue_remove(struct k_queue *queue, void *data)
2003{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002004 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002005}
2006
2007/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002008 * @brief Append an element to a queue only if it's not present already.
2009 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002010 * This routine appends data item to @a queue. The first word of the data
2011 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002012 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2013 *
2014 * @note Can be called by ISRs
2015 *
2016 * @param queue Address of the queue.
2017 * @param data Address of the data item.
2018 *
2019 * @return true if data item was added, false if not
2020 */
2021static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2022{
2023 sys_sfnode_t *test;
2024
2025 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2026 if (test == (sys_sfnode_t *) data) {
2027 return false;
2028 }
2029 }
2030
2031 k_queue_append(queue, data);
2032 return true;
2033}
2034
2035/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002036 * @brief Query a queue to see if it has data available.
2037 *
2038 * Note that the data might be already gone by the time this function returns
2039 * if other threads are also trying to read from the queue.
2040 *
2041 * @note Can be called by ISRs.
2042 *
2043 * @param queue Address of the queue.
2044 *
2045 * @return Non-zero if the queue is empty.
2046 * @return 0 if data is available.
2047 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002048__syscall int k_queue_is_empty(struct k_queue *queue);
2049
Patrik Flykt4344e272019-03-08 14:19:05 -07002050static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002051{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002052 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002053}
2054
2055/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002056 * @brief Peek element at the head of queue.
2057 *
2058 * Return element from the head of queue without removing it.
2059 *
2060 * @param queue Address of the queue.
2061 *
2062 * @return Head element, or NULL if queue is empty.
2063 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002064__syscall void *k_queue_peek_head(struct k_queue *queue);
2065
Patrik Flykt4344e272019-03-08 14:19:05 -07002066static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002067{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002068 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002069}
2070
2071/**
2072 * @brief Peek element at the tail of queue.
2073 *
2074 * Return element from the tail of queue without removing it.
2075 *
2076 * @param queue Address of the queue.
2077 *
2078 * @return Tail element, or NULL if queue is empty.
2079 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002080__syscall void *k_queue_peek_tail(struct k_queue *queue);
2081
Patrik Flykt4344e272019-03-08 14:19:05 -07002082static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002083{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002084 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002085}
2086
2087/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002088 * @brief Statically define and initialize a queue.
2089 *
2090 * The queue can be accessed outside the module where it is defined using:
2091 *
2092 * @code extern struct k_queue <name>; @endcode
2093 *
2094 * @param name Name of the queue.
2095 */
2096#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002097 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002098 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002099
Anas Nashif166f5192018-02-25 08:02:36 -06002100/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002101
Wentong Wu5611e922019-06-20 23:51:27 +08002102#ifdef CONFIG_USERSPACE
2103/**
2104 * @brief futex structure
2105 *
2106 * A k_futex is a lightweight mutual exclusion primitive designed
2107 * to minimize kernel involvement. Uncontended operation relies
2108 * only on atomic access to shared memory. k_futex are tracked as
2109 * kernel objects and can live in user memory so any access bypass
2110 * the kernel object permission management mechanism.
2111 */
2112struct k_futex {
2113 atomic_t val;
2114};
2115
2116/**
2117 * @brief futex kernel data structure
2118 *
2119 * z_futex_data are the helper data structure for k_futex to complete
2120 * futex contended operation on kernel side, structure z_futex_data
2121 * of every futex object is invisible in user mode.
2122 */
2123struct z_futex_data {
2124 _wait_q_t wait_q;
2125 struct k_spinlock lock;
2126};
2127
2128#define Z_FUTEX_DATA_INITIALIZER(obj) \
2129 { \
2130 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2131 }
2132
2133/**
2134 * @defgroup futex_apis FUTEX APIs
2135 * @ingroup kernel_apis
2136 * @{
2137 */
2138
2139/**
2140 * @brief Initialize a futex.
2141 *
2142 * This routine initializes a futex object, prior to its first use.
2143 *
2144 * @param futex Address of the k_futex.
2145 *
2146 * @return N/A
2147 */
2148__syscall void k_futex_init(struct k_futex *futex);
2149
2150/**
2151 * @brief Pend the current thread on a futex
2152 *
2153 * Tests that the supplied futex contains the expected value, and if so,
2154 * goes to sleep until some other thread calls k_futex_wake() on it.
2155 *
2156 * @param futex Address of the futex.
2157 * @param expected Expected value of the futex, if it is different the caller
2158 * will not wait on it.
2159 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2160 * special values K_NO_WAIT or K_FOREVER.
2161 * @retval -EACCES Caller does not have read access to futex address.
2162 * @retval -EAGAIN If the futex value did not match the expected parameter.
2163 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2164 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2165 * @retval 0 if the caller went to sleep and was woken up. The caller
2166 * should check the futex's value on wakeup to determine if it needs
2167 * to block again.
2168 */
2169__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2170
2171/**
2172 * @brief Wake one/all threads pending on a futex
2173 *
2174 * Wake up the highest priority thread pending on the supplied futex, or
2175 * wakeup all the threads pending on the supplied futex, and the behavior
2176 * depends on wake_all.
2177 *
2178 * @param futex Futex to wake up pending threads.
2179 * @param wake_all If true, wake up all pending threads; If false,
2180 * wakeup the highest priority thread.
2181 * @retval -EACCES Caller does not have access to the futex address.
2182 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2183 * @retval Number of threads that were woken up.
2184 */
2185__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2186
2187/** @} */
2188#endif
2189
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002191 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192};
2193
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002194/**
2195 * @cond INTERNAL_HIDDEN
2196 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002197#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002198 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002199 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002200 }
2201
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002202#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002203
Allan Stephensc98da842016-11-11 15:45:03 -05002204/**
2205 * INTERNAL_HIDDEN @endcond
2206 */
2207
2208/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002209 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002210 * @ingroup kernel_apis
2211 * @{
2212 */
2213
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
2221 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002222 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002224#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002225 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226
2227/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002228 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002229 *
2230 * This routine causes first thread pending on @a fifo, if any, to
2231 * return from k_fifo_get() call with NULL value (as if timeout
2232 * expired).
2233 *
2234 * @note Can be called by ISRs.
2235 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002236 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002237 *
2238 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002239 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002240 */
2241#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002242 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002243
2244/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002245 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002247 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002248 * aligned on a word boundary, and the first word of the item is reserved
2249 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002251 * @note Can be called by ISRs.
2252 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002253 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002254 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 *
2256 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002257 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002259#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002260 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002261
2262/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002263 * @brief Add an element to a FIFO queue.
2264 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002265 * This routine adds a data item to @a fifo. There is an implicit memory
2266 * allocation to create an additional temporary bookkeeping data structure from
2267 * the calling thread's resource pool, which is automatically freed when the
2268 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002269 *
2270 * @note Can be called by ISRs.
2271 *
2272 * @param fifo Address of the FIFO.
2273 * @param data Address of the data item.
2274 *
2275 * @retval 0 on success
2276 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002277 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002278 */
2279#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002280 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002281
2282/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002283 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002285 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002286 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * each data item pointing to the next data item; the list must be
2288 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002292 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @param head Pointer to first node in singly-linked list.
2294 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 *
2296 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002297 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002298 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002299#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002300 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301
2302/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002303 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002305 * This routine adds a list of data items to @a fifo in one operation.
2306 * The data items must be in a singly-linked list implemented using a
2307 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 * and must be re-initialized via sys_slist_init().
2309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002310 * @note Can be called by ISRs.
2311 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002312 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
2315 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002316 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002318#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002319 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320
2321/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002322 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002324 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002325 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2328 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002329 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 * or one of the special values K_NO_WAIT and K_FOREVER.
2332 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002333 * @return Address of the data item if successful; NULL if returned
2334 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002335 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002337#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002338 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002341 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002342 *
2343 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002344 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002345 *
2346 * @note Can be called by ISRs.
2347 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002348 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002349 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002350 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002351 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002352 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002353 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002354#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002355 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002356
2357/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002358 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002359 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002360 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302361 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002362 * on each iteration of processing, a head container will be peeked,
2363 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002364 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002365 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002366 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002367 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002368 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002369 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002370 */
2371#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002372 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002373
2374/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002375 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002376 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002377 * Return element from the tail of FIFO queue (without removing it). A usecase
2378 * for this is if elements of the FIFO queue are themselves containers. Then
2379 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002380 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002381 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002382 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002383 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002384 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002385 */
2386#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002387 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002388
2389/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002390 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002391 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002392 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002394 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002395 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002396 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002397 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002400 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002401 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002402
Anas Nashif166f5192018-02-25 08:02:36 -06002403/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002404
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002406 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002407};
2408
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002409/**
2410 * @cond INTERNAL_HIDDEN
2411 */
2412
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002413#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002414 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002415 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002416 }
2417
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002418#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2419
Allan Stephensc98da842016-11-11 15:45:03 -05002420/**
2421 * INTERNAL_HIDDEN @endcond
2422 */
2423
2424/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002425 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002426 * @ingroup kernel_apis
2427 * @{
2428 */
2429
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002430/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002431 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002433 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002434 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002435 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002436 *
2437 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002438 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002439 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002440#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002441 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442
2443/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002444 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002445 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002446 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002447 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450 * @note Can be called by ISRs.
2451 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002452 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454 *
2455 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002456 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002458#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002459 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460
2461/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002462 * @brief Add an element to a LIFO queue.
2463 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002464 * This routine adds a data item to @a lifo. There is an implicit memory
2465 * allocation to create an additional temporary bookkeeping data structure from
2466 * the calling thread's resource pool, which is automatically freed when the
2467 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002468 *
2469 * @note Can be called by ISRs.
2470 *
2471 * @param lifo Address of the LIFO.
2472 * @param data Address of the data item.
2473 *
2474 * @retval 0 on success
2475 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002476 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002477 */
2478#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002479 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002480
2481/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002482 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002485 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2488 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002489 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002491 * or one of the special values K_NO_WAIT and K_FOREVER.
2492 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002493 * @return Address of the data item if successful; NULL if returned
2494 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002495 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002497#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002498 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002501 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002503 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002504 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002505 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002508 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002511 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002512 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513
Anas Nashif166f5192018-02-25 08:02:36 -06002514/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002515
2516/**
2517 * @cond INTERNAL_HIDDEN
2518 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302519#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002521typedef uintptr_t stack_data_t;
2522
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523struct k_stack {
2524 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002525 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002526 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002528 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002529 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530};
2531
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002532#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002533 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002534 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002535 .base = stack_buffer, \
2536 .next = stack_buffer, \
2537 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002538 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002539 }
2540
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002541#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2542
Allan Stephensc98da842016-11-11 15:45:03 -05002543/**
2544 * INTERNAL_HIDDEN @endcond
2545 */
2546
2547/**
2548 * @defgroup stack_apis Stack APIs
2549 * @ingroup kernel_apis
2550 * @{
2551 */
2552
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002553/**
2554 * @brief Initialize a stack.
2555 *
2556 * This routine initializes a stack object, prior to its first use.
2557 *
2558 * @param stack Address of the stack.
2559 * @param buffer Address of array used to hold stacked values.
2560 * @param num_entries Maximum number of values that can be stacked.
2561 *
2562 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002563 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002564 */
Andrew Boief3bee952018-05-02 17:44:39 -07002565void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002566 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002567
2568
2569/**
2570 * @brief Initialize a stack.
2571 *
2572 * This routine initializes a stack object, prior to its first use. Internal
2573 * buffers will be allocated from the calling thread's resource pool.
2574 * This memory will be released if k_stack_cleanup() is called, or
2575 * userspace is enabled and the stack object loses all references to it.
2576 *
2577 * @param stack Address of the stack.
2578 * @param num_entries Maximum number of values that can be stacked.
2579 *
2580 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002581 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002582 */
2583
Adithya Baglody28080d32018-10-15 11:48:51 +05302584__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2585 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002586
2587/**
2588 * @brief Release a stack's allocated buffer
2589 *
2590 * If a stack object was given a dynamically allocated buffer via
2591 * k_stack_alloc_init(), this will free it. This function does nothing
2592 * if the buffer wasn't dynamically allocated.
2593 *
2594 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002595 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002596 */
2597void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598
2599/**
2600 * @brief Push an element onto a stack.
2601 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002602 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002603 *
2604 * @note Can be called by ISRs.
2605 *
2606 * @param stack Address of the stack.
2607 * @param data Value to push onto the stack.
2608 *
2609 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002610 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002612__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002613
2614/**
2615 * @brief Pop an element from a stack.
2616 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002617 * This routine removes a stack_data_t value from @a stack in a "last in,
2618 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 *
2620 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2621 *
2622 * @param stack Address of the stack.
2623 * @param data Address of area to hold the value popped from the stack.
2624 * @param timeout Waiting period to obtain a value (in milliseconds),
2625 * or one of the special values K_NO_WAIT and K_FOREVER.
2626 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002627 * @retval 0 Element popped from stack.
2628 * @retval -EBUSY Returned without waiting.
2629 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002630 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002632__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002638 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002639 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 * @param name Name of the stack.
2642 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002643 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002645#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002646 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002647 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002648 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002649 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002650 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651
Anas Nashif166f5192018-02-25 08:02:36 -06002652/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002653
Allan Stephens6bba9b02016-11-16 14:56:54 -05002654struct k_work;
2655
Allan Stephensc98da842016-11-11 15:45:03 -05002656/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002657 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002658 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659 */
2660
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661/**
2662 * @typedef k_work_handler_t
2663 * @brief Work item handler function type.
2664 *
2665 * A work item's handler function is executed by a workqueue's thread
2666 * when the work item is processed by the workqueue.
2667 *
2668 * @param work Address of the work item.
2669 *
2670 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002671 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002672 */
2673typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674
2675/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002678
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002680 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002681 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682};
2683
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002685 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686};
2687
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002689 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690 k_work_handler_t handler;
2691 atomic_t flags[1];
2692};
2693
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694struct k_delayed_work {
2695 struct k_work work;
2696 struct _timeout timeout;
2697 struct k_work_q *work_q;
2698};
2699
2700extern struct k_work_q k_sys_work_q;
2701
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002703 * INTERNAL_HIDDEN @endcond
2704 */
2705
Patrik Flykt4344e272019-03-08 14:19:05 -07002706#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002707 { \
2708 ._reserved = NULL, \
2709 .handler = work_handler, \
2710 .flags = { 0 } \
2711 }
2712
Patrik Flykt4344e272019-03-08 14:19:05 -07002713#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002714
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715/**
2716 * @brief Initialize a statically-defined work item.
2717 *
2718 * This macro can be used to initialize a statically-defined workqueue work
2719 * item, prior to its first use. For example,
2720 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002721 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002722 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002723 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002724 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002725 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002727#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002728 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729
2730/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002731 * @brief Initialize a work item.
2732 *
2733 * This routine initializes a workqueue work item, prior to its first use.
2734 *
2735 * @param work Address of work item.
2736 * @param handler Function to invoke each time work item is processed.
2737 *
2738 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002739 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740 */
2741static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2742{
Patrik Flykt4344e272019-03-08 14:19:05 -07002743 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744}
2745
2746/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002747 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002748 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002749 * This routine submits work item @a work to be processed by workqueue
2750 * @a work_q. If the work item is already pending in the workqueue's queue
2751 * as a result of an earlier submission, this routine has no effect on the
2752 * work item. If the work item has already been processed, or is currently
2753 * being processed, its work is considered complete and the work item can be
2754 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002755 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002756 * @warning
2757 * A submitted work item must not be modified until it has been processed
2758 * by the workqueue.
2759 *
2760 * @note Can be called by ISRs.
2761 *
2762 * @param work_q Address of workqueue.
2763 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002764 *
2765 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002766 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 */
2768static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2769 struct k_work *work)
2770{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002771 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002772 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773 }
2774}
2775
2776/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002777 * @brief Submit a work item to a user mode workqueue
2778 *
David B. Kinder06d78352018-12-17 14:32:40 -08002779 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002780 * memory allocation is made from the caller's resource pool which is freed
2781 * once the worker thread consumes the k_work item. The workqueue
2782 * thread must have memory access to the k_work item being submitted. The caller
2783 * must have permission granted on the work_q parameter's queue object.
2784 *
2785 * Otherwise this works the same as k_work_submit_to_queue().
2786 *
2787 * @note Can be called by ISRs.
2788 *
2789 * @param work_q Address of workqueue.
2790 * @param work Address of work item.
2791 *
2792 * @retval -EBUSY if the work item was already in some workqueue
2793 * @retval -ENOMEM if no memory for thread resource pool allocation
2794 * @retval 0 Success
2795 * @req K-WORK-001
2796 */
2797static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2798 struct k_work *work)
2799{
2800 int ret = -EBUSY;
2801
2802 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2803 ret = k_queue_alloc_append(&work_q->queue, work);
2804
2805 /* Couldn't insert into the queue. Clear the pending bit
2806 * so the work item can be submitted again
2807 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002808 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002809 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2810 }
2811 }
2812
2813 return ret;
2814}
2815
2816/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002817 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002819 * This routine indicates if work item @a work is pending in a workqueue's
2820 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002821 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002822 * @note Can be called by ISRs.
2823 *
2824 * @param work Address of work item.
2825 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002826 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002827 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002828 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002829static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002830{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002831 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002832}
2833
2834/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002835 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002836 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002837 * This routine starts workqueue @a work_q. The workqueue spawns its work
2838 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002839 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002840 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002841 * @param stack Pointer to work queue thread's stack space, as defined by
2842 * K_THREAD_STACK_DEFINE()
2843 * @param stack_size Size of the work queue thread's stack (in bytes), which
2844 * should either be the same constant passed to
2845 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002846 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847 *
2848 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002849 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002850 */
Andrew Boie507852a2017-07-25 18:47:07 -07002851extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002852 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002853 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002856 * @brief Start a workqueue in user mode
2857 *
2858 * This works identically to k_work_q_start() except it is callable from user
2859 * mode, and the worker thread created will run in user mode.
2860 * The caller must have permissions granted on both the work_q parameter's
2861 * thread and queue objects, and the same restrictions on priority apply as
2862 * k_thread_create().
2863 *
2864 * @param work_q Address of workqueue.
2865 * @param stack Pointer to work queue thread's stack space, as defined by
2866 * K_THREAD_STACK_DEFINE()
2867 * @param stack_size Size of the work queue thread's stack (in bytes), which
2868 * should either be the same constant passed to
2869 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2870 * @param prio Priority of the work queue's thread.
2871 *
2872 * @return N/A
2873 * @req K-WORK-001
2874 */
2875extern void k_work_q_user_start(struct k_work_q *work_q,
2876 k_thread_stack_t *stack,
2877 size_t stack_size, int prio);
2878
2879/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002880 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002882 * This routine initializes a workqueue delayed work item, prior to
2883 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002885 * @param work Address of delayed work item.
2886 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
2888 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002889 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002890 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002891extern void k_delayed_work_init(struct k_delayed_work *work,
2892 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002893
2894/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002895 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002897 * This routine schedules work item @a work to be processed by workqueue
2898 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002899 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002900 * Only when the countdown completes is the work item actually submitted to
2901 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002903 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002904 * counting down cancels the existing submission and restarts the
2905 * countdown using the new delay. Note that this behavior is
2906 * inherently subject to race conditions with the pre-existing
2907 * timeouts and work queue, so care must be taken to synchronize such
2908 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002909 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002910 * @warning
2911 * A delayed work item must not be modified until it has been processed
2912 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002914 * @note Can be called by ISRs.
2915 *
2916 * @param work_q Address of workqueue.
2917 * @param work Address of delayed work item.
2918 * @param delay Delay before submitting the work item (in milliseconds).
2919 *
2920 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002921 * @retval -EINVAL Work item is being processed or has completed its work.
2922 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002923 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002924 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002925extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2926 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002927 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928
2929/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002930 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002932 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002933 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002934 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002936 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002938 * @note The result of calling this on a k_delayed_work item that has
2939 * not been submitted (i.e. before the return of the
2940 * k_delayed_work_submit_to_queue() call) is undefined.
2941 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002942 * @param work Address of delayed work item.
2943 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002944 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002945 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002946 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002948extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002950/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951 * @brief Submit a work item to the system workqueue.
2952 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002953 * This routine submits work item @a work to be processed by the system
2954 * workqueue. If the work item is already pending in the workqueue's queue
2955 * as a result of an earlier submission, this routine has no effect on the
2956 * work item. If the work item has already been processed, or is currently
2957 * being processed, its work is considered complete and the work item can be
2958 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002960 * @warning
2961 * Work items submitted to the system workqueue should avoid using handlers
2962 * that block or yield since this may prevent the system workqueue from
2963 * processing other work items in a timely manner.
2964 *
2965 * @note Can be called by ISRs.
2966 *
2967 * @param work Address of work item.
2968 *
2969 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002970 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002971 */
2972static inline void k_work_submit(struct k_work *work)
2973{
2974 k_work_submit_to_queue(&k_sys_work_q, work);
2975}
2976
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002977/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978 * @brief Submit a delayed work item to the system workqueue.
2979 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002980 * This routine schedules work item @a work to be processed by the system
2981 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002982 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002983 * Only when the countdown completes is the work item actually submitted to
2984 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002986 * Submitting a previously submitted delayed work item that is still
2987 * counting down cancels the existing submission and restarts the countdown
2988 * using the new delay. If the work item is currently pending on the
2989 * workqueue's queue because the countdown has completed it is too late to
2990 * resubmit the item, and resubmission fails without impacting the work item.
2991 * If the work item has already been processed, or is currently being processed,
2992 * its work is considered complete and the work item can be resubmitted.
2993 *
2994 * @warning
2995 * Work items submitted to the system workqueue should avoid using handlers
2996 * that block or yield since this may prevent the system workqueue from
2997 * processing other work items in a timely manner.
2998 *
2999 * @note Can be called by ISRs.
3000 *
3001 * @param work Address of delayed work item.
3002 * @param delay Delay before submitting the work item (in milliseconds).
3003 *
3004 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003005 * @retval -EINVAL Work item is being processed or has completed its work.
3006 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003007 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003008 */
3009static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003010 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003012 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003013}
3014
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003016 * @brief Get time remaining before a delayed work gets scheduled.
3017 *
3018 * This routine computes the (approximate) time remaining before a
3019 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003020 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003021 *
3022 * @param work Delayed work item.
3023 *
3024 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003025 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003026 */
Kumar Galacc334c72017-04-21 10:55:34 -05003027static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003028{
Andy Ross52e444b2018-09-28 09:06:37 -07003029 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003030}
3031
Anas Nashif166f5192018-02-25 08:02:36 -06003032/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003033/**
Anas Nashifce78d162018-05-24 12:43:11 -05003034 * @defgroup mutex_apis Mutex APIs
3035 * @ingroup kernel_apis
3036 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003037 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003038
Anas Nashifce78d162018-05-24 12:43:11 -05003039/**
3040 * Mutex Structure
3041 * @ingroup mutex_apis
3042 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003043struct k_mutex {
3044 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003045 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003046 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003047 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003050 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003051};
3052
Anas Nashifce78d162018-05-24 12:43:11 -05003053/**
3054 * @cond INTERNAL_HIDDEN
3055 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003056#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003057 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003058 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059 .owner = NULL, \
3060 .lock_count = 0, \
3061 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003062 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063 }
3064
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003065#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
3066
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003067/**
Allan Stephensc98da842016-11-11 15:45:03 -05003068 * INTERNAL_HIDDEN @endcond
3069 */
3070
3071/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003072 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003075 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003076 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003078 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003079 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003080 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003082 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003083 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003085/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003088 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * Upon completion, the mutex is available and does not have an owner.
3091 *
3092 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003093 *
3094 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003095 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003096 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003097__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003098
3099/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * This routine locks @a mutex. If the mutex is locked by another thread,
3103 * the calling thread waits until the mutex becomes available or until
3104 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003106 * A thread is permitted to lock a mutex it has already locked. The operation
3107 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003109 * @param mutex Address of the mutex.
3110 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003111 * or one of the special values K_NO_WAIT and K_FOREVER.
3112 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003113 * @retval 0 Mutex locked.
3114 * @retval -EBUSY Returned without waiting.
3115 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003116 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003117 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003118__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119
3120/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * This routine unlocks @a mutex. The mutex must already be locked by the
3124 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003125 *
3126 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003128 * thread.
3129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 *
3132 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003133 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003134 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003135__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136
Allan Stephensc98da842016-11-11 15:45:03 -05003137/**
Anas Nashif166f5192018-02-25 08:02:36 -06003138 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003139 */
3140
3141/**
3142 * @cond INTERNAL_HIDDEN
3143 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003144
3145struct k_sem {
3146 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303147 u32_t count;
3148 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003149 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003151 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152};
3153
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003154#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003155 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003156 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003157 .count = initial_count, \
3158 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003159 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003160 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003161 }
3162
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003163#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003164
Allan Stephensc98da842016-11-11 15:45:03 -05003165/**
3166 * INTERNAL_HIDDEN @endcond
3167 */
3168
3169/**
3170 * @defgroup semaphore_apis Semaphore APIs
3171 * @ingroup kernel_apis
3172 * @{
3173 */
3174
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003178 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003180 * @param sem Address of the semaphore.
3181 * @param initial_count Initial semaphore count.
3182 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003183 *
3184 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003185 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003186 */
Andrew Boie99280232017-09-29 14:17:47 -07003187__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3188 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003189
3190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003191 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003193 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3196 *
3197 * @param sem Address of the semaphore.
3198 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003199 * or one of the special values K_NO_WAIT and K_FOREVER.
3200 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003201 * @note When porting code from the nanokernel legacy API to the new API, be
3202 * careful with the return value of this function. The return value is the
3203 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3204 * non-zero means failure, while the nano_sem_take family returns 1 for success
3205 * and 0 for failure.
3206 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003207 * @retval 0 Semaphore taken.
3208 * @retval -EBUSY Returned without waiting.
3209 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003210 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003211 */
Andrew Boie99280232017-09-29 14:17:47 -07003212__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003213
3214/**
3215 * @brief Give a semaphore.
3216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003217 * This routine gives @a sem, unless the semaphore is already at its maximum
3218 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003222 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003223 *
3224 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003225 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003226 */
Andrew Boie99280232017-09-29 14:17:47 -07003227__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003229/**
3230 * @brief Reset a semaphore's count to zero.
3231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003234 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003235 *
3236 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003237 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003238 */
Andrew Boie990bf162017-10-03 12:36:49 -07003239__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003240
Anas Nashif954d5502018-02-25 08:37:28 -06003241/**
3242 * @internal
3243 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003244static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245{
Patrik Flykt24d71432019-03-26 19:57:45 -06003246 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247}
3248
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003249/**
3250 * @brief Get a semaphore's count.
3251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003257 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003258 */
Andrew Boie990bf162017-10-03 12:36:49 -07003259__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003260
Anas Nashif954d5502018-02-25 08:37:28 -06003261/**
3262 * @internal
3263 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003264static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265{
3266 return sem->count;
3267}
3268
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003269/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003273 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003274 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * @param name Name of the semaphore.
3277 * @param initial_count Initial semaphore count.
3278 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003279 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003280 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003282 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003283 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303284 BUILD_ASSERT(((count_limit) != 0) && \
3285 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286
Anas Nashif166f5192018-02-25 08:02:36 -06003287/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003288
3289/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003290 * @defgroup msgq_apis Message Queue APIs
3291 * @ingroup kernel_apis
3292 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003293 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003295/**
3296 * @brief Message Queue Structure
3297 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298struct k_msgq {
3299 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003300 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003301 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003302 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303 char *buffer_start;
3304 char *buffer_end;
3305 char *read_ptr;
3306 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003307 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003309 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003310 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003312/**
3313 * @cond INTERNAL_HIDDEN
3314 */
3315
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003316
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003317#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003319 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003320 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003321 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003323 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324 .read_ptr = q_buffer, \
3325 .write_ptr = q_buffer, \
3326 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003327 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003329#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003330/**
3331 * INTERNAL_HIDDEN @endcond
3332 */
3333
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003334
Andrew Boie0fe789f2018-04-12 18:35:56 -07003335#define K_MSGQ_FLAG_ALLOC BIT(0)
3336
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003337/**
3338 * @brief Message Queue Attributes
3339 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303340struct k_msgq_attrs {
3341 size_t msg_size;
3342 u32_t max_msgs;
3343 u32_t used_msgs;
3344};
3345
Allan Stephensc98da842016-11-11 15:45:03 -05003346
3347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3351 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003352 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3353 * message is similarly aligned to this boundary, @a q_msg_size must also be
3354 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * The message queue can be accessed outside the module where it is defined
3357 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003358 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003359 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * @param q_name Name of the message queue.
3362 * @param q_msg_size Message size (in bytes).
3363 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003364 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003365 *
3366 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003367 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003368#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3369 static char __noinit __aligned(q_align) \
3370 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3371 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3372 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003373 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374
Peter Mitsisd7a37502016-10-13 11:37:40 -04003375/**
3376 * @brief Initialize a message queue.
3377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * This routine initializes a message queue object, prior to its first use.
3379 *
Allan Stephensda827222016-11-09 14:23:58 -06003380 * The message queue's ring buffer must contain space for @a max_msgs messages,
3381 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3382 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3383 * that each message is similarly aligned to this boundary, @a q_msg_size
3384 * must also be a multiple of N.
3385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * @param q Address of the message queue.
3387 * @param buffer Pointer to ring buffer that holds queued messages.
3388 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003389 * @param max_msgs Maximum number of messages that can be queued.
3390 *
3391 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003392 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003393 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003394void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3395 u32_t max_msgs);
3396
3397/**
3398 * @brief Initialize a message queue.
3399 *
3400 * This routine initializes a message queue object, prior to its first use,
3401 * allocating its internal ring buffer from the calling thread's resource
3402 * pool.
3403 *
3404 * Memory allocated for the ring buffer can be released by calling
3405 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3406 * all of its references.
3407 *
3408 * @param q Address of the message queue.
3409 * @param msg_size Message size (in bytes).
3410 * @param max_msgs Maximum number of messages that can be queued.
3411 *
3412 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3413 * thread's resource pool, or -EINVAL if the size parameters cause
3414 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003415 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003416 */
3417__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3418 u32_t max_msgs);
3419
3420
3421void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003422
3423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003424 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003427 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003428 * @note Can be called by ISRs.
3429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @param q Address of the message queue.
3431 * @param data Pointer to the message.
3432 * @param timeout Waiting period to add the message (in milliseconds),
3433 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003434 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003435 * @retval 0 Message sent.
3436 * @retval -ENOMSG Returned without waiting or queue purged.
3437 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003438 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003439 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003440__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003441
3442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003443 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003445 * This routine receives a message from message queue @a q in a "first in,
3446 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003447 *
Allan Stephensc98da842016-11-11 15:45:03 -05003448 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * @param q Address of the message queue.
3451 * @param data Address of area to hold the received message.
3452 * @param timeout Waiting period to receive the message (in milliseconds),
3453 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003454 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003455 * @retval 0 Message received.
3456 * @retval -ENOMSG Returned without waiting.
3457 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003458 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003459 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003460__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003461
3462/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003463 * @brief Peek/read a message from a message queue.
3464 *
3465 * This routine reads a message from message queue @a q in a "first in,
3466 * first out" manner and leaves the message in the queue.
3467 *
3468 * @note Can be called by ISRs.
3469 *
3470 * @param q Address of the message queue.
3471 * @param data Address of area to hold the message read from the queue.
3472 *
3473 * @retval 0 Message read.
3474 * @retval -ENOMSG Returned when the queue has no message.
3475 * @req K-MSGQ-002
3476 */
3477__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3478
3479/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * This routine discards all unreceived messages in a message queue's ring
3483 * buffer. Any threads that are blocked waiting to send a message to the
3484 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003487 *
3488 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003489 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003490 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003491__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003492
Peter Mitsis67be2492016-10-07 11:44:34 -04003493/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003494 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * This routine returns the number of unused entries in a message queue's
3497 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * @param q Address of the message queue.
3500 *
3501 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003502 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003503 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003504__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3505
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303506/**
3507 * @brief Get basic attributes of a message queue.
3508 *
3509 * This routine fetches basic attributes of message queue into attr argument.
3510 *
3511 * @param q Address of the message queue.
3512 * @param attrs pointer to message queue attribute structure.
3513 *
3514 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003515 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303516 */
3517__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3518
3519
Patrik Flykt4344e272019-03-08 14:19:05 -07003520static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003521{
3522 return q->max_msgs - q->used_msgs;
3523}
3524
Peter Mitsisd7a37502016-10-13 11:37:40 -04003525/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003526 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * @param q Address of the message queue.
3531 *
3532 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003533 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003534 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003535__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3536
Patrik Flykt4344e272019-03-08 14:19:05 -07003537static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538{
3539 return q->used_msgs;
3540}
3541
Anas Nashif166f5192018-02-25 08:02:36 -06003542/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003543
3544/**
3545 * @defgroup mem_pool_apis Memory Pool APIs
3546 * @ingroup kernel_apis
3547 * @{
3548 */
3549
Andy Ross73cb9582017-05-09 10:42:39 -07003550/* Note on sizing: the use of a 20 bit field for block means that,
3551 * assuming a reasonable minimum block size of 16 bytes, we're limited
3552 * to 16M of memory managed by a single pool. Long term it would be
3553 * good to move to a variable bit size based on configuration.
3554 */
3555struct k_mem_block_id {
3556 u32_t pool : 8;
3557 u32_t level : 4;
3558 u32_t block : 20;
3559};
3560
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003562 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003563 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564};
3565
Anas Nashif166f5192018-02-25 08:02:36 -06003566/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003567
3568/**
3569 * @defgroup mailbox_apis Mailbox APIs
3570 * @ingroup kernel_apis
3571 * @{
3572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003573
3574struct k_mbox_msg {
3575 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003576 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003577 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003578 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003580 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003581 /** sender's message data buffer */
3582 void *tx_data;
3583 /** internal use only - needed for legacy API support */
3584 void *_rx_data;
3585 /** message data block descriptor */
3586 struct k_mem_block tx_block;
3587 /** source thread id */
3588 k_tid_t rx_source_thread;
3589 /** target thread id */
3590 k_tid_t tx_target_thread;
3591 /** internal use only - thread waiting on send (may be a dummy) */
3592 k_tid_t _syncing_thread;
3593#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3594 /** internal use only - semaphore used during asynchronous send */
3595 struct k_sem *_async_sem;
3596#endif
3597};
3598
3599struct k_mbox {
3600 _wait_q_t tx_msg_queue;
3601 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003602 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003603
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003604 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003605};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003606/**
3607 * @cond INTERNAL_HIDDEN
3608 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003609
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003610#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003611 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003612 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3613 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003614 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615 }
3616
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003617#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3618
Peter Mitsis12092702016-10-14 12:57:23 -04003619/**
Allan Stephensc98da842016-11-11 15:45:03 -05003620 * INTERNAL_HIDDEN @endcond
3621 */
3622
3623/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003627 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003628 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003630 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003631 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003632 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003633#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003634 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003635 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003636
Peter Mitsis12092702016-10-14 12:57:23 -04003637/**
3638 * @brief Initialize a mailbox.
3639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * This routine initializes a mailbox object, prior to its first use.
3641 *
3642 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003643 *
3644 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003645 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003646 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003647extern void k_mbox_init(struct k_mbox *mbox);
3648
Peter Mitsis12092702016-10-14 12:57:23 -04003649/**
3650 * @brief Send a mailbox message in a synchronous manner.
3651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * This routine sends a message to @a mbox and waits for a receiver to both
3653 * receive and process it. The message data may be in a buffer, in a memory
3654 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @param mbox Address of the mailbox.
3657 * @param tx_msg Address of the transmit message descriptor.
3658 * @param timeout Waiting period for the message to be received (in
3659 * milliseconds), or one of the special values K_NO_WAIT
3660 * and K_FOREVER. Once the message has been received,
3661 * this routine waits as long as necessary for the message
3662 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003663 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003664 * @retval 0 Message sent.
3665 * @retval -ENOMSG Returned without waiting.
3666 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003667 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003668 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003669extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003670 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003671
Peter Mitsis12092702016-10-14 12:57:23 -04003672/**
3673 * @brief Send a mailbox message in an asynchronous manner.
3674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003675 * This routine sends a message to @a mbox without waiting for a receiver
3676 * to process it. The message data may be in a buffer, in a memory pool block,
3677 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3678 * will be given when the message has been both received and completely
3679 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003681 * @param mbox Address of the mailbox.
3682 * @param tx_msg Address of the transmit message descriptor.
3683 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003684 *
3685 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003686 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003687 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003688extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003689 struct k_sem *sem);
3690
Peter Mitsis12092702016-10-14 12:57:23 -04003691/**
3692 * @brief Receive a mailbox message.
3693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * This routine receives a message from @a mbox, then optionally retrieves
3695 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003697 * @param mbox Address of the mailbox.
3698 * @param rx_msg Address of the receive message descriptor.
3699 * @param buffer Address of the buffer to receive data, or NULL to defer data
3700 * retrieval and message disposal until later.
3701 * @param timeout Waiting period for a message to be received (in
3702 * milliseconds), or one of the special values K_NO_WAIT
3703 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003704 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003705 * @retval 0 Message received.
3706 * @retval -ENOMSG Returned without waiting.
3707 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003708 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003709 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003710extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003711 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003712
3713/**
3714 * @brief Retrieve mailbox message data into a buffer.
3715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * This routine completes the processing of a received message by retrieving
3717 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003718 *
3719 * Alternatively, this routine can be used to dispose of a received message
3720 * without retrieving its data.
3721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003722 * @param rx_msg Address of the receive message descriptor.
3723 * @param buffer Address of the buffer to receive data, or NULL to discard
3724 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003725 *
3726 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003727 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003728 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003729extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003730
3731/**
3732 * @brief Retrieve mailbox message data into a memory pool block.
3733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003734 * This routine completes the processing of a received message by retrieving
3735 * its data into a memory pool block, then disposing of the message.
3736 * The memory pool block that results from successful retrieval must be
3737 * returned to the pool once the data has been processed, even in cases
3738 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003739 *
3740 * Alternatively, this routine can be used to dispose of a received message
3741 * without retrieving its data. In this case there is no need to return a
3742 * memory pool block to the pool.
3743 *
3744 * This routine allocates a new memory pool block for the data only if the
3745 * data is not already in one. If a new block cannot be allocated, the routine
3746 * returns a failure code and the received message is left unchanged. This
3747 * permits the caller to reattempt data retrieval at a later time or to dispose
3748 * of the received message without retrieving its data.
3749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003750 * @param rx_msg Address of a receive message descriptor.
3751 * @param pool Address of memory pool, or NULL to discard data.
3752 * @param block Address of the area to hold memory pool block info.
3753 * @param timeout Waiting period to wait for a memory pool block (in
3754 * milliseconds), or one of the special values K_NO_WAIT
3755 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003756 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003757 * @retval 0 Data retrieved.
3758 * @retval -ENOMEM Returned without waiting.
3759 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003760 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003761 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003762extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003763 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003764 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003765
Anas Nashif166f5192018-02-25 08:02:36 -06003766/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003767
3768/**
Anas Nashifce78d162018-05-24 12:43:11 -05003769 * @defgroup pipe_apis Pipe APIs
3770 * @ingroup kernel_apis
3771 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003772 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773
Anas Nashifce78d162018-05-24 12:43:11 -05003774/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003775struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003776 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3777 size_t size; /**< Buffer size */
3778 size_t bytes_used; /**< # bytes used in buffer */
3779 size_t read_index; /**< Where in buffer to read from */
3780 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003781 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003782
3783 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003784 _wait_q_t readers; /**< Reader wait queue */
3785 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003786 } wait_q;
3787
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003788 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003789 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790};
3791
Anas Nashifce78d162018-05-24 12:43:11 -05003792/**
3793 * @cond INTERNAL_HIDDEN
3794 */
3795#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3796
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003797#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3798 { \
3799 .buffer = pipe_buffer, \
3800 .size = pipe_buffer_size, \
3801 .bytes_used = 0, \
3802 .read_index = 0, \
3803 .write_index = 0, \
3804 .lock = {}, \
3805 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003806 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3807 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003808 }, \
3809 _OBJECT_TRACING_INIT \
3810 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811 }
3812
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003813#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3814
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003815/**
Allan Stephensc98da842016-11-11 15:45:03 -05003816 * INTERNAL_HIDDEN @endcond
3817 */
3818
3819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003820 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003822 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003823 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003824 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003826 * @param name Name of the pipe.
3827 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3828 * or zero if no ring buffer is used.
3829 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003830 *
3831 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003832 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003833#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003834 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003835 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003836 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003837 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003840 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003842 * This routine initializes a pipe object, prior to its first use.
3843 *
3844 * @param pipe Address of the pipe.
3845 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3846 * is used.
3847 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3848 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003849 *
3850 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003851 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003853void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3854
3855/**
3856 * @brief Release a pipe's allocated buffer
3857 *
3858 * If a pipe object was given a dynamically allocated buffer via
3859 * k_pipe_alloc_init(), this will free it. This function does nothing
3860 * if the buffer wasn't dynamically allocated.
3861 *
3862 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003863 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003864 */
3865void k_pipe_cleanup(struct k_pipe *pipe);
3866
3867/**
3868 * @brief Initialize a pipe and allocate a buffer for it
3869 *
3870 * Storage for the buffer region will be allocated from the calling thread's
3871 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3872 * or userspace is enabled and the pipe object loses all references to it.
3873 *
3874 * This function should only be called on uninitialized pipe objects.
3875 *
3876 * @param pipe Address of the pipe.
3877 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3878 * buffer is used.
3879 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003880 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003881 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003882 */
3883__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003884
3885/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003886 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003888 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003890 * @param pipe Address of the pipe.
3891 * @param data Address of data to write.
3892 * @param bytes_to_write Size of data (in bytes).
3893 * @param bytes_written Address of area to hold the number of bytes written.
3894 * @param min_xfer Minimum number of bytes to write.
3895 * @param timeout Waiting period to wait for the data to be written (in
3896 * milliseconds), or one of the special values K_NO_WAIT
3897 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003899 * @retval 0 At least @a min_xfer bytes of data were written.
3900 * @retval -EIO Returned without waiting; zero data bytes were written.
3901 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003902 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003903 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003904 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003905__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3906 size_t bytes_to_write, size_t *bytes_written,
3907 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003908
3909/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003910 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003914 * @param pipe Address of the pipe.
3915 * @param data Address to place the data read from pipe.
3916 * @param bytes_to_read Maximum number of data bytes to read.
3917 * @param bytes_read Address of area to hold the number of bytes read.
3918 * @param min_xfer Minimum number of data bytes to read.
3919 * @param timeout Waiting period to wait for the data to be read (in
3920 * milliseconds), or one of the special values K_NO_WAIT
3921 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003922 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003923 * @retval 0 At least @a min_xfer bytes of data were read.
3924 * @retval -EIO Returned without waiting; zero data bytes were read.
3925 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003926 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003927 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003928 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003929__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3930 size_t bytes_to_read, size_t *bytes_read,
3931 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003932
3933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003934 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * This routine writes the data contained in a memory block to @a pipe.
3937 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003940 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003941 * @param block Memory block containing data to send
3942 * @param size Number of data bytes in memory block to send
3943 * @param sem Semaphore to signal upon completion (else NULL)
3944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003945 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003946 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003947 */
3948extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3949 size_t size, struct k_sem *sem);
3950
Anas Nashif166f5192018-02-25 08:02:36 -06003951/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003952
Allan Stephensc98da842016-11-11 15:45:03 -05003953/**
3954 * @cond INTERNAL_HIDDEN
3955 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003956
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003957struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003958 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003959 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003960 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003961 char *buffer;
3962 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003963 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003965 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966};
3967
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003968#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003969 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003970 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003971 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003972 .num_blocks = slab_num_blocks, \
3973 .block_size = slab_block_size, \
3974 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003975 .free_list = NULL, \
3976 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003977 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003978 }
3979
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003980#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3981
3982
Peter Mitsis578f9112016-10-07 13:50:31 -04003983/**
Allan Stephensc98da842016-11-11 15:45:03 -05003984 * INTERNAL_HIDDEN @endcond
3985 */
3986
3987/**
3988 * @defgroup mem_slab_apis Memory Slab APIs
3989 * @ingroup kernel_apis
3990 * @{
3991 */
3992
3993/**
Allan Stephensda827222016-11-09 14:23:58 -06003994 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003995 *
Allan Stephensda827222016-11-09 14:23:58 -06003996 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003997 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003998 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3999 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004000 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004001 *
Allan Stephensda827222016-11-09 14:23:58 -06004002 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004003 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004004 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004005 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004007 * @param name Name of the memory slab.
4008 * @param slab_block_size Size of each memory block (in bytes).
4009 * @param slab_num_blocks Number memory blocks.
4010 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004011 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004012 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004013#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004014 char __noinit __aligned(WB_UP(slab_align)) \
4015 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004016 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004017 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004018 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004019
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004020/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004021 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004024 *
Allan Stephensda827222016-11-09 14:23:58 -06004025 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4026 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004027 * N-byte boundary matching a word boundary, where N is a power of 2
4028 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004029 * To ensure that each memory block is similarly aligned to this boundary,
4030 * @a slab_block_size must also be a multiple of N.
4031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * @param slab Address of the memory slab.
4033 * @param buffer Pointer to buffer used for the memory blocks.
4034 * @param block_size Size of each memory block (in bytes).
4035 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004036 *
4037 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004038 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004039 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004040extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004041 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004042
4043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004044 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004046 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004048 * @param slab Address of the memory slab.
4049 * @param mem Pointer to block address area.
4050 * @param timeout Maximum time to wait for operation to complete
4051 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4052 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004053 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004054 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004055 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004056 * @retval -ENOMEM Returned without waiting.
4057 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004058 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004059 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004060extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004061 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004062
4063/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004064 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004066 * This routine releases a previously allocated memory block back to its
4067 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004069 * @param slab Address of the memory slab.
4070 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004071 *
4072 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004073 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004074 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004075extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004076
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004077/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004078 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004080 * This routine gets the number of memory blocks that are currently
4081 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004083 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004085 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004086 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004087 */
Kumar Galacc334c72017-04-21 10:55:34 -05004088static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004089{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004090 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004091}
4092
Peter Mitsisc001aa82016-10-13 13:53:37 -04004093/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004094 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004096 * This routine gets the number of memory blocks that are currently
4097 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004099 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004101 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004102 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004103 */
Kumar Galacc334c72017-04-21 10:55:34 -05004104static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004105{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004106 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004107}
4108
Anas Nashif166f5192018-02-25 08:02:36 -06004109/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004110
4111/**
4112 * @cond INTERNAL_HIDDEN
4113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004114
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004115struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004116 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004117 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004118};
4119
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004120/**
Allan Stephensc98da842016-11-11 15:45:03 -05004121 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004122 */
4123
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004124/**
Allan Stephensc98da842016-11-11 15:45:03 -05004125 * @addtogroup mem_pool_apis
4126 * @{
4127 */
4128
4129/**
4130 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004132 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4133 * long. The memory pool allows blocks to be repeatedly partitioned into
4134 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004135 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004136 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004137 * If the pool is to be accessed outside the module where it is defined, it
4138 * can be declared via
4139 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004140 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004142 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004143 * @param minsz Size of the smallest blocks in the pool (in bytes).
4144 * @param maxsz Size of the largest blocks in the pool (in bytes).
4145 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004146 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004147 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004148 */
Andy Ross73cb9582017-05-09 10:42:39 -07004149#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitre465b2cf2019-06-22 10:13:45 -04004150 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004151 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004152 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004153 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004154 .base = { \
4155 .buf = _mpool_buf_##name, \
Nicolas Pitre465b2cf2019-06-22 10:13:45 -04004156 .max_sz = _ALIGN4(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004157 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004158 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004159 .levels = _mpool_lvls_##name, \
4160 .flags = SYS_MEM_POOL_KERNEL \
4161 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004162 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004163
Peter Mitsis937042c2016-10-13 13:18:26 -04004164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004165 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004167 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004169 * @param pool Address of the memory pool.
4170 * @param block Pointer to block descriptor for the allocated memory.
4171 * @param size Amount of memory to allocate (in bytes).
4172 * @param timeout Maximum time to wait for operation to complete
4173 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4174 * or K_FOREVER to wait as long as necessary.
4175 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004176 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004177 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004178 * @retval -ENOMEM Returned without waiting.
4179 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004180 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004181 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004182extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004183 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004184
4185/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004186 * @brief Allocate memory from a memory pool with malloc() semantics
4187 *
4188 * Such memory must be released using k_free().
4189 *
4190 * @param pool Address of the memory pool.
4191 * @param size Amount of memory to allocate (in bytes).
4192 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004193 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004194 */
4195extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4196
4197/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004198 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004200 * This routine releases a previously allocated memory block back to its
4201 * memory pool.
4202 *
4203 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004204 *
4205 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004206 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004207 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004208extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004209
4210/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004211 * @brief Free memory allocated from a memory pool.
4212 *
4213 * This routine releases a previously allocated memory block back to its
4214 * memory pool
4215 *
4216 * @param id Memory block identifier.
4217 *
4218 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004219 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004220 */
4221extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4222
4223/**
Anas Nashif166f5192018-02-25 08:02:36 -06004224 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004225 */
4226
4227/**
4228 * @defgroup heap_apis Heap Memory Pool APIs
4229 * @ingroup kernel_apis
4230 * @{
4231 */
4232
4233/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004234 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004236 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004237 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004239 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004241 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004242 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004243 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004244extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004245
4246/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004247 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004248 *
4249 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004250 * returned must have been allocated from the heap memory pool or
4251 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004252 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004253 * If @a ptr is NULL, no operation is performed.
4254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004255 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004256 *
4257 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004258 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004259 */
4260extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004261
Allan Stephensc98da842016-11-11 15:45:03 -05004262/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004263 * @brief Allocate memory from heap, array style
4264 *
4265 * This routine provides traditional calloc() semantics. Memory is
4266 * allocated from the heap memory pool and zeroed.
4267 *
4268 * @param nmemb Number of elements in the requested array
4269 * @param size Size of each array element (in bytes).
4270 *
4271 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004272 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004273 */
4274extern void *k_calloc(size_t nmemb, size_t size);
4275
Anas Nashif166f5192018-02-25 08:02:36 -06004276/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004277
Benjamin Walshacc68c12017-01-29 18:57:45 -05004278/* polling API - PRIVATE */
4279
Benjamin Walshb0179862017-02-02 16:39:57 -05004280#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004281#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004282#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004283#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004284#endif
4285
Benjamin Walshacc68c12017-01-29 18:57:45 -05004286/* private - implementation data created as needed, per-type */
4287struct _poller {
4288 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004289 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004290};
4291
4292/* private - types bit positions */
4293enum _poll_types_bits {
4294 /* can be used to ignore an event */
4295 _POLL_TYPE_IGNORE,
4296
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004297 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004298 _POLL_TYPE_SIGNAL,
4299
4300 /* semaphore availability */
4301 _POLL_TYPE_SEM_AVAILABLE,
4302
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004303 /* queue/fifo/lifo data availability */
4304 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004305
4306 _POLL_NUM_TYPES
4307};
4308
Patrik Flykt4344e272019-03-08 14:19:05 -07004309#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004310
4311/* private - states bit positions */
4312enum _poll_states_bits {
4313 /* default state when creating event */
4314 _POLL_STATE_NOT_READY,
4315
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004316 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004317 _POLL_STATE_SIGNALED,
4318
4319 /* semaphore is available */
4320 _POLL_STATE_SEM_AVAILABLE,
4321
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004322 /* data is available to read on queue/fifo/lifo */
4323 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004324
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004325 /* queue/fifo/lifo wait was cancelled */
4326 _POLL_STATE_CANCELLED,
4327
Benjamin Walshacc68c12017-01-29 18:57:45 -05004328 _POLL_NUM_STATES
4329};
4330
Patrik Flykt4344e272019-03-08 14:19:05 -07004331#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004332
4333#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004334 (32 - (0 \
4335 + 8 /* tag */ \
4336 + _POLL_NUM_TYPES \
4337 + _POLL_NUM_STATES \
4338 + 1 /* modes */ \
4339 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004340
Benjamin Walshacc68c12017-01-29 18:57:45 -05004341/* end of polling API - PRIVATE */
4342
4343
4344/**
4345 * @defgroup poll_apis Async polling APIs
4346 * @ingroup kernel_apis
4347 * @{
4348 */
4349
4350/* Public polling API */
4351
4352/* public - values for k_poll_event.type bitfield */
4353#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004354#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4355#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4356#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004357#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004358
4359/* public - polling modes */
4360enum k_poll_modes {
4361 /* polling thread does not take ownership of objects when available */
4362 K_POLL_MODE_NOTIFY_ONLY = 0,
4363
4364 K_POLL_NUM_MODES
4365};
4366
4367/* public - values for k_poll_event.state bitfield */
4368#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004369#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4370#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4371#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004372#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004373#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004374
4375/* public - poll signal object */
4376struct k_poll_signal {
4377 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004378 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004379
4380 /*
4381 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4382 * user resets it to 0.
4383 */
4384 unsigned int signaled;
4385
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004386 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004387 int result;
4388};
4389
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004390#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004391 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004392 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004393 .signaled = 0, \
4394 .result = 0, \
4395 }
4396
4397struct k_poll_event {
4398 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004399 sys_dnode_t _node;
4400
4401 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004402 struct _poller *poller;
4403
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004404 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004405 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004406
Benjamin Walshacc68c12017-01-29 18:57:45 -05004407 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004408 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004409
4410 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004411 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004412
4413 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004414 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004415
4416 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004417 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004418
4419 /* per-type data */
4420 union {
4421 void *obj;
4422 struct k_poll_signal *signal;
4423 struct k_sem *sem;
4424 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004425 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004426 };
4427};
4428
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004429#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004430 { \
4431 .poller = NULL, \
4432 .type = event_type, \
4433 .state = K_POLL_STATE_NOT_READY, \
4434 .mode = event_mode, \
4435 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004436 { .obj = event_obj }, \
4437 }
4438
4439#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4440 event_tag) \
4441 { \
4442 .type = event_type, \
4443 .tag = event_tag, \
4444 .state = K_POLL_STATE_NOT_READY, \
4445 .mode = event_mode, \
4446 .unused = 0, \
4447 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004448 }
4449
4450/**
4451 * @brief Initialize one struct k_poll_event instance
4452 *
4453 * After this routine is called on a poll event, the event it ready to be
4454 * placed in an event array to be passed to k_poll().
4455 *
4456 * @param event The event to initialize.
4457 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4458 * values. Only values that apply to the same object being polled
4459 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4460 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004461 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004462 * @param obj Kernel object or poll signal.
4463 *
4464 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004465 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004466 */
4467
Kumar Galacc334c72017-04-21 10:55:34 -05004468extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004469 int mode, void *obj);
4470
4471/**
4472 * @brief Wait for one or many of multiple poll events to occur
4473 *
4474 * This routine allows a thread to wait concurrently for one or many of
4475 * multiple poll events to have occurred. Such events can be a kernel object
4476 * being available, like a semaphore, or a poll signal event.
4477 *
4478 * When an event notifies that a kernel object is available, the kernel object
4479 * is not "given" to the thread calling k_poll(): it merely signals the fact
4480 * that the object was available when the k_poll() call was in effect. Also,
4481 * all threads trying to acquire an object the regular way, i.e. by pending on
4482 * the object, have precedence over the thread polling on the object. This
4483 * means that the polling thread will never get the poll event on an object
4484 * until the object becomes available and its pend queue is empty. For this
4485 * reason, the k_poll() call is more effective when the objects being polled
4486 * only have one thread, the polling thread, trying to acquire them.
4487 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004488 * When k_poll() returns 0, the caller should loop on all the events that were
4489 * passed to k_poll() and check the state field for the values that were
4490 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004491 *
4492 * Before being reused for another call to k_poll(), the user has to reset the
4493 * state field to K_POLL_STATE_NOT_READY.
4494 *
Andrew Boie3772f772018-05-07 16:52:57 -07004495 * When called from user mode, a temporary memory allocation is required from
4496 * the caller's resource pool.
4497 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004498 * @param events An array of pointers to events to be polled for.
4499 * @param num_events The number of events in the array.
4500 * @param timeout Waiting period for an event to be ready (in milliseconds),
4501 * or one of the special values K_NO_WAIT and K_FOREVER.
4502 *
4503 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004504 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004505 * @retval -EINTR Polling has been interrupted, e.g. with
4506 * k_queue_cancel_wait(). All output events are still set and valid,
4507 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4508 * words, -EINTR status means that at least one of output events is
4509 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004510 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4511 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004512 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004513 */
4514
Andrew Boie3772f772018-05-07 16:52:57 -07004515__syscall int k_poll(struct k_poll_event *events, int num_events,
4516 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004517
4518/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004519 * @brief Initialize a poll signal object.
4520 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004521 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004522 *
4523 * @param signal A poll signal.
4524 *
4525 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004526 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004527 */
4528
Andrew Boie3772f772018-05-07 16:52:57 -07004529__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4530
4531/*
4532 * @brief Reset a poll signal object's state to unsignaled.
4533 *
4534 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004535 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004536 */
4537__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4538
Patrik Flykt4344e272019-03-08 14:19:05 -07004539static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004540{
Patrik Flykt24d71432019-03-26 19:57:45 -06004541 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004542}
4543
4544/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004545 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004546 *
4547 * @param signal A poll signal object
4548 * @param signaled An integer buffer which will be written nonzero if the
4549 * object was signaled
4550 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004551 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004552 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004553 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004554 */
4555__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4556 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004557
4558/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004559 * @brief Signal a poll signal object.
4560 *
4561 * This routine makes ready a poll signal, which is basically a poll event of
4562 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4563 * made ready to run. A @a result value can be specified.
4564 *
4565 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004566 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004567 * k_poll_signal_reset(). It thus has to be reset by the user before being
4568 * passed again to k_poll() or k_poll() will consider it being signaled, and
4569 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004570 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004571 * @note The result is stored and the 'signaled' field is set even if
4572 * this function returns an error indicating that an expiring poll was
4573 * not notified. The next k_poll() will detect the missed raise.
4574 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004575 * @param signal A poll signal.
4576 * @param result The value to store in the result field of the signal.
4577 *
4578 * @retval 0 The signal was delivered successfully.
4579 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004580 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004581 */
4582
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004583__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004584
Anas Nashif954d5502018-02-25 08:37:28 -06004585/**
4586 * @internal
4587 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004588extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004589
Anas Nashif166f5192018-02-25 08:02:36 -06004590/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004591
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004592/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004593 * @defgroup cpu_idle_apis CPU Idling APIs
4594 * @ingroup kernel_apis
4595 * @{
4596 */
4597
4598/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004599 * @brief Make the CPU idle.
4600 *
4601 * This function makes the CPU idle until an event wakes it up.
4602 *
4603 * In a regular system, the idle thread should be the only thread responsible
4604 * for making the CPU idle and triggering any type of power management.
4605 * However, in some more constrained systems, such as a single-threaded system,
4606 * the only thread would be responsible for this if needed.
4607 *
4608 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004609 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004610 */
4611extern void k_cpu_idle(void);
4612
4613/**
4614 * @brief Make the CPU idle in an atomic fashion.
4615 *
4616 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4617 * must be done atomically before making the CPU idle.
4618 *
4619 * @param key Interrupt locking key obtained from irq_lock().
4620 *
4621 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004622 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004623 */
4624extern void k_cpu_atomic_idle(unsigned int key);
4625
Anas Nashif30c3cff2019-01-22 08:18:13 -05004626/**
4627 * @}
4628 */
Anas Nashif954d5502018-02-25 08:37:28 -06004629
4630/**
4631 * @internal
4632 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004633extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004634
Patrik Flykt4344e272019-03-08 14:19:05 -07004635#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004636/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004637#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004638#else
4639
Andrew Boiecdb94d62017-04-18 15:22:05 -07004640/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004641 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004642 *
4643 * We won't have a real exception frame to determine the PC value when
4644 * the oops occurred, so print file and line number before we jump into
4645 * the fatal error handler.
4646 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004647#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004648 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Patrik Flykt4344e272019-03-08 14:19:05 -07004649 z_NanoFatalErrorHandler(reason, &_default_esf); \
Andy Ross92ce7672019-05-31 13:12:15 -07004650 k_thread_abort(k_current_get()); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004651 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004652
4653#endif /* _ARCH__EXCEPT */
4654
4655/**
4656 * @brief Fatally terminate a thread
4657 *
4658 * This should be called when a thread has encountered an unrecoverable
4659 * runtime condition and needs to terminate. What this ultimately
4660 * means is determined by the _fatal_error_handler() implementation, which
4661 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4662 *
4663 * If this is called from ISR context, the default system fatal error handler
4664 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004665 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004666 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004667#define k_oops() z_except_reason(_NANO_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004668
4669/**
4670 * @brief Fatally terminate the system
4671 *
4672 * This should be called when the Zephyr kernel has encountered an
4673 * unrecoverable runtime condition and needs to terminate. What this ultimately
4674 * means is determined by the _fatal_error_handler() implementation, which
4675 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004676 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004677 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004678#define k_panic() z_except_reason(_NANO_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004679
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004680/*
4681 * private APIs that are utilized by one or more public APIs
4682 */
4683
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004684#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004685/**
4686 * @internal
4687 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004688extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004689#else
Anas Nashif954d5502018-02-25 08:37:28 -06004690/**
4691 * @internal
4692 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004693#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004694#endif
4695
Anas Nashif954d5502018-02-25 08:37:28 -06004696/**
4697 * @internal
4698 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004699extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004700/**
4701 * @internal
4702 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004703extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004704
Andrew Boiedc5d9352017-06-02 12:56:47 -07004705/* arch/cpu.h may declare an architecture or platform-specific macro
4706 * for properly declaring stacks, compatible with MMU/MPU constraints if
4707 * enabled
4708 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004709
4710/**
4711 * @brief Obtain an extern reference to a stack
4712 *
4713 * This macro properly brings the symbol of a thread stack declared
4714 * elsewhere into scope.
4715 *
4716 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004717 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004718 */
4719#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4720
Patrik Flykt4344e272019-03-08 14:19:05 -07004721#ifdef Z_ARCH_THREAD_STACK_DEFINE
4722#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004723#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004724 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4725#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4726#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4727#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004728#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004729static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004730{
Patrik Flykt4344e272019-03-08 14:19:05 -07004731 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004732}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004733#else
4734/**
4735 * @brief Declare a toplevel thread stack memory region
4736 *
4737 * This declares a region of memory suitable for use as a thread's stack.
4738 *
4739 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4740 * 'noinit' section so that it isn't zeroed at boot
4741 *
Andrew Boie507852a2017-07-25 18:47:07 -07004742 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004743 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004744 * inside needs to be examined, examine thread->stack_info for the associated
4745 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004746 *
4747 * It is legal to precede this definition with the 'static' keyword.
4748 *
4749 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4750 * parameter of k_thread_create(), it may not be the same as the
4751 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4752 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004753 * Some arches may round the size of the usable stack region up to satisfy
4754 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4755 * size.
4756 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004757 * @param sym Thread stack symbol name
4758 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004759 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004760 */
4761#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004762 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004763
4764/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304765 * @brief Calculate size of stacks to be allocated in a stack array
4766 *
4767 * This macro calculates the size to be allocated for the stacks
4768 * inside a stack array. It accepts the indicated "size" as a parameter
4769 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4770 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4771 *
4772 * @param size Size of the stack memory region
4773 * @req K-TSTACK-001
4774 */
4775#define K_THREAD_STACK_LEN(size) (size)
4776
4777/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004778 * @brief Declare a toplevel array of thread stack memory regions
4779 *
4780 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4781 * definition for additional details and constraints.
4782 *
4783 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4784 * 'noinit' section so that it isn't zeroed at boot
4785 *
4786 * @param sym Thread stack symbol name
4787 * @param nmemb Number of stacks to declare
4788 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004789 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004790 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004791#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004792 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304793 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004794
4795/**
4796 * @brief Declare an embedded stack memory region
4797 *
4798 * Used for stacks embedded within other data structures. Use is highly
4799 * discouraged but in some cases necessary. For memory protection scenarios,
4800 * it is very important that any RAM preceding this member not be writable
4801 * by threads else a stack overflow will lead to silent corruption. In other
4802 * words, the containing data structure should live in RAM owned by the kernel.
4803 *
4804 * @param sym Thread stack symbol name
4805 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004806 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004807 */
4808#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004809 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004810
4811/**
4812 * @brief Return the size in bytes of a stack memory region
4813 *
4814 * Convenience macro for passing the desired stack size to k_thread_create()
4815 * since the underlying implementation may actually create something larger
4816 * (for instance a guard area).
4817 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004818 * The value returned here is not guaranteed to match the 'size' parameter
4819 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004820 *
4821 * @param sym Stack memory symbol
4822 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004823 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004824 */
4825#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4826
Andrew Boie575abc02019-03-19 10:42:24 -07004827
4828/**
4829 * @brief Indicate how much additional memory is reserved for stack objects
4830 *
4831 * Any given stack declaration may have additional memory in it for guard
4832 * areas or supervisor mode stacks. This macro indicates how much space
4833 * is reserved for this. The memory reserved may not be contiguous within
4834 * the stack object, and does not account for additional space used due to
4835 * enforce alignment.
4836 */
4837#define K_THREAD_STACK_RESERVED 0
4838
Andrew Boiedc5d9352017-06-02 12:56:47 -07004839/**
4840 * @brief Get a pointer to the physical stack buffer
4841 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004842 * This macro is deprecated. If a stack buffer needs to be examined, the
4843 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004844 *
4845 * @param sym Declared stack symbol name
4846 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004847 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004848 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004849static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004850{
4851 return (char *)sym;
4852}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004853
4854#endif /* _ARCH_DECLARE_STACK */
4855
Chunlin Hane9c97022017-07-07 20:29:30 +08004856/**
4857 * @defgroup mem_domain_apis Memory domain APIs
4858 * @ingroup kernel_apis
4859 * @{
4860 */
4861
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004862/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004863 * @def K_MEM_PARTITION_DEFINE
4864 * @brief Used to declare a memory partition
4865 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004866 */
4867#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4868#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4869 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004870 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004871 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004872#else
4873#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004874 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004875 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004876#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4877
Chunlin Hane9c97022017-07-07 20:29:30 +08004878/* memory partition */
4879struct k_mem_partition {
4880 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004881 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004882 /* size of memory partition */
4883 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004884#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004885 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304886 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004887#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004888};
4889
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004890/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304891 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004892struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304893#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004894 /* partitions in the domain */
4895 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304896#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004897 /* domain q */
4898 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004899 /* number of partitions in the domain */
4900 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004901};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304902
Chunlin Hane9c97022017-07-07 20:29:30 +08004903
4904/**
4905 * @brief Initialize a memory domain.
4906 *
4907 * Initialize a memory domain with given name and memory partitions.
4908 *
4909 * @param domain The memory domain to be initialized.
4910 * @param num_parts The number of array items of "parts" parameter.
4911 * @param parts An array of pointers to the memory partitions. Can be NULL
4912 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004913 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004914 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004915extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004916 struct k_mem_partition *parts[]);
4917/**
4918 * @brief Destroy a memory domain.
4919 *
4920 * Destroy a memory domain.
4921 *
4922 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004923 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004924 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004925extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4926
4927/**
4928 * @brief Add a memory partition into a memory domain.
4929 *
4930 * Add a memory partition into a memory domain.
4931 *
4932 * @param domain The memory domain to be added a memory partition.
4933 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004934 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004935 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004936extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4937 struct k_mem_partition *part);
4938
4939/**
4940 * @brief Remove a memory partition from a memory domain.
4941 *
4942 * Remove a memory partition from a memory domain.
4943 *
4944 * @param domain The memory domain to be removed a memory partition.
4945 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004946 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004947 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004948extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4949 struct k_mem_partition *part);
4950
4951/**
4952 * @brief Add a thread into a memory domain.
4953 *
4954 * Add a thread into a memory domain.
4955 *
4956 * @param domain The memory domain that the thread is going to be added into.
4957 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004958 *
4959 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004960 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004961extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4962 k_tid_t thread);
4963
4964/**
4965 * @brief Remove a thread from its memory domain.
4966 *
4967 * Remove a thread from its memory domain.
4968 *
4969 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004970 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004971 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004972extern void k_mem_domain_remove_thread(k_tid_t thread);
4973
Anas Nashif166f5192018-02-25 08:02:36 -06004974/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004975
Andrew Boie756f9072017-10-10 16:01:49 -07004976/**
4977 * @brief Emit a character buffer to the console device
4978 *
4979 * @param c String of characters to print
4980 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004981 *
4982 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004983 */
4984__syscall void k_str_out(char *c, size_t n);
4985
Andy Rosse7172672018-01-24 15:48:32 -08004986/**
4987 * @brief Start a numbered CPU on a MP-capable system
4988
4989 * This starts and initializes a specific CPU. The main thread on
4990 * startup is running on CPU zero, other processors are numbered
4991 * sequentially. On return from this function, the CPU is known to
4992 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004993 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004994 * with the provided key will work to enable them.
4995 *
4996 * Normally, in SMP mode this function will be called by the kernel
4997 * initialization and should not be used as a user API. But it is
4998 * defined here for special-purpose apps which want Zephyr running on
4999 * one core and to use others for design-specific processing.
5000 *
5001 * @param cpu_num Integer number of the CPU
5002 * @param stack Stack memory for the CPU
5003 * @param sz Stack buffer size, in bytes
5004 * @param fn Function to begin running on the CPU. First argument is
5005 * an irq_unlock() key.
5006 * @param arg Untyped argument to be passed to "fn"
5007 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005008extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08005009 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08005010
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005011
5012/**
5013 * @brief Disable preservation of floating point context information.
5014 *
5015 * This routine informs the kernel that the specified thread
5016 * will no longer be using the floating point registers.
5017 *
5018 * @warning
5019 * Some architectures apply restrictions on how the disabling of floating
5020 * point preservation may be requested, see z_arch_float_disable.
5021 *
5022 * @warning
5023 * This routine should only be used to disable floating point support for
5024 * a thread that currently has such support enabled.
5025 *
5026 * @param thread ID of thread.
5027 *
5028 * @retval 0 On success.
5029 * @retval -ENOSYS If the floating point disabling is not implemented.
5030 * -EINVAL If the floating point disabling could not be performed.
5031 */
5032__syscall int k_float_disable(struct k_thread *thread);
5033
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005034#ifdef __cplusplus
5035}
5036#endif
5037
Anas Nashif10291a02019-06-25 12:25:12 -04005038#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005039#include <syscalls/kernel.h>
5040
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005041#endif /* !_ASMLANGUAGE */
5042
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005043#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */