blob: 5cc05767ae17576be1bbf3154e39966ffd7af559 [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 Boiec3d4e652019-06-28 14:19:16 -0700210/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700211#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}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700253/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700254#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700255
256/**
257 * grant a thread access to a kernel object
258 *
259 * The thread will be granted access to the object if the caller is from
260 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700261 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700262 *
263 * @param object Address of kernel object
264 * @param thread Thread to grant access to the object
265 */
Andrew Boie743e4682017-10-04 12:25:50 -0700266__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700267
Andrew Boiea89bf012017-10-09 14:47:55 -0700268/**
269 * grant a thread access to a kernel object
270 *
271 * The thread will lose access to the object if the caller is from
272 * supervisor mode, or the caller is from user mode AND has permissions
273 * on both the object and the thread whose access is being revoked.
274 *
275 * @param object Address of kernel object
276 * @param thread Thread to remove access to the object
277 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700278void k_object_access_revoke(void *object, struct k_thread *thread);
279
280
281__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700282
283/**
284 * grant all present and future threads access to an object
285 *
286 * If the caller is from supervisor mode, or the caller is from user mode and
287 * have sufficient permissions on the object, then that object will have
288 * permissions granted to it for *all* current and future threads running in
289 * the system, effectively becoming a public kernel object.
290 *
291 * Use of this API should be avoided on systems that are running untrusted code
292 * as it is possible for such code to derive the addresses of kernel objects
293 * and perform unwanted operations on them.
294 *
Andrew Boie04caa672017-10-13 13:57:07 -0700295 * It is not possible to revoke permissions on public objects; once public,
296 * any thread may use it.
297 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700298 * @param object Address of kernel object
299 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700300void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700301
Andrew Boie31bdfc02017-11-08 16:38:03 -0800302/**
303 * Allocate a kernel object of a designated type
304 *
305 * This will instantiate at runtime a kernel object of the specified type,
306 * returning a pointer to it. The object will be returned in an uninitialized
307 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700308 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800309 *
310 * Currently, allocation of thread stacks is not supported.
311 *
312 * @param otype Requested kernel object type
313 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
314 * available
315 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700316__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800317
Andrew Boie97bf0012018-04-24 17:01:37 -0700318#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800319/**
320 * Free a kernel object previously allocated with k_object_alloc()
321 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700322 * This will return memory for a kernel object back to resource pool it was
323 * allocated from. Care must be exercised that the object will not be used
324 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800325 *
326 * @param obj Pointer to the kernel object memory address.
327 */
328void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700329#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700330/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700331static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700332{
Kumar Gala85699f72018-05-17 09:26:03 -0500333 ARG_UNUSED(otype);
334
Andrew Boie97bf0012018-04-24 17:01:37 -0700335 return NULL;
336}
337
338static inline void k_obj_free(void *obj)
339{
340 ARG_UNUSED(obj);
341}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700342/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800343#endif /* CONFIG_DYNAMIC_OBJECTS */
344
Anas Nashif4bcb2942019-01-23 23:06:29 -0500345/** @} */
346
Andrew Boiebca15da2017-10-15 14:17:48 -0700347/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700348 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700349 *
350 * The purpose of this data type is to clearly distinguish between the
351 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
352 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400353 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700354 * stack buffer region with guard areas that trigger a MPU or MMU fault
355 * if written to.
356 *
357 * APIs that want to work with the buffer inside should continue to use
358 * char *.
359 *
360 * Stacks should always be created with K_THREAD_STACK_DEFINE().
361 */
362struct __packed _k_thread_stack_element {
363 char data;
364};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700365typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700366
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700367/**
368 * @typedef k_thread_entry_t
369 * @brief Thread entry point function type.
370 *
371 * A thread's entry point function is invoked when the thread starts executing.
372 * Up to 3 argument values can be passed to the function.
373 *
374 * The thread terminates execution permanently if the entry point function
375 * returns. The thread is responsible for releasing any shared resources
376 * it may own (such as mutexes and dynamically allocated memory), prior to
377 * returning.
378 *
379 * @param p1 First argument.
380 * @param p2 Second argument.
381 * @param p3 Third argument.
382 *
383 * @return N/A
384 */
385typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700386
387#ifdef CONFIG_THREAD_MONITOR
388struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700389 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700390 void *parameter1;
391 void *parameter2;
392 void *parameter3;
393};
394#endif
395
396/* can be used for creating 'dummy' threads, e.g. for pending on objects */
397struct _thread_base {
398
399 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700400 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600401 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700402 struct rbnode qnode_rb;
403 };
404
Andy Ross1acd8c22018-05-03 14:51:49 -0700405 /* wait queue on which the thread is pended (needed only for
406 * trees, not dumb lists)
407 */
408 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700409
410 /* user facing 'thread options'; values defined in include/kernel.h */
411 u8_t user_options;
412
413 /* thread state */
414 u8_t thread_state;
415
416 /*
417 * scheduler lock count and thread priority
418 *
419 * These two fields control the preemptibility of a thread.
420 *
421 * When the scheduler is locked, sched_locked is decremented, which
422 * means that the scheduler is locked for values from 0xff to 0x01. A
423 * thread is coop if its prio is negative, thus 0x80 to 0xff when
424 * looked at the value as unsigned.
425 *
426 * By putting them end-to-end, this means that a thread is
427 * non-preemptible if the bundled value is greater than or equal to
428 * 0x0080.
429 */
430 union {
431 struct {
432#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
433 u8_t sched_locked;
434 s8_t prio;
435#else /* LITTLE and PDP */
436 s8_t prio;
437 u8_t sched_locked;
438#endif
439 };
440 u16_t preempt;
441 };
442
Andy Ross4a2e50f2018-05-15 11:06:25 -0700443#ifdef CONFIG_SCHED_DEADLINE
444 int prio_deadline;
445#endif
446
Andy Ross1acd8c22018-05-03 14:51:49 -0700447 u32_t order_key;
448
Andy Ross2724fd12018-01-29 14:55:20 -0800449#ifdef CONFIG_SMP
450 /* True for the per-CPU idle threads */
451 u8_t is_idle;
452
Andy Ross2724fd12018-01-29 14:55:20 -0800453 /* CPU index on which thread was last run */
454 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700455
456 /* Recursive count of irq_lock() calls */
457 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800458
459#endif
460
461#ifdef CONFIG_SCHED_CPU_MASK
462 /* "May run on" bits for each CPU */
463 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800464#endif
465
Andrew Boie73abd322017-04-04 13:19:13 -0700466 /* data returned by APIs */
467 void *swap_data;
468
469#ifdef CONFIG_SYS_CLOCK_EXISTS
470 /* this thread's entry in a timeout queue */
471 struct _timeout timeout;
472#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700473};
474
475typedef struct _thread_base _thread_base_t;
476
477#if defined(CONFIG_THREAD_STACK_INFO)
478/* Contains the stack information of a thread */
479struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700480 /* Stack start - Represents the start address of the thread-writable
481 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700482 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400483 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700484
485 /* Stack Size - Thread writable stack buffer size. Represents
486 * the size of the actual area, starting from the start member,
487 * that should be writable by the thread
488 */
Andrew Boie73abd322017-04-04 13:19:13 -0700489 u32_t size;
490};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700491
492typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700493#endif /* CONFIG_THREAD_STACK_INFO */
494
Chunlin Hane9c97022017-07-07 20:29:30 +0800495#if defined(CONFIG_USERSPACE)
496struct _mem_domain_info {
497 /* memory domain queue node */
498 sys_dnode_t mem_domain_q_node;
499 /* memory domain of the thread */
500 struct k_mem_domain *mem_domain;
501};
502
503#endif /* CONFIG_USERSPACE */
504
Daniel Leungfc182432018-08-16 15:42:28 -0700505#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
506struct _thread_userspace_local_data {
507 int errno_var;
508};
509#endif
510
Anas Nashifce78d162018-05-24 12:43:11 -0500511/**
512 * @ingroup thread_apis
513 * Thread Structure
514 */
Andrew Boie73abd322017-04-04 13:19:13 -0700515struct k_thread {
516
517 struct _thread_base base;
518
Anas Nashifce78d162018-05-24 12:43:11 -0500519 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700520 struct _callee_saved callee_saved;
521
Anas Nashifce78d162018-05-24 12:43:11 -0500522 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700523 void *init_data;
524
Anas Nashifce78d162018-05-24 12:43:11 -0500525 /**
526 * abort function
527 * @req K-THREAD-002
528 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700529 void (*fn_abort)(void);
530
531#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700533 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700534
Anas Nashifce78d162018-05-24 12:43:11 -0500535 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700536 struct k_thread *next_thread;
537#endif
538
Anas Nashif57554052018-03-03 02:31:05 -0600539#if defined(CONFIG_THREAD_NAME)
540 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700541 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600542#endif
543
Andrew Boie73abd322017-04-04 13:19:13 -0700544#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500545 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700546 void *custom_data;
547#endif
548
Daniel Leungfc182432018-08-16 15:42:28 -0700549#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
550 struct _thread_userspace_local_data *userspace_local_data;
551#endif
552
Andrew Boie73abd322017-04-04 13:19:13 -0700553#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700554#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700556 int errno_var;
557#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700558#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700559
560#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500561 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700562 struct _thread_stack_info stack_info;
563#endif /* CONFIG_THREAD_STACK_INFO */
564
Chunlin Hane9c97022017-07-07 20:29:30 +0800565#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500566 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800567 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500568 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700569 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800570#endif /* CONFIG_USERSPACE */
571
Andy Ross042d8ec2017-12-09 08:37:20 -0800572#if defined(CONFIG_USE_SWITCH)
573 /* When using __switch() a few previously arch-specific items
574 * become part of the core OS
575 */
576
Patrik Flykt4344e272019-03-08 14:19:05 -0700577 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800578 int swap_retval;
579
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600580 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800581 void *switch_handle;
582#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500583 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700584 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800585
Anas Nashifce78d162018-05-24 12:43:11 -0500586 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700587 struct _thread_arch arch;
588};
589
590typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400591typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593enum execution_context_types {
594 K_ISR = 0,
595 K_COOP_THREAD,
596 K_PREEMPT_THREAD,
597};
598
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500600 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100601 * @{
602 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530603typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
604 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100605
606/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530607 * @brief Iterate over all the threads in the system.
608 *
609 * This routine iterates over all the threads in the system and
610 * calls the user_cb function for each thread.
611 *
612 * @param user_cb Pointer to the user callback function.
613 * @param user_data Pointer to user data.
614 *
615 * @note CONFIG_THREAD_MONITOR must be set for this function
616 * to be effective. Also this API uses irq_lock to protect the
617 * _kernel.threads list which means creation of new threads and
618 * terminations of existing threads are blocked until this
619 * API returns.
620 *
621 * @return N/A
622 */
623extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
624
Anas Nashif166f5192018-02-25 08:02:36 -0600625/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100626
627/**
Allan Stephensc98da842016-11-11 15:45:03 -0500628 * @defgroup thread_apis Thread APIs
629 * @ingroup kernel_apis
630 * @{
631 */
632
Benjamin Walshed240f22017-01-22 13:05:08 -0500633#endif /* !_ASMLANGUAGE */
634
635
636/*
637 * Thread user options. May be needed by assembly code. Common part uses low
638 * bits, arch-specific use high bits.
639 */
640
Anas Nashifa541e932018-05-24 11:19:16 -0500641/**
642 * @brief system thread that must not abort
643 * @req K-THREAD-000
644 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700645#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500646
647#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500648/**
649 * @brief thread uses floating point registers
650 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700651#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500652#endif
653
Anas Nashifa541e932018-05-24 11:19:16 -0500654/**
655 * @brief user mode thread
656 *
657 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700658 * has additional restrictions
659 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700660#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700661
Anas Nashifa541e932018-05-24 11:19:16 -0500662/**
663 * @brief Inherit Permissions
664 *
665 * @details
666 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700667 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
668 * is not enabled.
669 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700670#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700671
Benjamin Walshed240f22017-01-22 13:05:08 -0500672#ifdef CONFIG_X86
673/* x86 Bitmask definitions for threads user options */
674
675#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
676/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700677#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500678#endif
679#endif
680
681/* end - thread options */
682
683#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700685 * @brief Create a thread.
686 *
687 * This routine initializes a thread, then schedules it for execution.
688 *
689 * The new thread may be scheduled for immediate execution or a delayed start.
690 * If the newly spawned thread does not have a delayed start the kernel
691 * scheduler may preempt the current thread to allow the new thread to
692 * execute.
693 *
694 * Thread options are architecture-specific, and can include K_ESSENTIAL,
695 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
696 * them using "|" (the logical OR operator).
697 *
698 * Historically, users often would use the beginning of the stack memory region
699 * to store the struct k_thread data, although corruption will occur if the
700 * stack overflows this region and stack protection features may not detect this
701 * situation.
702 *
703 * @param new_thread Pointer to uninitialized struct k_thread
704 * @param stack Pointer to the stack space.
705 * @param stack_size Stack size in bytes.
706 * @param entry Thread entry function.
707 * @param p1 1st entry point parameter.
708 * @param p2 2nd entry point parameter.
709 * @param p3 3rd entry point parameter.
710 * @param prio Thread priority.
711 * @param options Thread options.
712 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
713 *
714 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400715 *
716 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700717 */
Andrew Boie662c3452017-10-02 10:51:18 -0700718__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700719 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700720 size_t stack_size,
721 k_thread_entry_t entry,
722 void *p1, void *p2, void *p3,
723 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700724
Andrew Boie3f091b52017-08-30 14:34:14 -0700725/**
726 * @brief Drop a thread's privileges permanently to user mode
727 *
728 * @param entry Function to start executing from
729 * @param p1 1st entry point parameter
730 * @param p2 2nd entry point parameter
731 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400732 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700733 */
734extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
735 void *p1, void *p2,
736 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700737
Andrew Boied26cf2d2017-03-30 13:07:02 -0700738/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530739 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700740 *
741 * This is a convenience function. For the provided thread, grant access to
742 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700743 *
744 * The thread object must be initialized (i.e. running). The objects don't
745 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530746 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700747 *
748 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530749 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400750 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700751 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530752#define k_thread_access_grant(thread, ...) \
753 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700754
755/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700756 * @brief Assign a resource memory pool to a thread
757 *
758 * By default, threads have no resource pool assigned unless their parent
759 * thread has a resource pool, in which case it is inherited. Multiple
760 * threads may be assigned to the same memory pool.
761 *
762 * Changing a thread's resource pool will not migrate allocations from the
763 * previous pool.
764 *
765 * @param thread Target thread to assign a memory pool for resource requests,
766 * or NULL if the thread should no longer have a memory pool.
767 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400768 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700769 */
770static inline void k_thread_resource_pool_assign(struct k_thread *thread,
771 struct k_mem_pool *pool)
772{
773 thread->resource_pool = pool;
774}
775
776#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
777/**
778 * @brief Assign the system heap as a thread's resource pool
779 *
780 * Similar to k_thread_resource_pool_assign(), but the thread will use
781 * the kernel heap to draw memory.
782 *
783 * Use with caution, as a malicious thread could perform DoS attacks on the
784 * kernel heap.
785 *
786 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400787 *
788 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700789 */
790void k_thread_system_pool_assign(struct k_thread *thread);
791#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
792
793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700796 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700798 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200800 * @return Zero if the requested time has elapsed or the number of milliseconds
801 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700803__syscall s32_t k_sleep(s32_t ms);
804
805/**
806 * @brief Put the current thread to sleep with microsecond resolution.
807 *
808 * This function is unlikely to work as expected without kernel tuning.
809 * In particular, because the lower bound on the duration of a sleep is
810 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
811 * to achieve the resolution desired. The implications of doing this must
812 * be understood before attempting to use k_usleep(). Use with caution.
813 *
814 * @param us Number of microseconds to sleep.
815 *
816 * @return Zero if the requested time has elapsed or the number of microseconds
817 * left to sleep, if thread was woken up by \ref k_wakeup call.
818 */
819__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820
821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
824 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 * @return N/A
828 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800829__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830
831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
838 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400839 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 */
Andrew Boie468190a2017-09-29 14:00:48 -0700841__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842
843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * If @a thread is not currently sleeping, the routine has no effect.
849 *
850 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
852 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400853 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 */
Andrew Boie468190a2017-09-29 14:00:48 -0700855__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856
857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400861 *
862 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700864__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865
866/**
Allan Stephensc98da842016-11-11 15:45:03 -0500867 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * This routine permanently stops execution of @a thread. The thread is taken
870 * off all kernel queues it is part of (i.e. the ready queue, the timeout
871 * queue, or a kernel object wait queue). However, any kernel resources the
872 * thread might currently own (such as mutexes or memory blocks) are not
873 * released. It is the responsibility of the caller of this routine to ensure
874 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
878 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400879 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 */
Andrew Boie468190a2017-09-29 14:00:48 -0700881__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400882
Andrew Boie7d627c52017-08-30 11:01:56 -0700883
884/**
885 * @brief Start an inactive thread
886 *
887 * If a thread was created with K_FOREVER in the delay parameter, it will
888 * not be added to the scheduling queue until this function is called
889 * on it.
890 *
891 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400892 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700893 */
Andrew Boie468190a2017-09-29 14:00:48 -0700894__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700895
Allan Stephensc98da842016-11-11 15:45:03 -0500896/**
897 * @cond INTERNAL_HIDDEN
898 */
899
Benjamin Walshd211a522016-12-06 11:44:01 -0500900/* timeout has timed out and is not on _timeout_q anymore */
901#define _EXPIRED (-2)
902
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400903struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700904 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700905 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400906 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700907 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500908 void *init_p1;
909 void *init_p2;
910 void *init_p3;
911 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500912 u32_t init_options;
913 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500914 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600915 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400916};
917
Andrew Boied26cf2d2017-03-30 13:07:02 -0700918#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400919 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600920 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500921 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700922 .init_thread = (thread), \
923 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500924 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700925 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400926 .init_p1 = (void *)p1, \
927 .init_p2 = (void *)p2, \
928 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500929 .init_prio = (prio), \
930 .init_options = (options), \
931 .init_delay = (delay), \
932 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600933 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400934 }
935
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400936/**
Allan Stephensc98da842016-11-11 15:45:03 -0500937 * INTERNAL_HIDDEN @endcond
938 */
939
940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * @brief Statically define and initialize a thread.
942 *
943 * The thread may be scheduled for immediate execution or a delayed start.
944 *
945 * Thread options are architecture-specific, and can include K_ESSENTIAL,
946 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
947 * them using "|" (the logical OR operator).
948 *
949 * The ID of the thread can be accessed using:
950 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500951 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 *
953 * @param name Name of the thread.
954 * @param stack_size Stack size in bytes.
955 * @param entry Thread entry function.
956 * @param p1 1st entry point parameter.
957 * @param p2 2nd entry point parameter.
958 * @param p3 3rd entry point parameter.
959 * @param prio Thread priority.
960 * @param options Thread options.
961 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400962 *
Anas Nashif47420d02018-05-24 14:20:56 -0400963 * @req K-THREAD-010
964 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400965 * @internal It has been observed that the x86 compiler by default aligns
966 * these _static_thread_data structures to 32-byte boundaries, thereby
967 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400968 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400969 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500970#define K_THREAD_DEFINE(name, stack_size, \
971 entry, p1, p2, p3, \
972 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700973 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400974 struct k_thread _k_thread_obj_##name; \
975 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700976 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
977 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500978 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600979 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700980 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400981
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * @param thread ID of thread whose priority is needed.
988 *
989 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400990 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700992__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993
994/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
999 * Rescheduling can occur immediately depending on the priority @a thread is
1000 * set to:
1001 *
1002 * - If its priority is raised above the priority of the caller of this
1003 * function, and the caller is preemptible, @a thread will be scheduled in.
1004 *
1005 * - If the caller operates on itself, it lowers its priority below that of
1006 * other threads in the system, and the caller is preemptible, the thread of
1007 * highest priority will be scheduled in.
1008 *
1009 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1010 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1011 * highest priority.
1012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 * @param prio New priority.
1015 *
1016 * @warning Changing the priority of a thread currently involved in mutex
1017 * priority inheritance may result in undefined behavior.
1018 *
1019 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001020 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 */
Andrew Boie468190a2017-09-29 14:00:48 -07001022__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001023
Andy Ross4a2e50f2018-05-15 11:06:25 -07001024
1025#ifdef CONFIG_SCHED_DEADLINE
1026/**
1027 * @brief Set deadline expiration time for scheduler
1028 *
1029 * This sets the "deadline" expiration as a time delta from the
1030 * current time, in the same units used by k_cycle_get_32(). The
1031 * scheduler (when deadline scheduling is enabled) will choose the
1032 * next expiring thread when selecting between threads at the same
1033 * static priority. Threads at different priorities will be scheduled
1034 * according to their static priority.
1035 *
1036 * @note Deadlines that are negative (i.e. in the past) are still seen
1037 * as higher priority than others, even if the thread has "finished"
1038 * its work. If you don't want it scheduled anymore, you have to
1039 * reset the deadline into the future, block/pend the thread, or
1040 * modify its priority with k_thread_priority_set().
1041 *
1042 * @note Despite the API naming, the scheduler makes no guarantees the
1043 * the thread WILL be scheduled within that deadline, nor does it take
1044 * extra metadata (like e.g. the "runtime" and "period" parameters in
1045 * Linux sched_setattr()) that allows the kernel to validate the
1046 * scheduling for achievability. Such features could be implemented
1047 * above this call, which is simply input to the priority selection
1048 * logic.
1049 *
Anas Nashif240c5162019-06-10 12:25:50 -04001050 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001051 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001052 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1053 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001054 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001055 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001056 * @param thread A thread on which to set the deadline
1057 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001058 *
1059 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001060 */
1061__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1062#endif
1063
Andy Rossab46b1b2019-01-30 15:00:42 -08001064#ifdef CONFIG_SCHED_CPU_MASK
1065/**
1066 * @brief Sets all CPU enable masks to zero
1067 *
1068 * After this returns, the thread will no longer be schedulable on any
1069 * CPUs. The thread must not be currently runnable.
1070 *
Anas Nashif240c5162019-06-10 12:25:50 -04001071 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001072 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001073 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1074 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001075 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001076 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001077 * @param thread Thread to operate upon
1078 * @return Zero on success, otherwise error code
1079 */
1080int k_thread_cpu_mask_clear(k_tid_t thread);
1081
1082/**
1083 * @brief Sets all CPU enable masks to one
1084 *
1085 * After this returns, the thread will be schedulable on any CPU. The
1086 * thread must not be currently runnable.
1087 *
Anas Nashif240c5162019-06-10 12:25:50 -04001088 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001089 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001090 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1091 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001092 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001093 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001094 * @param thread Thread to operate upon
1095 * @return Zero on success, otherwise error code
1096 */
1097int k_thread_cpu_mask_enable_all(k_tid_t thread);
1098
1099/**
1100 * @brief Enable thread to run on specified CPU
1101 *
1102 * The thread must not be currently runnable.
1103 *
Anas Nashif240c5162019-06-10 12:25:50 -04001104 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001105 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001106 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1107 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001108 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001109 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001110 * @param thread Thread to operate upon
1111 * @param cpu CPU index
1112 * @return Zero on success, otherwise error code
1113 */
1114int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1115
1116/**
1117 * @brief Prevent thread to run on specified CPU
1118 *
1119 * The thread must not be currently runnable.
1120 *
Anas Nashif240c5162019-06-10 12:25:50 -04001121 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001122 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001123 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1124 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001125 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001126 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001127 * @param thread Thread to operate upon
1128 * @param cpu CPU index
1129 * @return Zero on success, otherwise error code
1130 */
1131int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1132#endif
1133
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * This routine prevents the kernel scheduler from making @a thread the
1138 * current thread. All other internal operations on @a thread are still
1139 * performed; for example, any timeout it is waiting on keeps ticking,
1140 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001142 * If @a thread is already suspended, the routine has no effect.
1143 *
1144 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 *
1146 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001147 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001148 */
Andrew Boie468190a2017-09-29 14:00:48 -07001149__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150
1151/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001152 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * This routine allows the kernel scheduler to make @a thread the current
1155 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001157 * If @a thread is not currently suspended, the routine has no effect.
1158 *
1159 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160 *
1161 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001162 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163 */
Andrew Boie468190a2017-09-29 14:00:48 -07001164__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001167 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * This routine specifies how the scheduler will perform time slicing of
1170 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001172 * To enable time slicing, @a slice must be non-zero. The scheduler
1173 * ensures that no thread runs for more than the specified time limit
1174 * before other threads of that priority are given a chance to execute.
1175 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001176 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 * execute. Once the scheduler selects a thread for execution, there is no
1180 * minimum guaranteed time the thread will execute before threads of greater or
1181 * equal priority are scheduled.
1182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 * for execution, this routine has no effect; the thread is immediately
1185 * rescheduled after the slice period expires.
1186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001187 * To disable timeslicing, set both @a slice and @a prio to zero.
1188 *
1189 * @param slice Maximum time slice length (in milliseconds).
1190 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191 *
1192 * @return N/A
1193 */
Kumar Galacc334c72017-04-21 10:55:34 -05001194extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001195
Anas Nashif166f5192018-02-25 08:02:36 -06001196/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001197
1198/**
1199 * @addtogroup isr_apis
1200 * @{
1201 */
1202
1203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001204 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
Allan Stephensc98da842016-11-11 15:45:03 -05001206 * This routine allows the caller to customize its actions, depending on
1207 * whether it is a thread or an ISR.
1208 *
1209 * @note Can be called by ISRs.
1210 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001211 * @return false if invoked by a thread.
1212 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001214extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001215
Benjamin Walsh445830d2016-11-10 15:54:27 -05001216/**
1217 * @brief Determine if code is running in a preemptible thread.
1218 *
Allan Stephensc98da842016-11-11 15:45:03 -05001219 * This routine allows the caller to customize its actions, depending on
1220 * whether it can be preempted by another thread. The routine returns a 'true'
1221 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001222 *
Allan Stephensc98da842016-11-11 15:45:03 -05001223 * - The code is running in a thread, not at ISR.
1224 * - The thread's priority is in the preemptible range.
1225 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001226 *
Allan Stephensc98da842016-11-11 15:45:03 -05001227 * @note Can be called by ISRs.
1228 *
1229 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001230 * @return Non-zero if invoked by a preemptible thread.
1231 */
Andrew Boie468190a2017-09-29 14:00:48 -07001232__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001233
Allan Stephensc98da842016-11-11 15:45:03 -05001234/**
Anas Nashif166f5192018-02-25 08:02:36 -06001235 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001236 */
1237
1238/**
1239 * @addtogroup thread_apis
1240 * @{
1241 */
1242
1243/**
1244 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001245 *
Allan Stephensc98da842016-11-11 15:45:03 -05001246 * This routine prevents the current thread from being preempted by another
1247 * thread by instructing the scheduler to treat it as a cooperative thread.
1248 * If the thread subsequently performs an operation that makes it unready,
1249 * it will be context switched out in the normal manner. When the thread
1250 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001251 *
Allan Stephensc98da842016-11-11 15:45:03 -05001252 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001253 *
Allan Stephensc98da842016-11-11 15:45:03 -05001254 * @note k_sched_lock() and k_sched_unlock() should normally be used
1255 * when the operation being performed can be safely interrupted by ISRs.
1256 * However, if the amount of processing involved is very small, better
1257 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001258 *
1259 * @return N/A
1260 */
1261extern void k_sched_lock(void);
1262
Allan Stephensc98da842016-11-11 15:45:03 -05001263/**
1264 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001265 *
Allan Stephensc98da842016-11-11 15:45:03 -05001266 * This routine reverses the effect of a previous call to k_sched_lock().
1267 * A thread must call the routine once for each time it called k_sched_lock()
1268 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001269 *
1270 * @return N/A
1271 */
1272extern void k_sched_unlock(void);
1273
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * Custom data is not used by the kernel itself, and is freely available
1280 * for a thread to use as it sees fit. It can be used as a framework
1281 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001283 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 *
1285 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001286 *
1287 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001288 */
Andrew Boie468190a2017-09-29 14:00:48 -07001289__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290
1291/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001297 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001298 */
Andrew Boie468190a2017-09-29 14:00:48 -07001299__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001300
1301/**
Anas Nashif57554052018-03-03 02:31:05 -06001302 * @brief Set current thread name
1303 *
1304 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1305 * tracing and debugging.
1306 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001307 * @param thread_id Thread to set name, or NULL to set the current thread
1308 * @param value Name string
1309 * @retval 0 on success
1310 * @retval -EFAULT Memory access error with supplied string
1311 * @retval -ENOSYS Thread name configuration option not enabled
1312 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001313 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001314__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001315
1316/**
1317 * @brief Get thread name
1318 *
1319 * Get the name of a thread
1320 *
1321 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001322 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001323 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001324const char *k_thread_name_get(k_tid_t thread_id);
1325
1326/**
1327 * @brief Copy the thread name into a supplied buffer
1328 *
1329 * @param thread_id Thread to obtain name information
1330 * @param buf Destination buffer
1331 * @param size Destinatiomn buffer size
1332 * @retval -ENOSPC Destination buffer too small
1333 * @retval -EFAULT Memory access error
1334 * @retval -ENOSYS Thread name feature not enabled
1335 * @retval 0 Success
1336 */
1337__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1338 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001339
1340/**
Andy Rosscfe62032018-09-29 07:34:55 -07001341 * @}
1342 */
1343
1344/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001345 * @addtogroup clock_apis
1346 * @{
1347 */
1348
1349/**
1350 * @brief Generate null timeout delay.
1351 *
1352 * This macro generates a timeout delay that that instructs a kernel API
1353 * not to wait if the requested operation cannot be performed immediately.
1354 *
1355 * @return Timeout delay value.
1356 */
1357#define K_NO_WAIT 0
1358
1359/**
1360 * @brief Generate timeout delay from milliseconds.
1361 *
1362 * This macro generates a timeout delay that that instructs a kernel API
1363 * to wait up to @a ms milliseconds to perform the requested operation.
1364 *
1365 * @param ms Duration in milliseconds.
1366 *
1367 * @return Timeout delay value.
1368 */
Johan Hedberg14471692016-11-13 10:52:15 +02001369#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001370
1371/**
1372 * @brief Generate timeout delay from seconds.
1373 *
1374 * This macro generates a timeout delay that that instructs a kernel API
1375 * to wait up to @a s seconds to perform the requested operation.
1376 *
1377 * @param s Duration in seconds.
1378 *
1379 * @return Timeout delay value.
1380 */
Johan Hedberg14471692016-11-13 10:52:15 +02001381#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001382
1383/**
1384 * @brief Generate timeout delay from minutes.
1385 *
1386 * This macro generates a timeout delay that that instructs a kernel API
1387 * to wait up to @a m minutes to perform the requested operation.
1388 *
1389 * @param m Duration in minutes.
1390 *
1391 * @return Timeout delay value.
1392 */
Johan Hedberg14471692016-11-13 10:52:15 +02001393#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001394
1395/**
1396 * @brief Generate timeout delay from hours.
1397 *
1398 * This macro generates a timeout delay that that instructs a kernel API
1399 * to wait up to @a h hours to perform the requested operation.
1400 *
1401 * @param h Duration in hours.
1402 *
1403 * @return Timeout delay value.
1404 */
Johan Hedberg14471692016-11-13 10:52:15 +02001405#define K_HOURS(h) K_MINUTES((h) * 60)
1406
Allan Stephensc98da842016-11-11 15:45:03 -05001407/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001408 * @brief Generate infinite timeout delay.
1409 *
1410 * This macro generates a timeout delay that that instructs a kernel API
1411 * to wait as long as necessary to perform the requested operation.
1412 *
1413 * @return Timeout delay value.
1414 */
1415#define K_FOREVER (-1)
1416
1417/**
Anas Nashif166f5192018-02-25 08:02:36 -06001418 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001419 */
1420
1421/**
Allan Stephensc98da842016-11-11 15:45:03 -05001422 * @cond INTERNAL_HIDDEN
1423 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001424
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001425struct k_timer {
1426 /*
1427 * _timeout structure must be first here if we want to use
1428 * dynamic timer allocation. timeout.node is used in the double-linked
1429 * list of free timers
1430 */
1431 struct _timeout timeout;
1432
Allan Stephens45bfa372016-10-12 12:39:42 -05001433 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434 _wait_q_t wait_q;
1435
1436 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001437 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438
1439 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001440 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001441
1442 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001443 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444
Allan Stephens45bfa372016-10-12 12:39:42 -05001445 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001446 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001447
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001448 /* user-specific data, also used to support legacy features */
1449 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001451 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001452};
1453
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001454#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001456 .timeout = { \
1457 .node = {},\
1458 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001459 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001460 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001461 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001462 .expiry_fn = expiry, \
1463 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001464 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001465 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001466 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001467 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001468 }
1469
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001470#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001471
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001472/**
Allan Stephensc98da842016-11-11 15:45:03 -05001473 * INTERNAL_HIDDEN @endcond
1474 */
1475
1476/**
1477 * @defgroup timer_apis Timer APIs
1478 * @ingroup kernel_apis
1479 * @{
1480 */
1481
1482/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001483 * @typedef k_timer_expiry_t
1484 * @brief Timer expiry function type.
1485 *
1486 * A timer's expiry function is executed by the system clock interrupt handler
1487 * each time the timer expires. The expiry function is optional, and is only
1488 * invoked if the timer has been initialized with one.
1489 *
1490 * @param timer Address of timer.
1491 *
1492 * @return N/A
1493 */
1494typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1495
1496/**
1497 * @typedef k_timer_stop_t
1498 * @brief Timer stop function type.
1499 *
1500 * A timer's stop function is executed if the timer is stopped prematurely.
1501 * The function runs in the context of the thread that stops the timer.
1502 * The stop function is optional, and is only invoked if the timer has been
1503 * initialized with one.
1504 *
1505 * @param timer Address of timer.
1506 *
1507 * @return N/A
1508 */
1509typedef void (*k_timer_stop_t)(struct k_timer *timer);
1510
1511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001512 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001514 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001515 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001516 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001517 *
1518 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001519 * @param expiry_fn Function to invoke each time the timer expires.
1520 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001521 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001522#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001523 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001524 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001525
Allan Stephens45bfa372016-10-12 12:39:42 -05001526/**
1527 * @brief Initialize a timer.
1528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001529 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001530 *
1531 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001532 * @param expiry_fn Function to invoke each time the timer expires.
1533 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001534 *
1535 * @return N/A
1536 */
1537extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001538 k_timer_expiry_t expiry_fn,
1539 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001540
Allan Stephens45bfa372016-10-12 12:39:42 -05001541/**
1542 * @brief Start a timer.
1543 *
1544 * This routine starts a timer, and resets its status to zero. The timer
1545 * begins counting down using the specified duration and period values.
1546 *
1547 * Attempting to start a timer that is already running is permitted.
1548 * The timer's status is reset to zero and the timer begins counting down
1549 * using the new duration and period values.
1550 *
1551 * @param timer Address of timer.
1552 * @param duration Initial timer duration (in milliseconds).
1553 * @param period Timer period (in milliseconds).
1554 *
1555 * @return N/A
1556 */
Andrew Boiea354d492017-09-29 16:22:28 -07001557__syscall void k_timer_start(struct k_timer *timer,
1558 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001559
1560/**
1561 * @brief Stop a timer.
1562 *
1563 * This routine stops a running timer prematurely. The timer's stop function,
1564 * if one exists, is invoked by the caller.
1565 *
1566 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001567 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001568 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001569 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1570 * if @a k_timer_stop is to be called from ISRs.
1571 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001572 * @param timer Address of timer.
1573 *
1574 * @return N/A
1575 */
Andrew Boiea354d492017-09-29 16:22:28 -07001576__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001577
1578/**
1579 * @brief Read timer status.
1580 *
1581 * This routine reads the timer's status, which indicates the number of times
1582 * it has expired since its status was last read.
1583 *
1584 * Calling this routine resets the timer's status to zero.
1585 *
1586 * @param timer Address of timer.
1587 *
1588 * @return Timer status.
1589 */
Andrew Boiea354d492017-09-29 16:22:28 -07001590__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001591
1592/**
1593 * @brief Synchronize thread to timer expiration.
1594 *
1595 * This routine blocks the calling thread until the timer's status is non-zero
1596 * (indicating that it has expired at least once since it was last examined)
1597 * or the timer is stopped. If the timer status is already non-zero,
1598 * or the timer is already stopped, the caller continues without waiting.
1599 *
1600 * Calling this routine resets the timer's status to zero.
1601 *
1602 * This routine must not be used by interrupt handlers, since they are not
1603 * allowed to block.
1604 *
1605 * @param timer Address of timer.
1606 *
1607 * @return Timer status.
1608 */
Andrew Boiea354d492017-09-29 16:22:28 -07001609__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001610
Andy Ross52e444b2018-09-28 09:06:37 -07001611extern s32_t z_timeout_remaining(struct _timeout *timeout);
1612
Allan Stephens45bfa372016-10-12 12:39:42 -05001613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001615 *
1616 * This routine computes the (approximate) time remaining before a running
1617 * timer next expires. If the timer is not running, it returns zero.
1618 *
1619 * @param timer Address of timer.
1620 *
1621 * @return Remaining time (in milliseconds).
1622 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001623__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001624
Patrik Flykt4344e272019-03-08 14:19:05 -07001625static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001626{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001627 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1628 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001629}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001630
Allan Stephensc98da842016-11-11 15:45:03 -05001631/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001632 * @brief Associate user-specific data with a timer.
1633 *
1634 * This routine records the @a user_data with the @a timer, to be retrieved
1635 * later.
1636 *
1637 * It can be used e.g. in a timer handler shared across multiple subsystems to
1638 * retrieve data specific to the subsystem this timer is associated with.
1639 *
1640 * @param timer Address of timer.
1641 * @param user_data User data to associate with the timer.
1642 *
1643 * @return N/A
1644 */
Andrew Boiea354d492017-09-29 16:22:28 -07001645__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1646
Anas Nashif954d5502018-02-25 08:37:28 -06001647/**
1648 * @internal
1649 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001650static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001651 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001652{
1653 timer->user_data = user_data;
1654}
1655
1656/**
1657 * @brief Retrieve the user-specific data from a timer.
1658 *
1659 * @param timer Address of timer.
1660 *
1661 * @return The user data.
1662 */
Andrew Boiea354d492017-09-29 16:22:28 -07001663__syscall void *k_timer_user_data_get(struct k_timer *timer);
1664
Patrik Flykt4344e272019-03-08 14:19:05 -07001665static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001666{
1667 return timer->user_data;
1668}
1669
Anas Nashif166f5192018-02-25 08:02:36 -06001670/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001671
Allan Stephensc98da842016-11-11 15:45:03 -05001672/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001673 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001674 * @{
1675 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001676
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001680 * This routine returns the elapsed time since the system booted,
1681 * in milliseconds.
1682 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001683 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001684 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001685 * While this function returns time in milliseconds, it does
1686 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001687 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001688 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001689 *
1690 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001691 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001692__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001693
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001694/**
1695 * @brief Enable clock always on in tickless kernel
1696 *
Andy Ross1db9f182019-06-25 10:09:45 -07001697 * Deprecated. This does nothing (it was always just a hint). This
1698 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1699 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001700 *
1701 * @retval prev_status Previous status of always on flag
1702 */
Andy Ross1db9f182019-06-25 10:09:45 -07001703/* LCOV_EXCL_START */
1704__deprecated static inline int k_enable_sys_clock_always_on(void)
1705{
1706 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1707 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1708
1709 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1710}
1711/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001712
1713/**
1714 * @brief Disable clock always on in tickless kernel
1715 *
Andy Ross1db9f182019-06-25 10:09:45 -07001716 * Deprecated. This does nothing (it was always just a hint). This
1717 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1718 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001719 */
Andy Ross1db9f182019-06-25 10:09:45 -07001720/* LCOV_EXCL_START */
1721__deprecated static inline void k_disable_sys_clock_always_on(void)
1722{
1723 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1724 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1725}
1726/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001727
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * This routine returns the lower 32-bits of the elapsed time since the system
1732 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * This routine can be more efficient than k_uptime_get(), as it reduces the
1735 * need for interrupt locking and 64-bit math. However, the 32-bit result
1736 * cannot hold a system uptime time larger than approximately 50 days, so the
1737 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001738 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001739 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001740 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001741 * While this function returns time in milliseconds, it does
1742 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001743 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001744 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001745 *
1746 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001747 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001748__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001749
1750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * This routine computes the elapsed time between the current system uptime
1754 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001756 * @param reftime Pointer to a reference time, which is updated to the current
1757 * uptime upon return.
1758 *
1759 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001760 */
Andy Ross987c0e52018-09-27 16:50:00 -07001761static inline s64_t k_uptime_delta(s64_t *reftime)
1762{
1763 s64_t uptime, delta;
1764
1765 uptime = k_uptime_get();
1766 delta = uptime - *reftime;
1767 *reftime = uptime;
1768
1769 return delta;
1770}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001771
1772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * This routine computes the elapsed time between the current system uptime
1776 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001778 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1779 * need for interrupt locking and 64-bit math. However, the 32-bit result
1780 * cannot hold an elapsed time larger than approximately 50 days, so the
1781 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001783 * @param reftime Pointer to a reference time, which is updated to the current
1784 * uptime upon return.
1785 *
1786 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001787 */
Andy Ross987c0e52018-09-27 16:50:00 -07001788static inline u32_t k_uptime_delta_32(s64_t *reftime)
1789{
1790 return (u32_t)k_uptime_delta(reftime);
1791}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001792
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * This routine returns the current time, as measured by the system's hardware
1797 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001801#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001802
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001803/**
Anas Nashif166f5192018-02-25 08:02:36 -06001804 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001805 */
1806
Allan Stephensc98da842016-11-11 15:45:03 -05001807/**
1808 * @cond INTERNAL_HIDDEN
1809 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001810
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001811struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001812 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001813 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001814 union {
1815 _wait_q_t wait_q;
1816
1817 _POLL_EVENT;
1818 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001819
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001820 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001821};
1822
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001823#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001824 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001825 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001826 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001827 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001828 _OBJECT_TRACING_INIT \
1829 }
1830
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001831#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1832
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001833extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1834
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001835/**
1836 * INTERNAL_HIDDEN @endcond
1837 */
1838
1839/**
1840 * @defgroup queue_apis Queue APIs
1841 * @ingroup kernel_apis
1842 * @{
1843 */
1844
1845/**
1846 * @brief Initialize a queue.
1847 *
1848 * This routine initializes a queue object, prior to its first use.
1849 *
1850 * @param queue Address of the queue.
1851 *
1852 * @return N/A
1853 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001854__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001855
1856/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001857 * @brief Cancel waiting on a queue.
1858 *
1859 * This routine causes first thread pending on @a queue, if any, to
1860 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001861 * If the queue is being waited on by k_poll(), it will return with
1862 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1863 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001864 *
1865 * @note Can be called by ISRs.
1866 *
1867 * @param queue Address of the queue.
1868 *
1869 * @return N/A
1870 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001871__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001872
1873/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001874 * @brief Append an element to the end of a queue.
1875 *
1876 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001877 * aligned on a word boundary, and the first word of the item is reserved
1878 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001879 *
1880 * @note Can be called by ISRs.
1881 *
1882 * @param queue Address of the queue.
1883 * @param data Address of the data item.
1884 *
1885 * @return N/A
1886 */
1887extern void k_queue_append(struct k_queue *queue, void *data);
1888
1889/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001890 * @brief Append an element to a queue.
1891 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001892 * This routine appends a data item to @a queue. There is an implicit memory
1893 * allocation to create an additional temporary bookkeeping data structure from
1894 * the calling thread's resource pool, which is automatically freed when the
1895 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001896 *
1897 * @note Can be called by ISRs.
1898 *
1899 * @param queue Address of the queue.
1900 * @param data Address of the data item.
1901 *
1902 * @retval 0 on success
1903 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1904 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301905__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001906
1907/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001908 * @brief Prepend an element to a queue.
1909 *
1910 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001911 * aligned on a word boundary, and the first word of the item is reserved
1912 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001913 *
1914 * @note Can be called by ISRs.
1915 *
1916 * @param queue Address of the queue.
1917 * @param data Address of the data item.
1918 *
1919 * @return N/A
1920 */
1921extern void k_queue_prepend(struct k_queue *queue, void *data);
1922
1923/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001924 * @brief Prepend an element to a queue.
1925 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001926 * This routine prepends a data item to @a queue. There is an implicit memory
1927 * allocation to create an additional temporary bookkeeping data structure from
1928 * the calling thread's resource pool, which is automatically freed when the
1929 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001930 *
1931 * @note Can be called by ISRs.
1932 *
1933 * @param queue Address of the queue.
1934 * @param data Address of the data item.
1935 *
1936 * @retval 0 on success
1937 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1938 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301939__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001940
1941/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001942 * @brief Inserts an element to a queue.
1943 *
1944 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001945 * data item must be aligned on a word boundary, and the first word of
1946 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001947 *
1948 * @note Can be called by ISRs.
1949 *
1950 * @param queue Address of the queue.
1951 * @param prev Address of the previous data item.
1952 * @param data Address of the data item.
1953 *
1954 * @return N/A
1955 */
1956extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1957
1958/**
1959 * @brief Atomically append a list of elements to a queue.
1960 *
1961 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001962 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001963 * in each data item pointing to the next data item; the list must be
1964 * NULL-terminated.
1965 *
1966 * @note Can be called by ISRs.
1967 *
1968 * @param queue Address of the queue.
1969 * @param head Pointer to first node in singly-linked list.
1970 * @param tail Pointer to last node in singly-linked list.
1971 *
1972 * @return N/A
1973 */
1974extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1975
1976/**
1977 * @brief Atomically add a list of elements to a queue.
1978 *
1979 * This routine adds a list of data items to @a queue in one operation.
1980 * The data items must be in a singly-linked list implemented using a
1981 * sys_slist_t object. Upon completion, the original list is empty.
1982 *
1983 * @note Can be called by ISRs.
1984 *
1985 * @param queue Address of the queue.
1986 * @param list Pointer to sys_slist_t object.
1987 *
1988 * @return N/A
1989 */
1990extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1991
1992/**
1993 * @brief Get an element from a queue.
1994 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001995 * This routine removes first data item from @a queue. The first word of the
1996 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001997 *
1998 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1999 *
2000 * @param queue Address of the queue.
2001 * @param timeout Waiting period to obtain a data item (in milliseconds),
2002 * or one of the special values K_NO_WAIT and K_FOREVER.
2003 *
2004 * @return Address of the data item if successful; NULL if returned
2005 * without waiting, or waiting period timed out.
2006 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002007__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002008
2009/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002010 * @brief Remove an element from a queue.
2011 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002012 * This routine removes data item from @a queue. The first word of the
2013 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002014 * rely on sys_slist_find_and_remove which is not a constant time operation.
2015 *
2016 * @note Can be called by ISRs
2017 *
2018 * @param queue Address of the queue.
2019 * @param data Address of the data item.
2020 *
2021 * @return true if data item was removed
2022 */
2023static inline bool k_queue_remove(struct k_queue *queue, void *data)
2024{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002025 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002026}
2027
2028/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002029 * @brief Append an element to a queue only if it's not present already.
2030 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002031 * This routine appends data item to @a queue. The first word of the data
2032 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002033 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2034 *
2035 * @note Can be called by ISRs
2036 *
2037 * @param queue Address of the queue.
2038 * @param data Address of the data item.
2039 *
2040 * @return true if data item was added, false if not
2041 */
2042static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2043{
2044 sys_sfnode_t *test;
2045
2046 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2047 if (test == (sys_sfnode_t *) data) {
2048 return false;
2049 }
2050 }
2051
2052 k_queue_append(queue, data);
2053 return true;
2054}
2055
2056/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002057 * @brief Query a queue to see if it has data available.
2058 *
2059 * Note that the data might be already gone by the time this function returns
2060 * if other threads are also trying to read from the queue.
2061 *
2062 * @note Can be called by ISRs.
2063 *
2064 * @param queue Address of the queue.
2065 *
2066 * @return Non-zero if the queue is empty.
2067 * @return 0 if data is available.
2068 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002069__syscall int k_queue_is_empty(struct k_queue *queue);
2070
Patrik Flykt4344e272019-03-08 14:19:05 -07002071static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002072{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002073 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002074}
2075
2076/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002077 * @brief Peek element at the head of queue.
2078 *
2079 * Return element from the head of queue without removing it.
2080 *
2081 * @param queue Address of the queue.
2082 *
2083 * @return Head element, or NULL if queue is empty.
2084 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002085__syscall void *k_queue_peek_head(struct k_queue *queue);
2086
Patrik Flykt4344e272019-03-08 14:19:05 -07002087static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002088{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002089 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002090}
2091
2092/**
2093 * @brief Peek element at the tail of queue.
2094 *
2095 * Return element from the tail of queue without removing it.
2096 *
2097 * @param queue Address of the queue.
2098 *
2099 * @return Tail element, or NULL if queue is empty.
2100 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002101__syscall void *k_queue_peek_tail(struct k_queue *queue);
2102
Patrik Flykt4344e272019-03-08 14:19:05 -07002103static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002104{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002105 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002106}
2107
2108/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002109 * @brief Statically define and initialize a queue.
2110 *
2111 * The queue can be accessed outside the module where it is defined using:
2112 *
2113 * @code extern struct k_queue <name>; @endcode
2114 *
2115 * @param name Name of the queue.
2116 */
2117#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002118 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002119 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002120
Anas Nashif166f5192018-02-25 08:02:36 -06002121/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002122
Wentong Wu5611e922019-06-20 23:51:27 +08002123#ifdef CONFIG_USERSPACE
2124/**
2125 * @brief futex structure
2126 *
2127 * A k_futex is a lightweight mutual exclusion primitive designed
2128 * to minimize kernel involvement. Uncontended operation relies
2129 * only on atomic access to shared memory. k_futex are tracked as
2130 * kernel objects and can live in user memory so any access bypass
2131 * the kernel object permission management mechanism.
2132 */
2133struct k_futex {
2134 atomic_t val;
2135};
2136
2137/**
2138 * @brief futex kernel data structure
2139 *
2140 * z_futex_data are the helper data structure for k_futex to complete
2141 * futex contended operation on kernel side, structure z_futex_data
2142 * of every futex object is invisible in user mode.
2143 */
2144struct z_futex_data {
2145 _wait_q_t wait_q;
2146 struct k_spinlock lock;
2147};
2148
2149#define Z_FUTEX_DATA_INITIALIZER(obj) \
2150 { \
2151 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2152 }
2153
2154/**
2155 * @defgroup futex_apis FUTEX APIs
2156 * @ingroup kernel_apis
2157 * @{
2158 */
2159
2160/**
Wentong Wu5611e922019-06-20 23:51:27 +08002161 * @brief Pend the current thread on a futex
2162 *
2163 * Tests that the supplied futex contains the expected value, and if so,
2164 * goes to sleep until some other thread calls k_futex_wake() on it.
2165 *
2166 * @param futex Address of the futex.
2167 * @param expected Expected value of the futex, if it is different the caller
2168 * will not wait on it.
2169 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2170 * special values K_NO_WAIT or K_FOREVER.
2171 * @retval -EACCES Caller does not have read access to futex address.
2172 * @retval -EAGAIN If the futex value did not match the expected parameter.
2173 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2174 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2175 * @retval 0 if the caller went to sleep and was woken up. The caller
2176 * should check the futex's value on wakeup to determine if it needs
2177 * to block again.
2178 */
2179__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2180
2181/**
2182 * @brief Wake one/all threads pending on a futex
2183 *
2184 * Wake up the highest priority thread pending on the supplied futex, or
2185 * wakeup all the threads pending on the supplied futex, and the behavior
2186 * depends on wake_all.
2187 *
2188 * @param futex Futex to wake up pending threads.
2189 * @param wake_all If true, wake up all pending threads; If false,
2190 * wakeup the highest priority thread.
2191 * @retval -EACCES Caller does not have access to the futex address.
2192 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2193 * @retval Number of threads that were woken up.
2194 */
2195__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2196
2197/** @} */
2198#endif
2199
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002200struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002201 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202};
2203
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002204/**
2205 * @cond INTERNAL_HIDDEN
2206 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002207#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002208 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002209 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002210 }
2211
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002212#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002213
Allan Stephensc98da842016-11-11 15:45:03 -05002214/**
2215 * INTERNAL_HIDDEN @endcond
2216 */
2217
2218/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002220 * @ingroup kernel_apis
2221 * @{
2222 */
2223
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002224/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002225 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002227 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002229 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
2231 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002232 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002234#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002235 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236
2237/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002238 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002239 *
2240 * This routine causes first thread pending on @a fifo, if any, to
2241 * return from k_fifo_get() call with NULL value (as if timeout
2242 * expired).
2243 *
2244 * @note Can be called by ISRs.
2245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002247 *
2248 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002249 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002250 */
2251#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002252 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002253
2254/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002255 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002258 * aligned on a word boundary, and the first word of the item is reserved
2259 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002261 * @note Can be called by ISRs.
2262 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002263 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002265 *
2266 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002267 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002269#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002270 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002271
2272/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002273 * @brief Add an element to a FIFO queue.
2274 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002275 * This routine adds a data item to @a fifo. There is an implicit memory
2276 * allocation to create an additional temporary bookkeeping data structure from
2277 * the calling thread's resource pool, which is automatically freed when the
2278 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002279 *
2280 * @note Can be called by ISRs.
2281 *
2282 * @param fifo Address of the FIFO.
2283 * @param data Address of the data item.
2284 *
2285 * @retval 0 on success
2286 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002287 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002288 */
2289#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002290 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002291
2292/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002293 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002295 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002296 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * each data item pointing to the next data item; the list must be
2298 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002300 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002302 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002303 * @param head Pointer to first node in singly-linked list.
2304 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
2306 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002307 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002309#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002310 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311
2312/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002313 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * This routine adds a list of data items to @a fifo in one operation.
2316 * The data items must be in a singly-linked list implemented using a
2317 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 * and must be re-initialized via sys_slist_init().
2319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002320 * @note Can be called by ISRs.
2321 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002322 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
2325 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002326 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002328#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002329 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330
2331/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002332 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002335 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2338 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002339 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 * or one of the special values K_NO_WAIT and K_FOREVER.
2342 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002343 * @return Address of the data item if successful; NULL if returned
2344 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002345 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002347#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002348 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002351 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002352 *
2353 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002355 *
2356 * @note Can be called by ISRs.
2357 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002358 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002359 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002360 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002361 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002362 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002363 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002364#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002365 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002366
2367/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002368 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002369 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002370 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302371 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002372 * on each iteration of processing, a head container will be peeked,
2373 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002374 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002375 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002376 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002377 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002378 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002379 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002380 */
2381#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002382 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002383
2384/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002385 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002386 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002387 * Return element from the tail of FIFO queue (without removing it). A usecase
2388 * for this is if elements of the FIFO queue are themselves containers. Then
2389 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002390 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002391 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002392 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002393 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002394 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002395 */
2396#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002397 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002398
2399/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002400 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002402 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002404 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002405 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002406 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002407 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002408 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002410 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002411 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412
Anas Nashif166f5192018-02-25 08:02:36 -06002413/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002414
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002416 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417};
2418
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002419/**
2420 * @cond INTERNAL_HIDDEN
2421 */
2422
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002423#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002424 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002425 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002426 }
2427
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002428#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2429
Allan Stephensc98da842016-11-11 15:45:03 -05002430/**
2431 * INTERNAL_HIDDEN @endcond
2432 */
2433
2434/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002435 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002436 * @ingroup kernel_apis
2437 * @{
2438 */
2439
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002441 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002443 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002445 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446 *
2447 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002448 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002450#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002451 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002452
2453/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002454 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002456 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002457 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002458 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002460 * @note Can be called by ISRs.
2461 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002462 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 *
2465 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002466 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002468#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002469 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470
2471/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002472 * @brief Add an element to a LIFO queue.
2473 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002474 * This routine adds a data item to @a lifo. There is an implicit memory
2475 * allocation to create an additional temporary bookkeeping data structure from
2476 * the calling thread's resource pool, which is automatically freed when the
2477 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002478 *
2479 * @note Can be called by ISRs.
2480 *
2481 * @param lifo Address of the LIFO.
2482 * @param data Address of the data item.
2483 *
2484 * @retval 0 on success
2485 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002486 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002487 */
2488#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002489 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002490
2491/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002492 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002495 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2498 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002499 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 * or one of the special values K_NO_WAIT and K_FOREVER.
2502 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002503 * @return Address of the data item if successful; NULL if returned
2504 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002505 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002507#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002508 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002511 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002513 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002515 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002518 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002521 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002522 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523
Anas Nashif166f5192018-02-25 08:02:36 -06002524/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002525
2526/**
2527 * @cond INTERNAL_HIDDEN
2528 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302529#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002531typedef uintptr_t stack_data_t;
2532
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533struct k_stack {
2534 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002535 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002536 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002538 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002539 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540};
2541
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002542#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002543 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002544 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002545 .base = stack_buffer, \
2546 .next = stack_buffer, \
2547 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002548 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002549 }
2550
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002551#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2552
Allan Stephensc98da842016-11-11 15:45:03 -05002553/**
2554 * INTERNAL_HIDDEN @endcond
2555 */
2556
2557/**
2558 * @defgroup stack_apis Stack APIs
2559 * @ingroup kernel_apis
2560 * @{
2561 */
2562
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563/**
2564 * @brief Initialize a stack.
2565 *
2566 * This routine initializes a stack object, prior to its first use.
2567 *
2568 * @param stack Address of the stack.
2569 * @param buffer Address of array used to hold stacked values.
2570 * @param num_entries Maximum number of values that can be stacked.
2571 *
2572 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002573 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 */
Andrew Boief3bee952018-05-02 17:44:39 -07002575void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002576 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002577
2578
2579/**
2580 * @brief Initialize a stack.
2581 *
2582 * This routine initializes a stack object, prior to its first use. Internal
2583 * buffers will be allocated from the calling thread's resource pool.
2584 * This memory will be released if k_stack_cleanup() is called, or
2585 * userspace is enabled and the stack object loses all references to it.
2586 *
2587 * @param stack Address of the stack.
2588 * @param num_entries Maximum number of values that can be stacked.
2589 *
2590 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002591 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002592 */
2593
Adithya Baglody28080d32018-10-15 11:48:51 +05302594__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2595 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002596
2597/**
2598 * @brief Release a stack's allocated buffer
2599 *
2600 * If a stack object was given a dynamically allocated buffer via
2601 * k_stack_alloc_init(), this will free it. This function does nothing
2602 * if the buffer wasn't dynamically allocated.
2603 *
2604 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002605 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002606 */
2607void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608
2609/**
2610 * @brief Push an element onto a stack.
2611 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002612 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002613 *
2614 * @note Can be called by ISRs.
2615 *
2616 * @param stack Address of the stack.
2617 * @param data Value to push onto the stack.
2618 *
2619 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002620 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002622__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002623
2624/**
2625 * @brief Pop an element from a stack.
2626 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002627 * This routine removes a stack_data_t value from @a stack in a "last in,
2628 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 *
2630 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2631 *
2632 * @param stack Address of the stack.
2633 * @param data Address of area to hold the value popped from the stack.
2634 * @param timeout Waiting period to obtain a value (in milliseconds),
2635 * or one of the special values K_NO_WAIT and K_FOREVER.
2636 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002637 * @retval 0 Element popped from stack.
2638 * @retval -EBUSY Returned without waiting.
2639 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002640 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002642__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002648 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002649 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * @param name Name of the stack.
2652 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002653 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002655#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002656 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002657 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002658 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002659 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002660 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002661
Anas Nashif166f5192018-02-25 08:02:36 -06002662/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002663
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664struct k_work;
2665
Allan Stephensc98da842016-11-11 15:45:03 -05002666/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002667 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002668 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669 */
2670
Allan Stephens6bba9b02016-11-16 14:56:54 -05002671/**
2672 * @typedef k_work_handler_t
2673 * @brief Work item handler function type.
2674 *
2675 * A work item's handler function is executed by a workqueue's thread
2676 * when the work item is processed by the workqueue.
2677 *
2678 * @param work Address of the work item.
2679 *
2680 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002681 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002682 */
2683typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684
2685/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002690 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002691 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692};
2693
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002695 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696};
2697
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002699 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700 k_work_handler_t handler;
2701 atomic_t flags[1];
2702};
2703
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704struct k_delayed_work {
2705 struct k_work work;
2706 struct _timeout timeout;
2707 struct k_work_q *work_q;
2708};
2709
2710extern struct k_work_q k_sys_work_q;
2711
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * INTERNAL_HIDDEN @endcond
2714 */
2715
Patrik Flykt4344e272019-03-08 14:19:05 -07002716#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002717 { \
2718 ._reserved = NULL, \
2719 .handler = work_handler, \
2720 .flags = { 0 } \
2721 }
2722
Patrik Flykt4344e272019-03-08 14:19:05 -07002723#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002724
Allan Stephens6bba9b02016-11-16 14:56:54 -05002725/**
2726 * @brief Initialize a statically-defined work item.
2727 *
2728 * This macro can be used to initialize a statically-defined workqueue work
2729 * item, prior to its first use. For example,
2730 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002731 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002733 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002734 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002735 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002737#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002738 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739
2740/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002741 * @brief Initialize a work item.
2742 *
2743 * This routine initializes a workqueue work item, prior to its first use.
2744 *
2745 * @param work Address of work item.
2746 * @param handler Function to invoke each time work item is processed.
2747 *
2748 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002749 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 */
2751static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2752{
Patrik Flykt4344e272019-03-08 14:19:05 -07002753 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754}
2755
2756/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002758 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002759 * This routine submits work item @a work to be processed by workqueue
2760 * @a work_q. If the work item is already pending in the workqueue's queue
2761 * as a result of an earlier submission, this routine has no effect on the
2762 * work item. If the work item has already been processed, or is currently
2763 * being processed, its work is considered complete and the work item can be
2764 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002765 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002766 * @warning
2767 * A submitted work item must not be modified until it has been processed
2768 * by the workqueue.
2769 *
2770 * @note Can be called by ISRs.
2771 *
2772 * @param work_q Address of workqueue.
2773 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002774 *
2775 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002776 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777 */
2778static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2779 struct k_work *work)
2780{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002781 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002782 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 }
2784}
2785
2786/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002787 * @brief Submit a work item to a user mode workqueue
2788 *
David B. Kinder06d78352018-12-17 14:32:40 -08002789 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002790 * memory allocation is made from the caller's resource pool which is freed
2791 * once the worker thread consumes the k_work item. The workqueue
2792 * thread must have memory access to the k_work item being submitted. The caller
2793 * must have permission granted on the work_q parameter's queue object.
2794 *
2795 * Otherwise this works the same as k_work_submit_to_queue().
2796 *
2797 * @note Can be called by ISRs.
2798 *
2799 * @param work_q Address of workqueue.
2800 * @param work Address of work item.
2801 *
2802 * @retval -EBUSY if the work item was already in some workqueue
2803 * @retval -ENOMEM if no memory for thread resource pool allocation
2804 * @retval 0 Success
2805 * @req K-WORK-001
2806 */
2807static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2808 struct k_work *work)
2809{
2810 int ret = -EBUSY;
2811
2812 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2813 ret = k_queue_alloc_append(&work_q->queue, work);
2814
2815 /* Couldn't insert into the queue. Clear the pending bit
2816 * so the work item can be submitted again
2817 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002818 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002819 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2820 }
2821 }
2822
2823 return ret;
2824}
2825
2826/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002827 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002829 * This routine indicates if work item @a work is pending in a workqueue's
2830 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002831 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002832 * @note Can be called by ISRs.
2833 *
2834 * @param work Address of work item.
2835 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002836 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002837 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002838 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002839static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002840{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002841 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002842}
2843
2844/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002845 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002847 * This routine starts workqueue @a work_q. The workqueue spawns its work
2848 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002849 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002850 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002851 * @param stack Pointer to work queue thread's stack space, as defined by
2852 * K_THREAD_STACK_DEFINE()
2853 * @param stack_size Size of the work queue thread's stack (in bytes), which
2854 * should either be the same constant passed to
2855 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002856 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
2858 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002859 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860 */
Andrew Boie507852a2017-07-25 18:47:07 -07002861extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002862 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002863 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002866 * @brief Start a workqueue in user mode
2867 *
2868 * This works identically to k_work_q_start() except it is callable from user
2869 * mode, and the worker thread created will run in user mode.
2870 * The caller must have permissions granted on both the work_q parameter's
2871 * thread and queue objects, and the same restrictions on priority apply as
2872 * k_thread_create().
2873 *
2874 * @param work_q Address of workqueue.
2875 * @param stack Pointer to work queue thread's stack space, as defined by
2876 * K_THREAD_STACK_DEFINE()
2877 * @param stack_size Size of the work queue thread's stack (in bytes), which
2878 * should either be the same constant passed to
2879 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2880 * @param prio Priority of the work queue's thread.
2881 *
2882 * @return N/A
2883 * @req K-WORK-001
2884 */
2885extern void k_work_q_user_start(struct k_work_q *work_q,
2886 k_thread_stack_t *stack,
2887 size_t stack_size, int prio);
2888
2889/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002890 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002891 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002892 * This routine initializes a workqueue delayed work item, prior to
2893 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002894 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002895 * @param work Address of delayed work item.
2896 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002897 *
2898 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002899 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002901extern void k_delayed_work_init(struct k_delayed_work *work,
2902 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002903
2904/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002905 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002907 * This routine schedules work item @a work to be processed by workqueue
2908 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002909 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002910 * Only when the countdown completes is the work item actually submitted to
2911 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002913 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002914 * counting down cancels the existing submission and restarts the
2915 * countdown using the new delay. Note that this behavior is
2916 * inherently subject to race conditions with the pre-existing
2917 * timeouts and work queue, so care must be taken to synchronize such
2918 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002920 * @warning
2921 * A delayed work item must not be modified until it has been processed
2922 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002924 * @note Can be called by ISRs.
2925 *
2926 * @param work_q Address of workqueue.
2927 * @param work Address of delayed work item.
2928 * @param delay Delay before submitting the work item (in milliseconds).
2929 *
2930 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002931 * @retval -EINVAL Work item is being processed or has completed its work.
2932 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002933 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002935extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2936 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002937 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938
2939/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002940 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002942 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002943 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002944 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002946 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002948 * @note The result of calling this on a k_delayed_work item that has
2949 * not been submitted (i.e. before the return of the
2950 * k_delayed_work_submit_to_queue() call) is undefined.
2951 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002952 * @param work Address of delayed work item.
2953 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002954 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002955 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002956 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002958extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002960/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961 * @brief Submit a work item to the system workqueue.
2962 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002963 * This routine submits work item @a work to be processed by the system
2964 * workqueue. If the work item is already pending in the workqueue's queue
2965 * as a result of an earlier submission, this routine has no effect on the
2966 * work item. If the work item has already been processed, or is currently
2967 * being processed, its work is considered complete and the work item can be
2968 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002970 * @warning
2971 * Work items submitted to the system workqueue should avoid using handlers
2972 * that block or yield since this may prevent the system workqueue from
2973 * processing other work items in a timely manner.
2974 *
2975 * @note Can be called by ISRs.
2976 *
2977 * @param work Address of work item.
2978 *
2979 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002980 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981 */
2982static inline void k_work_submit(struct k_work *work)
2983{
2984 k_work_submit_to_queue(&k_sys_work_q, work);
2985}
2986
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002987/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988 * @brief Submit a delayed work item to the system workqueue.
2989 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002990 * This routine schedules work item @a work to be processed by the system
2991 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002992 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002993 * Only when the countdown completes is the work item actually submitted to
2994 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002996 * Submitting a previously submitted delayed work item that is still
2997 * counting down cancels the existing submission and restarts the countdown
2998 * using the new delay. If the work item is currently pending on the
2999 * workqueue's queue because the countdown has completed it is too late to
3000 * resubmit the item, and resubmission fails without impacting the work item.
3001 * If the work item has already been processed, or is currently being processed,
3002 * its work is considered complete and the work item can be resubmitted.
3003 *
3004 * @warning
3005 * Work items submitted to the system workqueue should avoid using handlers
3006 * that block or yield since this may prevent the system workqueue from
3007 * processing other work items in a timely manner.
3008 *
3009 * @note Can be called by ISRs.
3010 *
3011 * @param work Address of delayed work item.
3012 * @param delay Delay before submitting the work item (in milliseconds).
3013 *
3014 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003015 * @retval -EINVAL Work item is being processed or has completed its work.
3016 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003017 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018 */
3019static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003020 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003022 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023}
3024
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003026 * @brief Get time remaining before a delayed work gets scheduled.
3027 *
3028 * This routine computes the (approximate) time remaining before a
3029 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003030 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003031 *
3032 * @param work Delayed work item.
3033 *
3034 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003035 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003036 */
Kumar Galacc334c72017-04-21 10:55:34 -05003037static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003038{
Andy Ross52e444b2018-09-28 09:06:37 -07003039 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003040}
3041
Anas Nashif166f5192018-02-25 08:02:36 -06003042/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003043/**
Anas Nashifce78d162018-05-24 12:43:11 -05003044 * @defgroup mutex_apis Mutex APIs
3045 * @ingroup kernel_apis
3046 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003047 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048
Anas Nashifce78d162018-05-24 12:43:11 -05003049/**
3050 * Mutex Structure
3051 * @ingroup mutex_apis
3052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053struct k_mutex {
3054 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003055 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003056 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003057 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003060 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061};
3062
Anas Nashifce78d162018-05-24 12:43:11 -05003063/**
3064 * @cond INTERNAL_HIDDEN
3065 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003066#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003068 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069 .owner = NULL, \
3070 .lock_count = 0, \
3071 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003072 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 }
3074
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003075#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
3076
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003077/**
Allan Stephensc98da842016-11-11 15:45:03 -05003078 * INTERNAL_HIDDEN @endcond
3079 */
3080
3081/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003082 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003084 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003085 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003086 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003088 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003089 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003092 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003093 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003095/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * Upon completion, the mutex is available and does not have an owner.
3101 *
3102 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003103 *
3104 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003105 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003106 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003107__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003108
3109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * This routine locks @a mutex. If the mutex is locked by another thread,
3113 * the calling thread waits until the mutex becomes available or until
3114 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003116 * A thread is permitted to lock a mutex it has already locked. The operation
3117 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * @param mutex Address of the mutex.
3120 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121 * or one of the special values K_NO_WAIT and K_FOREVER.
3122 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003123 * @retval 0 Mutex locked.
3124 * @retval -EBUSY Returned without waiting.
3125 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003126 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003127 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003128__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003129
3130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * This routine unlocks @a mutex. The mutex must already be locked by the
3134 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003135 *
3136 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138 * thread.
3139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003141 *
3142 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003143 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003144 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003145__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003146
Allan Stephensc98da842016-11-11 15:45:03 -05003147/**
Anas Nashif166f5192018-02-25 08:02:36 -06003148 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003149 */
3150
3151/**
3152 * @cond INTERNAL_HIDDEN
3153 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154
3155struct k_sem {
3156 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303157 u32_t count;
3158 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003159 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003160
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003161 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162};
3163
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003164#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003165 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003166 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003167 .count = initial_count, \
3168 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003169 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003170 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003171 }
3172
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003173#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003174
Allan Stephensc98da842016-11-11 15:45:03 -05003175/**
3176 * INTERNAL_HIDDEN @endcond
3177 */
3178
3179/**
3180 * @defgroup semaphore_apis Semaphore APIs
3181 * @ingroup kernel_apis
3182 * @{
3183 */
3184
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003185/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * @param sem Address of the semaphore.
3191 * @param initial_count Initial semaphore count.
3192 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003193 *
3194 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003195 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003196 */
Andrew Boie99280232017-09-29 14:17:47 -07003197__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3198 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003199
3200/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3206 *
3207 * @param sem Address of the semaphore.
3208 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003209 * or one of the special values K_NO_WAIT and K_FOREVER.
3210 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003211 * @note When porting code from the nanokernel legacy API to the new API, be
3212 * careful with the return value of this function. The return value is the
3213 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3214 * non-zero means failure, while the nano_sem_take family returns 1 for success
3215 * and 0 for failure.
3216 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003217 * @retval 0 Semaphore taken.
3218 * @retval -EBUSY Returned without waiting.
3219 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003220 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003221 */
Andrew Boie99280232017-09-29 14:17:47 -07003222__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003223
3224/**
3225 * @brief Give a semaphore.
3226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * This routine gives @a sem, unless the semaphore is already at its maximum
3228 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003230 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003233 *
3234 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003235 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003236 */
Andrew Boie99280232017-09-29 14:17:47 -07003237__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003239/**
3240 * @brief Reset a semaphore's count to zero.
3241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003245 *
3246 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003247 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003248 */
Andrew Boie990bf162017-10-03 12:36:49 -07003249__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003250
Anas Nashif954d5502018-02-25 08:37:28 -06003251/**
3252 * @internal
3253 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003254static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003255{
Patrik Flykt24d71432019-03-26 19:57:45 -06003256 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003257}
3258
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003259/**
3260 * @brief Get a semaphore's count.
3261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003267 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003268 */
Andrew Boie990bf162017-10-03 12:36:49 -07003269__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003270
Anas Nashif954d5502018-02-25 08:37:28 -06003271/**
3272 * @internal
3273 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003274static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275{
3276 return sem->count;
3277}
3278
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003279/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003283 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003284 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * @param name Name of the semaphore.
3287 * @param initial_count Initial semaphore count.
3288 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003289 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003292 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003293 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303294 BUILD_ASSERT(((count_limit) != 0) && \
3295 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296
Anas Nashif166f5192018-02-25 08:02:36 -06003297/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003298
3299/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003300 * @defgroup msgq_apis Message Queue APIs
3301 * @ingroup kernel_apis
3302 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003303 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003305/**
3306 * @brief Message Queue Structure
3307 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308struct k_msgq {
3309 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003310 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003311 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003312 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313 char *buffer_start;
3314 char *buffer_end;
3315 char *read_ptr;
3316 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003317 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003319 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003320 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003321};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003322/**
3323 * @cond INTERNAL_HIDDEN
3324 */
3325
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003327#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003329 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003330 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003331 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003333 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 .read_ptr = q_buffer, \
3335 .write_ptr = q_buffer, \
3336 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003337 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003338 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003339#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003340/**
3341 * INTERNAL_HIDDEN @endcond
3342 */
3343
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003344
Andrew Boie0fe789f2018-04-12 18:35:56 -07003345#define K_MSGQ_FLAG_ALLOC BIT(0)
3346
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003347/**
3348 * @brief Message Queue Attributes
3349 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303350struct k_msgq_attrs {
3351 size_t msg_size;
3352 u32_t max_msgs;
3353 u32_t used_msgs;
3354};
3355
Allan Stephensc98da842016-11-11 15:45:03 -05003356
3357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3361 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003362 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3363 * message is similarly aligned to this boundary, @a q_msg_size must also be
3364 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * The message queue can be accessed outside the module where it is defined
3367 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003368 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003369 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @param q_name Name of the message queue.
3372 * @param q_msg_size Message size (in bytes).
3373 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003374 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003375 *
3376 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003377 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003378#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3379 static char __noinit __aligned(q_align) \
3380 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3381 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3382 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003383 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003384
Peter Mitsisd7a37502016-10-13 11:37:40 -04003385/**
3386 * @brief Initialize a message queue.
3387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * This routine initializes a message queue object, prior to its first use.
3389 *
Allan Stephensda827222016-11-09 14:23:58 -06003390 * The message queue's ring buffer must contain space for @a max_msgs messages,
3391 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3392 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3393 * that each message is similarly aligned to this boundary, @a q_msg_size
3394 * must also be a multiple of N.
3395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * @param q Address of the message queue.
3397 * @param buffer Pointer to ring buffer that holds queued messages.
3398 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003399 * @param max_msgs Maximum number of messages that can be queued.
3400 *
3401 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003402 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003403 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003404void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3405 u32_t max_msgs);
3406
3407/**
3408 * @brief Initialize a message queue.
3409 *
3410 * This routine initializes a message queue object, prior to its first use,
3411 * allocating its internal ring buffer from the calling thread's resource
3412 * pool.
3413 *
3414 * Memory allocated for the ring buffer can be released by calling
3415 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3416 * all of its references.
3417 *
3418 * @param q Address of the message queue.
3419 * @param msg_size Message size (in bytes).
3420 * @param max_msgs Maximum number of messages that can be queued.
3421 *
3422 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3423 * thread's resource pool, or -EINVAL if the size parameters cause
3424 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003425 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003426 */
3427__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3428 u32_t max_msgs);
3429
3430
3431void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003432
3433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003437 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003438 * @note Can be called by ISRs.
3439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * @param q Address of the message queue.
3441 * @param data Pointer to the message.
3442 * @param timeout Waiting period to add the message (in milliseconds),
3443 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003444 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003445 * @retval 0 Message sent.
3446 * @retval -ENOMSG Returned without waiting or queue purged.
3447 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003448 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003449 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003450__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003451
3452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * This routine receives a message from message queue @a q in a "first in,
3456 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003457 *
Allan Stephensc98da842016-11-11 15:45:03 -05003458 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @param q Address of the message queue.
3461 * @param data Address of area to hold the received message.
3462 * @param timeout Waiting period to receive the message (in milliseconds),
3463 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003464 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003465 * @retval 0 Message received.
3466 * @retval -ENOMSG Returned without waiting.
3467 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003468 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003469 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003470__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003471
3472/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003473 * @brief Peek/read a message from a message queue.
3474 *
3475 * This routine reads a message from message queue @a q in a "first in,
3476 * first out" manner and leaves the message in the queue.
3477 *
3478 * @note Can be called by ISRs.
3479 *
3480 * @param q Address of the message queue.
3481 * @param data Address of area to hold the message read from the queue.
3482 *
3483 * @retval 0 Message read.
3484 * @retval -ENOMSG Returned when the queue has no message.
3485 * @req K-MSGQ-002
3486 */
3487__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3488
3489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003492 * This routine discards all unreceived messages in a message queue's ring
3493 * buffer. Any threads that are blocked waiting to send a message to the
3494 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003497 *
3498 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003499 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003500 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003501__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502
Peter Mitsis67be2492016-10-07 11:44:34 -04003503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003504 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * This routine returns the number of unused entries in a message queue's
3507 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * @param q Address of the message queue.
3510 *
3511 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003512 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003513 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003514__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3515
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303516/**
3517 * @brief Get basic attributes of a message queue.
3518 *
3519 * This routine fetches basic attributes of message queue into attr argument.
3520 *
3521 * @param q Address of the message queue.
3522 * @param attrs pointer to message queue attribute structure.
3523 *
3524 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003525 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303526 */
3527__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3528
3529
Patrik Flykt4344e272019-03-08 14:19:05 -07003530static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003531{
3532 return q->max_msgs - q->used_msgs;
3533}
3534
Peter Mitsisd7a37502016-10-13 11:37:40 -04003535/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003540 * @param q Address of the message queue.
3541 *
3542 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003543 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003544 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003545__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3546
Patrik Flykt4344e272019-03-08 14:19:05 -07003547static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003548{
3549 return q->used_msgs;
3550}
3551
Anas Nashif166f5192018-02-25 08:02:36 -06003552/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003553
3554/**
3555 * @defgroup mem_pool_apis Memory Pool APIs
3556 * @ingroup kernel_apis
3557 * @{
3558 */
3559
Andy Ross73cb9582017-05-09 10:42:39 -07003560/* Note on sizing: the use of a 20 bit field for block means that,
3561 * assuming a reasonable minimum block size of 16 bytes, we're limited
3562 * to 16M of memory managed by a single pool. Long term it would be
3563 * good to move to a variable bit size based on configuration.
3564 */
3565struct k_mem_block_id {
3566 u32_t pool : 8;
3567 u32_t level : 4;
3568 u32_t block : 20;
3569};
3570
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003571struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003572 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003573 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003574};
3575
Anas Nashif166f5192018-02-25 08:02:36 -06003576/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003577
3578/**
3579 * @defgroup mailbox_apis Mailbox APIs
3580 * @ingroup kernel_apis
3581 * @{
3582 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003583
3584struct k_mbox_msg {
3585 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003586 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003587 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003588 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003589 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003590 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003591 /** sender's message data buffer */
3592 void *tx_data;
3593 /** internal use only - needed for legacy API support */
3594 void *_rx_data;
3595 /** message data block descriptor */
3596 struct k_mem_block tx_block;
3597 /** source thread id */
3598 k_tid_t rx_source_thread;
3599 /** target thread id */
3600 k_tid_t tx_target_thread;
3601 /** internal use only - thread waiting on send (may be a dummy) */
3602 k_tid_t _syncing_thread;
3603#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3604 /** internal use only - semaphore used during asynchronous send */
3605 struct k_sem *_async_sem;
3606#endif
3607};
3608
3609struct k_mbox {
3610 _wait_q_t tx_msg_queue;
3611 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003612 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003613
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003614 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003616/**
3617 * @cond INTERNAL_HIDDEN
3618 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003619
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003620#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003621 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003622 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3623 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003624 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003625 }
3626
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003627#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3628
Peter Mitsis12092702016-10-14 12:57:23 -04003629/**
Allan Stephensc98da842016-11-11 15:45:03 -05003630 * INTERNAL_HIDDEN @endcond
3631 */
3632
3633/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003634 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003637 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003638 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003641 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003642 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003643#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003644 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003645 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003646
Peter Mitsis12092702016-10-14 12:57:23 -04003647/**
3648 * @brief Initialize a mailbox.
3649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * This routine initializes a mailbox object, prior to its first use.
3651 *
3652 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003653 *
3654 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003655 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003656 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003657extern void k_mbox_init(struct k_mbox *mbox);
3658
Peter Mitsis12092702016-10-14 12:57:23 -04003659/**
3660 * @brief Send a mailbox message in a synchronous manner.
3661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003662 * This routine sends a message to @a mbox and waits for a receiver to both
3663 * receive and process it. The message data may be in a buffer, in a memory
3664 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * @param mbox Address of the mailbox.
3667 * @param tx_msg Address of the transmit message descriptor.
3668 * @param timeout Waiting period for the message to be received (in
3669 * milliseconds), or one of the special values K_NO_WAIT
3670 * and K_FOREVER. Once the message has been received,
3671 * this routine waits as long as necessary for the message
3672 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003673 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003674 * @retval 0 Message sent.
3675 * @retval -ENOMSG Returned without waiting.
3676 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003677 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003678 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003679extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003680 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003681
Peter Mitsis12092702016-10-14 12:57:23 -04003682/**
3683 * @brief Send a mailbox message in an asynchronous manner.
3684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003685 * This routine sends a message to @a mbox without waiting for a receiver
3686 * to process it. The message data may be in a buffer, in a memory pool block,
3687 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3688 * will be given when the message has been both received and completely
3689 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @param mbox Address of the mailbox.
3692 * @param tx_msg Address of the transmit message descriptor.
3693 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003694 *
3695 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003696 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003697 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003698extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699 struct k_sem *sem);
3700
Peter Mitsis12092702016-10-14 12:57:23 -04003701/**
3702 * @brief Receive a mailbox message.
3703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003704 * This routine receives a message from @a mbox, then optionally retrieves
3705 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003707 * @param mbox Address of the mailbox.
3708 * @param rx_msg Address of the receive message descriptor.
3709 * @param buffer Address of the buffer to receive data, or NULL to defer data
3710 * retrieval and message disposal until later.
3711 * @param timeout Waiting period for a message to be received (in
3712 * milliseconds), or one of the special values K_NO_WAIT
3713 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003714 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003715 * @retval 0 Message received.
3716 * @retval -ENOMSG Returned without waiting.
3717 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003718 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003719 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003720extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003721 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003722
3723/**
3724 * @brief Retrieve mailbox message data into a buffer.
3725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003726 * This routine completes the processing of a received message by retrieving
3727 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003728 *
3729 * Alternatively, this routine can be used to dispose of a received message
3730 * without retrieving its data.
3731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003732 * @param rx_msg Address of the receive message descriptor.
3733 * @param buffer Address of the buffer to receive data, or NULL to discard
3734 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003735 *
3736 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003737 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003738 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003739extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003740
3741/**
3742 * @brief Retrieve mailbox message data into a memory pool block.
3743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003744 * This routine completes the processing of a received message by retrieving
3745 * its data into a memory pool block, then disposing of the message.
3746 * The memory pool block that results from successful retrieval must be
3747 * returned to the pool once the data has been processed, even in cases
3748 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003749 *
3750 * Alternatively, this routine can be used to dispose of a received message
3751 * without retrieving its data. In this case there is no need to return a
3752 * memory pool block to the pool.
3753 *
3754 * This routine allocates a new memory pool block for the data only if the
3755 * data is not already in one. If a new block cannot be allocated, the routine
3756 * returns a failure code and the received message is left unchanged. This
3757 * permits the caller to reattempt data retrieval at a later time or to dispose
3758 * of the received message without retrieving its data.
3759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003760 * @param rx_msg Address of a receive message descriptor.
3761 * @param pool Address of memory pool, or NULL to discard data.
3762 * @param block Address of the area to hold memory pool block info.
3763 * @param timeout Waiting period to wait for a memory pool block (in
3764 * milliseconds), or one of the special values K_NO_WAIT
3765 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003766 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003767 * @retval 0 Data retrieved.
3768 * @retval -ENOMEM Returned without waiting.
3769 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003770 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003771 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003772extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003773 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003774 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003775
Anas Nashif166f5192018-02-25 08:02:36 -06003776/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003777
3778/**
Anas Nashifce78d162018-05-24 12:43:11 -05003779 * @defgroup pipe_apis Pipe APIs
3780 * @ingroup kernel_apis
3781 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003782 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003783
Anas Nashifce78d162018-05-24 12:43:11 -05003784/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003786 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3787 size_t size; /**< Buffer size */
3788 size_t bytes_used; /**< # bytes used in buffer */
3789 size_t read_index; /**< Where in buffer to read from */
3790 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003791 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792
3793 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003794 _wait_q_t readers; /**< Reader wait queue */
3795 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003796 } wait_q;
3797
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003798 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003799 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003800};
3801
Anas Nashifce78d162018-05-24 12:43:11 -05003802/**
3803 * @cond INTERNAL_HIDDEN
3804 */
3805#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3806
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003807#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3808 { \
3809 .buffer = pipe_buffer, \
3810 .size = pipe_buffer_size, \
3811 .bytes_used = 0, \
3812 .read_index = 0, \
3813 .write_index = 0, \
3814 .lock = {}, \
3815 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003816 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3817 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003818 }, \
3819 _OBJECT_TRACING_INIT \
3820 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 }
3822
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003823#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3824
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003825/**
Allan Stephensc98da842016-11-11 15:45:03 -05003826 * INTERNAL_HIDDEN @endcond
3827 */
3828
3829/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003830 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003832 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003833 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003834 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003836 * @param name Name of the pipe.
3837 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3838 * or zero if no ring buffer is used.
3839 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003840 *
3841 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003842 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003843#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003844 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003845 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003846 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003847 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003848
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003850 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003852 * This routine initializes a pipe object, prior to its first use.
3853 *
3854 * @param pipe Address of the pipe.
3855 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3856 * is used.
3857 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3858 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003859 *
3860 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003861 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003862 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003863void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3864
3865/**
3866 * @brief Release a pipe's allocated buffer
3867 *
3868 * If a pipe object was given a dynamically allocated buffer via
3869 * k_pipe_alloc_init(), this will free it. This function does nothing
3870 * if the buffer wasn't dynamically allocated.
3871 *
3872 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003873 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003874 */
3875void k_pipe_cleanup(struct k_pipe *pipe);
3876
3877/**
3878 * @brief Initialize a pipe and allocate a buffer for it
3879 *
3880 * Storage for the buffer region will be allocated from the calling thread's
3881 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3882 * or userspace is enabled and the pipe object loses all references to it.
3883 *
3884 * This function should only be called on uninitialized pipe objects.
3885 *
3886 * @param pipe Address of the pipe.
3887 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3888 * buffer is used.
3889 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003890 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003891 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003892 */
3893__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003894
3895/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003896 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003897 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003898 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003900 * @param pipe Address of the pipe.
3901 * @param data Address of data to write.
3902 * @param bytes_to_write Size of data (in bytes).
3903 * @param bytes_written Address of area to hold the number of bytes written.
3904 * @param min_xfer Minimum number of bytes to write.
3905 * @param timeout Waiting period to wait for the data to be written (in
3906 * milliseconds), or one of the special values K_NO_WAIT
3907 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003908 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003909 * @retval 0 At least @a min_xfer bytes of data were written.
3910 * @retval -EIO Returned without waiting; zero data bytes were written.
3911 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003913 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003914 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003915__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3916 size_t bytes_to_write, size_t *bytes_written,
3917 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003918
3919/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003920 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * @param pipe Address of the pipe.
3925 * @param data Address to place the data read from pipe.
3926 * @param bytes_to_read Maximum number of data bytes to read.
3927 * @param bytes_read Address of area to hold the number of bytes read.
3928 * @param min_xfer Minimum number of data bytes to read.
3929 * @param timeout Waiting period to wait for the data to be read (in
3930 * milliseconds), or one of the special values K_NO_WAIT
3931 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003932 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003933 * @retval 0 At least @a min_xfer bytes of data were read.
3934 * @retval -EIO Returned without waiting; zero data bytes were read.
3935 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003937 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003939__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3940 size_t bytes_to_read, size_t *bytes_read,
3941 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003942
3943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003944 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * This routine writes the data contained in a memory block to @a pipe.
3947 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003948 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003950 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003951 * @param block Memory block containing data to send
3952 * @param size Number of data bytes in memory block to send
3953 * @param sem Semaphore to signal upon completion (else NULL)
3954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003956 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003957 */
3958extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3959 size_t size, struct k_sem *sem);
3960
Anas Nashif166f5192018-02-25 08:02:36 -06003961/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003962
Allan Stephensc98da842016-11-11 15:45:03 -05003963/**
3964 * @cond INTERNAL_HIDDEN
3965 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003967struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003968 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003969 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003970 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003971 char *buffer;
3972 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003973 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003975 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976};
3977
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003978#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003979 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003980 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003981 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003982 .num_blocks = slab_num_blocks, \
3983 .block_size = slab_block_size, \
3984 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003985 .free_list = NULL, \
3986 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003987 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003988 }
3989
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003990#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3991
3992
Peter Mitsis578f9112016-10-07 13:50:31 -04003993/**
Allan Stephensc98da842016-11-11 15:45:03 -05003994 * INTERNAL_HIDDEN @endcond
3995 */
3996
3997/**
3998 * @defgroup mem_slab_apis Memory Slab APIs
3999 * @ingroup kernel_apis
4000 * @{
4001 */
4002
4003/**
Allan Stephensda827222016-11-09 14:23:58 -06004004 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004005 *
Allan Stephensda827222016-11-09 14:23:58 -06004006 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004007 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004008 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4009 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004010 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004011 *
Allan Stephensda827222016-11-09 14:23:58 -06004012 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004013 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004014 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004015 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004017 * @param name Name of the memory slab.
4018 * @param slab_block_size Size of each memory block (in bytes).
4019 * @param slab_num_blocks Number memory blocks.
4020 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004021 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004022 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004023#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004024 char __noinit __aligned(WB_UP(slab_align)) \
4025 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004026 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004027 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004028 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004029
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004030/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004031 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004033 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004034 *
Allan Stephensda827222016-11-09 14:23:58 -06004035 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4036 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004037 * N-byte boundary matching a word boundary, where N is a power of 2
4038 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004039 * To ensure that each memory block is similarly aligned to this boundary,
4040 * @a slab_block_size must also be a multiple of N.
4041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004042 * @param slab Address of the memory slab.
4043 * @param buffer Pointer to buffer used for the memory blocks.
4044 * @param block_size Size of each memory block (in bytes).
4045 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004046 *
4047 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004048 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004049 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004050extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004051 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004052
4053/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004054 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004056 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004058 * @param slab Address of the memory slab.
4059 * @param mem Pointer to block address area.
4060 * @param timeout Maximum time to wait for operation to complete
4061 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4062 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004063 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004064 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004065 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004066 * @retval -ENOMEM Returned without waiting.
4067 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004068 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004069 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004070extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004071 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004072
4073/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004074 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004076 * This routine releases a previously allocated memory block back to its
4077 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004079 * @param slab Address of the memory slab.
4080 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004081 *
4082 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004083 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004084 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004085extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004086
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004087/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004088 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004090 * This routine gets the number of memory blocks that are currently
4091 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004093 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004095 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004096 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004097 */
Kumar Galacc334c72017-04-21 10:55:34 -05004098static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004099{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004100 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004101}
4102
Peter Mitsisc001aa82016-10-13 13:53:37 -04004103/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004104 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004106 * This routine gets the number of memory blocks that are currently
4107 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004109 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004111 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004112 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004113 */
Kumar Galacc334c72017-04-21 10:55:34 -05004114static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004115{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004116 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004117}
4118
Anas Nashif166f5192018-02-25 08:02:36 -06004119/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004120
4121/**
4122 * @cond INTERNAL_HIDDEN
4123 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004124
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004125struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004126 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004127 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004128};
4129
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004130/**
Allan Stephensc98da842016-11-11 15:45:03 -05004131 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004132 */
4133
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004134/**
Allan Stephensc98da842016-11-11 15:45:03 -05004135 * @addtogroup mem_pool_apis
4136 * @{
4137 */
4138
4139/**
4140 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004142 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4143 * long. The memory pool allows blocks to be repeatedly partitioned into
4144 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004145 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004146 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004147 * If the pool is to be accessed outside the module where it is defined, it
4148 * can be declared via
4149 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004150 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004152 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004153 * @param minsz Size of the smallest blocks in the pool (in bytes).
4154 * @param maxsz Size of the largest blocks in the pool (in bytes).
4155 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004156 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004157 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004158 */
Andy Ross73cb9582017-05-09 10:42:39 -07004159#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitreace11bb2019-05-26 21:12:05 -04004160 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK); \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004161 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004162 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004163 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004164 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004165 .base = { \
4166 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004167 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004168 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004169 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004170 .levels = _mpool_lvls_##name, \
4171 .flags = SYS_MEM_POOL_KERNEL \
4172 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004173 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004174
Peter Mitsis937042c2016-10-13 13:18:26 -04004175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004176 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004178 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004180 * @param pool Address of the memory pool.
4181 * @param block Pointer to block descriptor for the allocated memory.
4182 * @param size Amount of memory to allocate (in bytes).
4183 * @param timeout Maximum time to wait for operation to complete
4184 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4185 * or K_FOREVER to wait as long as necessary.
4186 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004187 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004188 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004189 * @retval -ENOMEM Returned without waiting.
4190 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004191 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004192 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004193extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004194 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004195
4196/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004197 * @brief Allocate memory from a memory pool with malloc() semantics
4198 *
4199 * Such memory must be released using k_free().
4200 *
4201 * @param pool Address of the memory pool.
4202 * @param size Amount of memory to allocate (in bytes).
4203 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004204 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004205 */
4206extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4207
4208/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004209 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004211 * This routine releases a previously allocated memory block back to its
4212 * memory pool.
4213 *
4214 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004215 *
4216 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004217 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004218 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004219extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004220
4221/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004222 * @brief Free memory allocated from a memory pool.
4223 *
4224 * This routine releases a previously allocated memory block back to its
4225 * memory pool
4226 *
4227 * @param id Memory block identifier.
4228 *
4229 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004230 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004231 */
4232extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4233
4234/**
Anas Nashif166f5192018-02-25 08:02:36 -06004235 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004236 */
4237
4238/**
4239 * @defgroup heap_apis Heap Memory Pool APIs
4240 * @ingroup kernel_apis
4241 * @{
4242 */
4243
4244/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004245 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004247 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004248 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004250 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004252 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004253 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004254 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004255extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004256
4257/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004258 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004259 *
4260 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004261 * returned must have been allocated from the heap memory pool or
4262 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004263 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004264 * If @a ptr is NULL, no operation is performed.
4265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004266 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004267 *
4268 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004269 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004270 */
4271extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004272
Allan Stephensc98da842016-11-11 15:45:03 -05004273/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004274 * @brief Allocate memory from heap, array style
4275 *
4276 * This routine provides traditional calloc() semantics. Memory is
4277 * allocated from the heap memory pool and zeroed.
4278 *
4279 * @param nmemb Number of elements in the requested array
4280 * @param size Size of each array element (in bytes).
4281 *
4282 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004283 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004284 */
4285extern void *k_calloc(size_t nmemb, size_t size);
4286
Anas Nashif166f5192018-02-25 08:02:36 -06004287/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004288
Benjamin Walshacc68c12017-01-29 18:57:45 -05004289/* polling API - PRIVATE */
4290
Benjamin Walshb0179862017-02-02 16:39:57 -05004291#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004292#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004293#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004294#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004295#endif
4296
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297/* private - implementation data created as needed, per-type */
4298struct _poller {
4299 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004300 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004301};
4302
4303/* private - types bit positions */
4304enum _poll_types_bits {
4305 /* can be used to ignore an event */
4306 _POLL_TYPE_IGNORE,
4307
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004308 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004309 _POLL_TYPE_SIGNAL,
4310
4311 /* semaphore availability */
4312 _POLL_TYPE_SEM_AVAILABLE,
4313
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004314 /* queue/fifo/lifo data availability */
4315 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004316
4317 _POLL_NUM_TYPES
4318};
4319
Patrik Flykt4344e272019-03-08 14:19:05 -07004320#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004321
4322/* private - states bit positions */
4323enum _poll_states_bits {
4324 /* default state when creating event */
4325 _POLL_STATE_NOT_READY,
4326
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004327 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004328 _POLL_STATE_SIGNALED,
4329
4330 /* semaphore is available */
4331 _POLL_STATE_SEM_AVAILABLE,
4332
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004333 /* data is available to read on queue/fifo/lifo */
4334 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004335
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004336 /* queue/fifo/lifo wait was cancelled */
4337 _POLL_STATE_CANCELLED,
4338
Benjamin Walshacc68c12017-01-29 18:57:45 -05004339 _POLL_NUM_STATES
4340};
4341
Patrik Flykt4344e272019-03-08 14:19:05 -07004342#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004343
4344#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004345 (32 - (0 \
4346 + 8 /* tag */ \
4347 + _POLL_NUM_TYPES \
4348 + _POLL_NUM_STATES \
4349 + 1 /* modes */ \
4350 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004351
Benjamin Walshacc68c12017-01-29 18:57:45 -05004352/* end of polling API - PRIVATE */
4353
4354
4355/**
4356 * @defgroup poll_apis Async polling APIs
4357 * @ingroup kernel_apis
4358 * @{
4359 */
4360
4361/* Public polling API */
4362
4363/* public - values for k_poll_event.type bitfield */
4364#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004365#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4366#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4367#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004368#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004369
4370/* public - polling modes */
4371enum k_poll_modes {
4372 /* polling thread does not take ownership of objects when available */
4373 K_POLL_MODE_NOTIFY_ONLY = 0,
4374
4375 K_POLL_NUM_MODES
4376};
4377
4378/* public - values for k_poll_event.state bitfield */
4379#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004380#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4381#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4382#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004383#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004384#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004385
4386/* public - poll signal object */
4387struct k_poll_signal {
4388 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004389 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004390
4391 /*
4392 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4393 * user resets it to 0.
4394 */
4395 unsigned int signaled;
4396
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004397 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004398 int result;
4399};
4400
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004401#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004402 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004403 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004404 .signaled = 0, \
4405 .result = 0, \
4406 }
4407
4408struct k_poll_event {
4409 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004410 sys_dnode_t _node;
4411
4412 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004413 struct _poller *poller;
4414
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004415 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004416 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004417
Benjamin Walshacc68c12017-01-29 18:57:45 -05004418 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004419 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004420
4421 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004422 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004423
4424 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004425 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004426
4427 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004428 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004429
4430 /* per-type data */
4431 union {
4432 void *obj;
4433 struct k_poll_signal *signal;
4434 struct k_sem *sem;
4435 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004436 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004437 };
4438};
4439
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004440#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004441 { \
4442 .poller = NULL, \
4443 .type = event_type, \
4444 .state = K_POLL_STATE_NOT_READY, \
4445 .mode = event_mode, \
4446 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004447 { .obj = event_obj }, \
4448 }
4449
4450#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4451 event_tag) \
4452 { \
4453 .type = event_type, \
4454 .tag = event_tag, \
4455 .state = K_POLL_STATE_NOT_READY, \
4456 .mode = event_mode, \
4457 .unused = 0, \
4458 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004459 }
4460
4461/**
4462 * @brief Initialize one struct k_poll_event instance
4463 *
4464 * After this routine is called on a poll event, the event it ready to be
4465 * placed in an event array to be passed to k_poll().
4466 *
4467 * @param event The event to initialize.
4468 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4469 * values. Only values that apply to the same object being polled
4470 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4471 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004472 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004473 * @param obj Kernel object or poll signal.
4474 *
4475 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004476 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004477 */
4478
Kumar Galacc334c72017-04-21 10:55:34 -05004479extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004480 int mode, void *obj);
4481
4482/**
4483 * @brief Wait for one or many of multiple poll events to occur
4484 *
4485 * This routine allows a thread to wait concurrently for one or many of
4486 * multiple poll events to have occurred. Such events can be a kernel object
4487 * being available, like a semaphore, or a poll signal event.
4488 *
4489 * When an event notifies that a kernel object is available, the kernel object
4490 * is not "given" to the thread calling k_poll(): it merely signals the fact
4491 * that the object was available when the k_poll() call was in effect. Also,
4492 * all threads trying to acquire an object the regular way, i.e. by pending on
4493 * the object, have precedence over the thread polling on the object. This
4494 * means that the polling thread will never get the poll event on an object
4495 * until the object becomes available and its pend queue is empty. For this
4496 * reason, the k_poll() call is more effective when the objects being polled
4497 * only have one thread, the polling thread, trying to acquire them.
4498 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004499 * When k_poll() returns 0, the caller should loop on all the events that were
4500 * passed to k_poll() and check the state field for the values that were
4501 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004502 *
4503 * Before being reused for another call to k_poll(), the user has to reset the
4504 * state field to K_POLL_STATE_NOT_READY.
4505 *
Andrew Boie3772f772018-05-07 16:52:57 -07004506 * When called from user mode, a temporary memory allocation is required from
4507 * the caller's resource pool.
4508 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004509 * @param events An array of pointers to events to be polled for.
4510 * @param num_events The number of events in the array.
4511 * @param timeout Waiting period for an event to be ready (in milliseconds),
4512 * or one of the special values K_NO_WAIT and K_FOREVER.
4513 *
4514 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004515 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004516 * @retval -EINTR Polling has been interrupted, e.g. with
4517 * k_queue_cancel_wait(). All output events are still set and valid,
4518 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4519 * words, -EINTR status means that at least one of output events is
4520 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004521 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4522 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004523 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004524 */
4525
Andrew Boie3772f772018-05-07 16:52:57 -07004526__syscall int k_poll(struct k_poll_event *events, int num_events,
4527 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004528
4529/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004530 * @brief Initialize a poll signal object.
4531 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004532 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004533 *
4534 * @param signal A poll signal.
4535 *
4536 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004537 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004538 */
4539
Andrew Boie3772f772018-05-07 16:52:57 -07004540__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4541
4542/*
4543 * @brief Reset a poll signal object's state to unsignaled.
4544 *
4545 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004546 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004547 */
4548__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4549
Patrik Flykt4344e272019-03-08 14:19:05 -07004550static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004551{
Patrik Flykt24d71432019-03-26 19:57:45 -06004552 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004553}
4554
4555/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004556 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004557 *
4558 * @param signal A poll signal object
4559 * @param signaled An integer buffer which will be written nonzero if the
4560 * object was signaled
4561 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004562 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004563 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004564 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004565 */
4566__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4567 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004568
4569/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004570 * @brief Signal a poll signal object.
4571 *
4572 * This routine makes ready a poll signal, which is basically a poll event of
4573 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4574 * made ready to run. A @a result value can be specified.
4575 *
4576 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004577 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004578 * k_poll_signal_reset(). It thus has to be reset by the user before being
4579 * passed again to k_poll() or k_poll() will consider it being signaled, and
4580 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004581 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004582 * @note The result is stored and the 'signaled' field is set even if
4583 * this function returns an error indicating that an expiring poll was
4584 * not notified. The next k_poll() will detect the missed raise.
4585 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004586 * @param signal A poll signal.
4587 * @param result The value to store in the result field of the signal.
4588 *
4589 * @retval 0 The signal was delivered successfully.
4590 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004591 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004592 */
4593
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004594__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004595
Anas Nashif954d5502018-02-25 08:37:28 -06004596/**
4597 * @internal
4598 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004599extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004600
Anas Nashif166f5192018-02-25 08:02:36 -06004601/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004602
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004603/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004604 * @defgroup cpu_idle_apis CPU Idling APIs
4605 * @ingroup kernel_apis
4606 * @{
4607 */
4608
4609/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004610 * @brief Make the CPU idle.
4611 *
4612 * This function makes the CPU idle until an event wakes it up.
4613 *
4614 * In a regular system, the idle thread should be the only thread responsible
4615 * for making the CPU idle and triggering any type of power management.
4616 * However, in some more constrained systems, such as a single-threaded system,
4617 * the only thread would be responsible for this if needed.
4618 *
4619 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004620 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004621 */
4622extern void k_cpu_idle(void);
4623
4624/**
4625 * @brief Make the CPU idle in an atomic fashion.
4626 *
4627 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4628 * must be done atomically before making the CPU idle.
4629 *
4630 * @param key Interrupt locking key obtained from irq_lock().
4631 *
4632 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004633 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004634 */
4635extern void k_cpu_atomic_idle(unsigned int key);
4636
Anas Nashif30c3cff2019-01-22 08:18:13 -05004637/**
4638 * @}
4639 */
Anas Nashif954d5502018-02-25 08:37:28 -06004640
4641/**
4642 * @internal
4643 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004644extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004645
Patrik Flykt4344e272019-03-08 14:19:05 -07004646#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004647/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004648#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004649#else
4650
Andrew Boiecdb94d62017-04-18 15:22:05 -07004651/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004652 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004653 *
4654 * We won't have a real exception frame to determine the PC value when
4655 * the oops occurred, so print file and line number before we jump into
4656 * the fatal error handler.
4657 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004658#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004659 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Patrik Flykt4344e272019-03-08 14:19:05 -07004660 z_NanoFatalErrorHandler(reason, &_default_esf); \
Andy Ross92ce7672019-05-31 13:12:15 -07004661 k_thread_abort(k_current_get()); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004662 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004663
4664#endif /* _ARCH__EXCEPT */
4665
4666/**
4667 * @brief Fatally terminate a thread
4668 *
4669 * This should be called when a thread has encountered an unrecoverable
4670 * runtime condition and needs to terminate. What this ultimately
4671 * means is determined by the _fatal_error_handler() implementation, which
4672 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4673 *
4674 * If this is called from ISR context, the default system fatal error handler
4675 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004676 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004677 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004678#define k_oops() z_except_reason(_NANO_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004679
4680/**
4681 * @brief Fatally terminate the system
4682 *
4683 * This should be called when the Zephyr kernel has encountered an
4684 * unrecoverable runtime condition and needs to terminate. What this ultimately
4685 * means is determined by the _fatal_error_handler() implementation, which
4686 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004687 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004688 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004689#define k_panic() z_except_reason(_NANO_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004690
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004691/*
4692 * private APIs that are utilized by one or more public APIs
4693 */
4694
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004695#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004696/**
4697 * @internal
4698 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004699extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004700#else
Anas Nashif954d5502018-02-25 08:37:28 -06004701/**
4702 * @internal
4703 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004704#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004705#endif
4706
Anas Nashif954d5502018-02-25 08:37:28 -06004707/**
4708 * @internal
4709 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004710extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004711/**
4712 * @internal
4713 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004714extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004715
Andrew Boiedc5d9352017-06-02 12:56:47 -07004716/* arch/cpu.h may declare an architecture or platform-specific macro
4717 * for properly declaring stacks, compatible with MMU/MPU constraints if
4718 * enabled
4719 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004720
4721/**
4722 * @brief Obtain an extern reference to a stack
4723 *
4724 * This macro properly brings the symbol of a thread stack declared
4725 * elsewhere into scope.
4726 *
4727 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004728 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004729 */
4730#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4731
Patrik Flykt4344e272019-03-08 14:19:05 -07004732#ifdef Z_ARCH_THREAD_STACK_DEFINE
4733#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004734#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004735 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4736#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4737#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4738#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004739#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004740static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004741{
Patrik Flykt4344e272019-03-08 14:19:05 -07004742 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004743}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004744#else
4745/**
4746 * @brief Declare a toplevel thread stack memory region
4747 *
4748 * This declares a region of memory suitable for use as a thread's stack.
4749 *
4750 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4751 * 'noinit' section so that it isn't zeroed at boot
4752 *
Andrew Boie507852a2017-07-25 18:47:07 -07004753 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004754 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004755 * inside needs to be examined, examine thread->stack_info for the associated
4756 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004757 *
4758 * It is legal to precede this definition with the 'static' keyword.
4759 *
4760 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4761 * parameter of k_thread_create(), it may not be the same as the
4762 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4763 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004764 * Some arches may round the size of the usable stack region up to satisfy
4765 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4766 * size.
4767 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004768 * @param sym Thread stack symbol name
4769 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004770 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004771 */
4772#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004773 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004774
4775/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304776 * @brief Calculate size of stacks to be allocated in a stack array
4777 *
4778 * This macro calculates the size to be allocated for the stacks
4779 * inside a stack array. It accepts the indicated "size" as a parameter
4780 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4781 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4782 *
4783 * @param size Size of the stack memory region
4784 * @req K-TSTACK-001
4785 */
4786#define K_THREAD_STACK_LEN(size) (size)
4787
4788/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004789 * @brief Declare a toplevel array of thread stack memory regions
4790 *
4791 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4792 * definition for additional details and constraints.
4793 *
4794 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4795 * 'noinit' section so that it isn't zeroed at boot
4796 *
4797 * @param sym Thread stack symbol name
4798 * @param nmemb Number of stacks to declare
4799 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004800 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004801 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004802#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004803 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304804 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004805
4806/**
4807 * @brief Declare an embedded stack memory region
4808 *
4809 * Used for stacks embedded within other data structures. Use is highly
4810 * discouraged but in some cases necessary. For memory protection scenarios,
4811 * it is very important that any RAM preceding this member not be writable
4812 * by threads else a stack overflow will lead to silent corruption. In other
4813 * words, the containing data structure should live in RAM owned by the kernel.
4814 *
4815 * @param sym Thread stack symbol name
4816 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004817 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004818 */
4819#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004820 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004821
4822/**
4823 * @brief Return the size in bytes of a stack memory region
4824 *
4825 * Convenience macro for passing the desired stack size to k_thread_create()
4826 * since the underlying implementation may actually create something larger
4827 * (for instance a guard area).
4828 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004829 * The value returned here is not guaranteed to match the 'size' parameter
4830 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004831 *
4832 * @param sym Stack memory symbol
4833 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004834 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004835 */
4836#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4837
Andrew Boie575abc02019-03-19 10:42:24 -07004838
4839/**
4840 * @brief Indicate how much additional memory is reserved for stack objects
4841 *
4842 * Any given stack declaration may have additional memory in it for guard
4843 * areas or supervisor mode stacks. This macro indicates how much space
4844 * is reserved for this. The memory reserved may not be contiguous within
4845 * the stack object, and does not account for additional space used due to
4846 * enforce alignment.
4847 */
4848#define K_THREAD_STACK_RESERVED 0
4849
Andrew Boiedc5d9352017-06-02 12:56:47 -07004850/**
4851 * @brief Get a pointer to the physical stack buffer
4852 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004853 * This macro is deprecated. If a stack buffer needs to be examined, the
4854 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004855 *
4856 * @param sym Declared stack symbol name
4857 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004858 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004859 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004860static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004861{
4862 return (char *)sym;
4863}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004864
4865#endif /* _ARCH_DECLARE_STACK */
4866
Chunlin Hane9c97022017-07-07 20:29:30 +08004867/**
4868 * @defgroup mem_domain_apis Memory domain APIs
4869 * @ingroup kernel_apis
4870 * @{
4871 */
4872
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004873/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004874 * @def K_MEM_PARTITION_DEFINE
4875 * @brief Used to declare a memory partition
4876 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004877 */
4878#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4879#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4880 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004881 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004882 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004883#else
4884#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004885 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004886 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004887#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4888
Chunlin Hane9c97022017-07-07 20:29:30 +08004889/* memory partition */
4890struct k_mem_partition {
4891 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004892 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004893 /* size of memory partition */
4894 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004895#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004896 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304897 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004898#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004899};
4900
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004901/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304902 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004903struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304904#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004905 /* partitions in the domain */
4906 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304907#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004908 /* domain q */
4909 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004910 /* number of partitions in the domain */
4911 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004912};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304913
Chunlin Hane9c97022017-07-07 20:29:30 +08004914
4915/**
4916 * @brief Initialize a memory domain.
4917 *
4918 * Initialize a memory domain with given name and memory partitions.
4919 *
4920 * @param domain The memory domain to be initialized.
4921 * @param num_parts The number of array items of "parts" parameter.
4922 * @param parts An array of pointers to the memory partitions. Can be NULL
4923 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004924 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004925 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004926extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004927 struct k_mem_partition *parts[]);
4928/**
4929 * @brief Destroy a memory domain.
4930 *
4931 * Destroy a memory domain.
4932 *
4933 * @param domain The memory domain to be destroyed.
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_destroy(struct k_mem_domain *domain);
4937
4938/**
4939 * @brief Add a memory partition into a memory domain.
4940 *
4941 * Add a memory partition into a memory domain.
4942 *
4943 * @param domain The memory domain to be added a memory partition.
4944 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004945 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004946 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004947extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4948 struct k_mem_partition *part);
4949
4950/**
4951 * @brief Remove a memory partition from a memory domain.
4952 *
4953 * Remove a memory partition from a memory domain.
4954 *
4955 * @param domain The memory domain to be removed a memory partition.
4956 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004957 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004958 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004959extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4960 struct k_mem_partition *part);
4961
4962/**
4963 * @brief Add a thread into a memory domain.
4964 *
4965 * Add a thread into a memory domain.
4966 *
4967 * @param domain The memory domain that the thread is going to be added into.
4968 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004969 *
4970 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004971 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004972extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4973 k_tid_t thread);
4974
4975/**
4976 * @brief Remove a thread from its memory domain.
4977 *
4978 * Remove a thread from its memory domain.
4979 *
4980 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004981 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004982 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004983extern void k_mem_domain_remove_thread(k_tid_t thread);
4984
Anas Nashif166f5192018-02-25 08:02:36 -06004985/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004986
Andrew Boie756f9072017-10-10 16:01:49 -07004987/**
4988 * @brief Emit a character buffer to the console device
4989 *
4990 * @param c String of characters to print
4991 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004992 *
4993 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004994 */
4995__syscall void k_str_out(char *c, size_t n);
4996
Andy Rosse7172672018-01-24 15:48:32 -08004997/**
4998 * @brief Start a numbered CPU on a MP-capable system
4999
5000 * This starts and initializes a specific CPU. The main thread on
5001 * startup is running on CPU zero, other processors are numbered
5002 * sequentially. On return from this function, the CPU is known to
5003 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07005004 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08005005 * with the provided key will work to enable them.
5006 *
5007 * Normally, in SMP mode this function will be called by the kernel
5008 * initialization and should not be used as a user API. But it is
5009 * defined here for special-purpose apps which want Zephyr running on
5010 * one core and to use others for design-specific processing.
5011 *
5012 * @param cpu_num Integer number of the CPU
5013 * @param stack Stack memory for the CPU
5014 * @param sz Stack buffer size, in bytes
5015 * @param fn Function to begin running on the CPU. First argument is
5016 * an irq_unlock() key.
5017 * @param arg Untyped argument to be passed to "fn"
5018 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005019extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08005020 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08005021
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005022
5023/**
5024 * @brief Disable preservation of floating point context information.
5025 *
5026 * This routine informs the kernel that the specified thread
5027 * will no longer be using the floating point registers.
5028 *
5029 * @warning
5030 * Some architectures apply restrictions on how the disabling of floating
5031 * point preservation may be requested, see z_arch_float_disable.
5032 *
5033 * @warning
5034 * This routine should only be used to disable floating point support for
5035 * a thread that currently has such support enabled.
5036 *
5037 * @param thread ID of thread.
5038 *
5039 * @retval 0 On success.
5040 * @retval -ENOSYS If the floating point disabling is not implemented.
5041 * -EINVAL If the floating point disabling could not be performed.
5042 */
5043__syscall int k_float_disable(struct k_thread *thread);
5044
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005045#ifdef __cplusplus
5046}
5047#endif
5048
Anas Nashif10291a02019-06-25 12:25:12 -04005049#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005050#include <syscalls/kernel.h>
5051
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005052#endif /* !_ASMLANGUAGE */
5053
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005054#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */