blob: cc646216325a762b8ea2566daba35e3c2279bc2c [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>
Stephanos Ioannidis33fbe002019-09-09 21:26:59 +090020#include <toolchain.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
Anas Nashifbbb157d2017-01-15 08:46:31 -050026/**
27 * @brief Kernel APIs
28 * @defgroup kernel_apis Kernel APIs
29 * @{
30 * @}
31 */
32
Anas Nashif61f4b242016-11-18 10:53:59 -050033#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
35#else
36#define K_DEBUG(fmt, ...)
37#endif
38
Benjamin Walsh2f280412017-01-14 19:23:46 -050039#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
40#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
41#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
42#elif defined(CONFIG_COOP_ENABLED)
43#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
44#define _NUM_PREEMPT_PRIO (0)
45#elif defined(CONFIG_PREEMPT_ENABLED)
46#define _NUM_COOP_PRIO (0)
47#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
48#else
49#error "invalid configuration"
50#endif
51
52#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_PRIO_PREEMPT(x) (x)
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#define K_ANY NULL
56#define K_END NULL
57
Benjamin Walshedb35702017-01-14 18:47:22 -050058#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050060#elif defined(CONFIG_COOP_ENABLED)
61#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
62#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050064#else
65#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#endif
67
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050068#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
70#else
71#define K_LOWEST_THREAD_PRIO -1
72#endif
73
Benjamin Walshfab8d922016-11-08 15:36:36 -050074#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
75
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
77#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
78
Anas Nashif2f203c22016-12-18 06:57:45 -050079#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -080080#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +080081#define _OBJECT_TRACING_LINKED_FLAG u8_t __linked;
82#define _OBJECT_TRACING_INIT \
83 .__next = NULL, \
84 .__linked = 0,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085#else
Anas Nashif2f203c22016-12-18 06:57:45 -050086#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -080087#define _OBJECT_TRACING_NEXT_PTR(type)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +080088#define _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040089#endif
90
Benjamin Walshacc68c12017-01-29 18:57:45 -050091#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +030092#define _POLL_EVENT_OBJ_INIT(obj) \
93 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
94#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -050095#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +030096#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -050097#define _POLL_EVENT
98#endif
99
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500100struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101struct k_mutex;
102struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103struct k_msgq;
104struct k_mbox;
105struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200106struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107struct k_fifo;
108struct k_lifo;
109struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400110struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mem_pool;
112struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500113struct k_poll_event;
114struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800115struct k_mem_domain;
116struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800117struct k_futex;
Andrew Boief2734ab2020-03-11 06:37:42 -0700118struct z_futex_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119
Anas Nashife71293e2019-12-04 20:00:14 -0500120/**
121 * @brief Kernel Object Types
122 *
123 * This enumeration needs to be kept in sync with the lists of kernel objects
Andrew Boie5bd891d2017-09-27 12:59:28 -0700124 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
125 * function in kernel/userspace.c
126 */
Andrew Boie945af952017-08-22 13:15:23 -0700127enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700128 K_OBJ_ANY,
129
Leandro Pereirac2003672018-04-04 13:50:32 -0700130 /** @cond
131 * Doxygen should ignore this build-time generated include file
132 * when genrating API documentation. Enumeration values are
133 * generated during build by gen_kobject_list.py. It includes
134 * basic kernel objects (e.g. pipes and mutexes) and driver types.
135 */
136#include <kobj-types-enum.h>
137 /** @endcond
138 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700139
Andrew Boie945af952017-08-22 13:15:23 -0700140 K_OBJ_LAST
141};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500142/**
143 * @defgroup usermode_apis User Mode APIs
144 * @ingroup kernel_apis
145 * @{
146 */
Andrew Boie945af952017-08-22 13:15:23 -0700147
148#ifdef CONFIG_USERSPACE
Andrew Boie28be7932020-03-11 10:56:19 -0700149#ifdef CONFIG_GEN_PRIV_STACKS
150/* Metadata struct for K_OBJ_THREAD_STACK_ELEMENT */
151struct z_stack_data {
152 /* Size of the entire stack object, including reserved areas */
153 size_t size;
154
155 /* Stack buffer for privilege mode elevations */
156 u8_t *priv;
157};
158#endif /* CONFIG_GEN_PRIV_STACKS */
159
Andrew Boief2734ab2020-03-11 06:37:42 -0700160/* Object extra data. Only some objects use this, determined by object type */
161union z_object_data {
162 /* Backing mutex for K_OBJ_SYS_MUTEX */
163 struct k_mutex *mutex;
164
165 /* Numerical thread ID for K_OBJ_THREAD */
166 unsigned int thread_id;
167
Andrew Boie28be7932020-03-11 10:56:19 -0700168#ifdef CONFIG_GEN_PRIV_STACKS
169 /* Metadata for K_OBJ_THREAD_STACK_ELEMENT */
170 struct z_stack_data *stack_data;
171#else
Andrew Boie4bad34e2020-03-11 06:56:58 -0700172 /* Stack buffer size for K_OBJ_THREAD_STACK_ELEMENT */
Andrew Boief2734ab2020-03-11 06:37:42 -0700173 size_t stack_size;
Andrew Boie28be7932020-03-11 10:56:19 -0700174#endif /* CONFIG_GEN_PRIV_STACKS */
Andrew Boief2734ab2020-03-11 06:37:42 -0700175
176 /* Futex wait queue and spinlock for K_OBJ_FUTEX */
177 struct z_futex_data *futex_data;
178
179 /* All other objects */
180 int unused;
181};
182
Andrew Boie945af952017-08-22 13:15:23 -0700183/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700184 * z_object_find() */
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700185struct z_object {
Andrew Boie22553a72019-11-19 18:27:42 -0800186 void *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700187 u8_t perms[CONFIG_MAX_THREAD_BYTES];
188 u8_t type;
189 u8_t flags;
Andrew Boief2734ab2020-03-11 06:37:42 -0700190 union z_object_data data;
Andrew Boiedf555242018-05-25 07:28:54 -0700191} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700192
Andrew Boie2f3a89f2020-03-11 07:00:55 -0700193struct z_object_assignment {
Andrew Boie877f82e2017-10-17 11:20:22 -0700194 struct k_thread *thread;
195 void * const *objects;
196};
197
198/**
199 * @brief Grant a static thread access to a list of kernel objects
200 *
201 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
202 * a set of kernel objects. These objects do not need to be in an initialized
203 * state. The permissions will be granted when the threads are initialized
204 * in the early boot sequence.
205 *
206 * All arguments beyond the first must be pointers to kernel objects.
207 *
208 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
209 */
210#define K_THREAD_ACCESS_GRANT(name_, ...) \
211 static void * const _CONCAT(_object_list_, name_)[] = \
212 { __VA_ARGS__, NULL }; \
Andrew Boie2f3a89f2020-03-11 07:00:55 -0700213 static const Z_STRUCT_SECTION_ITERABLE(z_object_assignment, \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400214 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700215 { (&_k_thread_obj_ ## name_), \
216 (_CONCAT(_object_list_, name_)) }
217
Anas Nashife71293e2019-12-04 20:00:14 -0500218/** Object initialized */
Andrew Boie945af952017-08-22 13:15:23 -0700219#define K_OBJ_FLAG_INITIALIZED BIT(0)
Anas Nashife71293e2019-12-04 20:00:14 -0500220/** Object is Public */
Andrew Boie04caa672017-10-13 13:57:07 -0700221#define K_OBJ_FLAG_PUBLIC BIT(1)
Anas Nashife71293e2019-12-04 20:00:14 -0500222/** Object allocated */
Andrew Boie97bf0012018-04-24 17:01:37 -0700223#define K_OBJ_FLAG_ALLOC BIT(2)
Anas Nashife71293e2019-12-04 20:00:14 -0500224/** Driver Object */
Andrew Boie78757072019-07-23 13:29:30 -0700225#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700226
227/**
228 * Lookup a kernel object and init its metadata if it exists
229 *
230 * Calling this on an object will make it usable from userspace.
231 * Intended to be called as the last statement in kernel object init
232 * functions.
233 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500234 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700235 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700236void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700237#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700238/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700239#define K_THREAD_ACCESS_GRANT(thread, ...)
240
Anas Nashif954d5502018-02-25 08:37:28 -0600241/**
242 * @internal
243 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700244static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700245{
246 ARG_UNUSED(obj);
247}
248
Anas Nashif954d5502018-02-25 08:37:28 -0600249/**
250 * @internal
251 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700252static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700253 struct k_thread *thread)
254{
255 ARG_UNUSED(object);
256 ARG_UNUSED(thread);
257}
258
Anas Nashif954d5502018-02-25 08:37:28 -0600259/**
260 * @internal
261 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700262static inline void k_object_access_revoke(void *object,
263 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700264{
265 ARG_UNUSED(object);
266 ARG_UNUSED(thread);
267}
268
Andrew Boiee9cfc542018-04-13 13:15:28 -0700269/**
270 * @internal
271 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700272static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700273{
274 ARG_UNUSED(object);
275}
276
Andrew Boie41bab6e2017-10-14 14:42:23 -0700277static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700278{
279 ARG_UNUSED(object);
280}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700281/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700282#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700283
284/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600285 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700286 *
287 * The thread will be granted access to the object if the caller is from
288 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700289 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700290 *
291 * @param object Address of kernel object
292 * @param thread Thread to grant access to the object
293 */
Andrew Boie743e4682017-10-04 12:25:50 -0700294__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700295
Andrew Boiea89bf012017-10-09 14:47:55 -0700296/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600297 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700298 *
299 * The thread will lose access to the object if the caller is from
300 * supervisor mode, or the caller is from user mode AND has permissions
301 * on both the object and the thread whose access is being revoked.
302 *
303 * @param object Address of kernel object
304 * @param thread Thread to remove access to the object
305 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700306void k_object_access_revoke(void *object, struct k_thread *thread);
307
Anas Nashife71293e2019-12-04 20:00:14 -0500308/**
309 * @brief Release an object
310 *
311 * Allows user threads to drop their own permission on an object
312 * Their permissions are automatically cleared when a thread terminates.
313 *
314 * @param object The object to be released
315 *
316 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700317__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700318
319/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600320 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700321 *
322 * If the caller is from supervisor mode, or the caller is from user mode and
323 * have sufficient permissions on the object, then that object will have
324 * permissions granted to it for *all* current and future threads running in
325 * the system, effectively becoming a public kernel object.
326 *
327 * Use of this API should be avoided on systems that are running untrusted code
328 * as it is possible for such code to derive the addresses of kernel objects
329 * and perform unwanted operations on them.
330 *
Andrew Boie04caa672017-10-13 13:57:07 -0700331 * It is not possible to revoke permissions on public objects; once public,
332 * any thread may use it.
333 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700334 * @param object Address of kernel object
335 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700336void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700337
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338/**
339 * Allocate a kernel object of a designated type
340 *
341 * This will instantiate at runtime a kernel object of the specified type,
342 * returning a pointer to it. The object will be returned in an uninitialized
343 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700344 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800345 *
346 * Currently, allocation of thread stacks is not supported.
347 *
348 * @param otype Requested kernel object type
349 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
350 * available
351 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700352__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800353
Andrew Boie97bf0012018-04-24 17:01:37 -0700354#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800355/**
356 * Free a kernel object previously allocated with k_object_alloc()
357 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700358 * This will return memory for a kernel object back to resource pool it was
359 * allocated from. Care must be exercised that the object will not be used
360 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800361 *
362 * @param obj Pointer to the kernel object memory address.
363 */
364void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700365#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700366/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700367static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700368{
Kumar Gala85699f72018-05-17 09:26:03 -0500369 ARG_UNUSED(otype);
370
Andrew Boie97bf0012018-04-24 17:01:37 -0700371 return NULL;
372}
Anas Nashife71293e2019-12-04 20:00:14 -0500373/**
374 * @brief Free an object
375 *
376 * @param obj
377 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700378static inline void k_obj_free(void *obj)
379{
380 ARG_UNUSED(obj);
381}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700382/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800383#endif /* CONFIG_DYNAMIC_OBJECTS */
384
Anas Nashif4bcb2942019-01-23 23:06:29 -0500385/** @} */
386
Andrew Boiebca15da2017-10-15 14:17:48 -0700387
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700388/**
389 * @typedef k_thread_entry_t
390 * @brief Thread entry point function type.
391 *
392 * A thread's entry point function is invoked when the thread starts executing.
393 * Up to 3 argument values can be passed to the function.
394 *
395 * The thread terminates execution permanently if the entry point function
396 * returns. The thread is responsible for releasing any shared resources
397 * it may own (such as mutexes and dynamically allocated memory), prior to
398 * returning.
399 *
400 * @param p1 First argument.
401 * @param p2 Second argument.
402 * @param p3 Third argument.
403 *
404 * @return N/A
405 */
Andrew Boie73abd322017-04-04 13:19:13 -0700406
407#ifdef CONFIG_THREAD_MONITOR
408struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700409 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700410 void *parameter1;
411 void *parameter2;
412 void *parameter3;
413};
414#endif
415
416/* can be used for creating 'dummy' threads, e.g. for pending on objects */
417struct _thread_base {
418
419 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700420 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600421 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700422 struct rbnode qnode_rb;
423 };
424
Andy Ross1acd8c22018-05-03 14:51:49 -0700425 /* wait queue on which the thread is pended (needed only for
426 * trees, not dumb lists)
427 */
428 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700429
430 /* user facing 'thread options'; values defined in include/kernel.h */
431 u8_t user_options;
432
433 /* thread state */
434 u8_t thread_state;
435
436 /*
437 * scheduler lock count and thread priority
438 *
439 * These two fields control the preemptibility of a thread.
440 *
441 * When the scheduler is locked, sched_locked is decremented, which
442 * means that the scheduler is locked for values from 0xff to 0x01. A
443 * thread is coop if its prio is negative, thus 0x80 to 0xff when
444 * looked at the value as unsigned.
445 *
446 * By putting them end-to-end, this means that a thread is
447 * non-preemptible if the bundled value is greater than or equal to
448 * 0x0080.
449 */
450 union {
451 struct {
452#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
453 u8_t sched_locked;
454 s8_t prio;
455#else /* LITTLE and PDP */
456 s8_t prio;
457 u8_t sched_locked;
458#endif
459 };
460 u16_t preempt;
461 };
462
Andy Ross4a2e50f2018-05-15 11:06:25 -0700463#ifdef CONFIG_SCHED_DEADLINE
464 int prio_deadline;
465#endif
466
Andy Ross1acd8c22018-05-03 14:51:49 -0700467 u32_t order_key;
468
Andy Ross2724fd12018-01-29 14:55:20 -0800469#ifdef CONFIG_SMP
470 /* True for the per-CPU idle threads */
471 u8_t is_idle;
472
Andy Ross2724fd12018-01-29 14:55:20 -0800473 /* CPU index on which thread was last run */
474 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700475
476 /* Recursive count of irq_lock() calls */
477 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800478
479#endif
480
481#ifdef CONFIG_SCHED_CPU_MASK
482 /* "May run on" bits for each CPU */
483 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800484#endif
485
Andrew Boie73abd322017-04-04 13:19:13 -0700486 /* data returned by APIs */
487 void *swap_data;
488
489#ifdef CONFIG_SYS_CLOCK_EXISTS
490 /* this thread's entry in a timeout queue */
491 struct _timeout timeout;
492#endif
Andrew Boie322816e2020-02-20 16:33:06 -0800493
494 _wait_q_t join_waiters;
Andrew Boie73abd322017-04-04 13:19:13 -0700495};
496
497typedef struct _thread_base _thread_base_t;
498
499#if defined(CONFIG_THREAD_STACK_INFO)
500/* Contains the stack information of a thread */
501struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700502 /* Stack start - Represents the start address of the thread-writable
503 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700504 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400505 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700506
507 /* Stack Size - Thread writable stack buffer size. Represents
508 * the size of the actual area, starting from the start member,
509 * that should be writable by the thread
510 */
Andrew Boie528233e2019-12-11 14:54:15 -0800511 size_t size;
Andrew Boie73abd322017-04-04 13:19:13 -0700512};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700513
514typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700515#endif /* CONFIG_THREAD_STACK_INFO */
516
Chunlin Hane9c97022017-07-07 20:29:30 +0800517#if defined(CONFIG_USERSPACE)
518struct _mem_domain_info {
Anas Nashife71293e2019-12-04 20:00:14 -0500519 /** memory domain queue node */
Chunlin Hane9c97022017-07-07 20:29:30 +0800520 sys_dnode_t mem_domain_q_node;
Anas Nashife71293e2019-12-04 20:00:14 -0500521 /** memory domain of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800522 struct k_mem_domain *mem_domain;
523};
524
525#endif /* CONFIG_USERSPACE */
526
Daniel Leungfc182432018-08-16 15:42:28 -0700527#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
528struct _thread_userspace_local_data {
529 int errno_var;
530};
531#endif
532
Anas Nashifce78d162018-05-24 12:43:11 -0500533/**
534 * @ingroup thread_apis
535 * Thread Structure
536 */
Andrew Boie73abd322017-04-04 13:19:13 -0700537struct k_thread {
538
539 struct _thread_base base;
540
Anas Nashifce78d162018-05-24 12:43:11 -0500541 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700542 struct _callee_saved callee_saved;
543
Anas Nashifce78d162018-05-24 12:43:11 -0500544 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700545 void *init_data;
546
Anas Nashifce78d162018-05-24 12:43:11 -0500547 /**
548 * abort function
Anas Nashifce78d162018-05-24 12:43:11 -0500549 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700550 void (*fn_abort)(void);
551
552#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500553 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700554 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700555
Anas Nashifce78d162018-05-24 12:43:11 -0500556 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700557 struct k_thread *next_thread;
558#endif
559
Anas Nashif57554052018-03-03 02:31:05 -0600560#if defined(CONFIG_THREAD_NAME)
Anas Nashife71293e2019-12-04 20:00:14 -0500561 /** Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700562 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600563#endif
564
Andrew Boie73abd322017-04-04 13:19:13 -0700565#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500566 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700567 void *custom_data;
568#endif
569
Daniel Leungfc182432018-08-16 15:42:28 -0700570#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
571 struct _thread_userspace_local_data *userspace_local_data;
572#endif
573
Andrew Boie73abd322017-04-04 13:19:13 -0700574#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700575#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500576 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700577 int errno_var;
578#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700579#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700580
581#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500582 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700583 struct _thread_stack_info stack_info;
584#endif /* CONFIG_THREAD_STACK_INFO */
585
Chunlin Hane9c97022017-07-07 20:29:30 +0800586#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500587 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800588 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500589 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700590 k_thread_stack_t *stack_obj;
Andy Ross7353c7f2020-02-06 13:39:03 -0800591 /** current syscall frame pointer */
592 void *syscall_frame;
Chunlin Hane9c97022017-07-07 20:29:30 +0800593#endif /* CONFIG_USERSPACE */
594
Andy Ross7353c7f2020-02-06 13:39:03 -0800595
Andy Ross042d8ec2017-12-09 08:37:20 -0800596#if defined(CONFIG_USE_SWITCH)
597 /* When using __switch() a few previously arch-specific items
598 * become part of the core OS
599 */
600
Patrik Flykt4344e272019-03-08 14:19:05 -0700601 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800602 int swap_retval;
603
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800604 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800605 void *switch_handle;
606#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500607 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700608 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800609
Anas Nashifce78d162018-05-24 12:43:11 -0500610 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700611 struct _thread_arch arch;
612};
613
614typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400615typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400616
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400617enum execution_context_types {
618 K_ISR = 0,
619 K_COOP_THREAD,
620 K_PREEMPT_THREAD,
621};
622
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500624 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100625 * @{
626 */
Anas Nashife71293e2019-12-04 20:00:14 -0500627
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530628typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
629 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100630
631/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530632 * @brief Iterate over all the threads in the system.
633 *
634 * This routine iterates over all the threads in the system and
635 * calls the user_cb function for each thread.
636 *
637 * @param user_cb Pointer to the user callback function.
638 * @param user_data Pointer to user data.
639 *
640 * @note CONFIG_THREAD_MONITOR must be set for this function
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100641 * to be effective.
642 * @note This API uses @ref k_spin_lock to protect the _kernel.threads
643 * list which means creation of new threads and terminations of existing
644 * threads are blocked until this API returns.
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530645 *
646 * @return N/A
647 */
648extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
649
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100650/**
651 * @brief Iterate over all the threads in the system without locking.
652 *
653 * This routine works exactly the same like @ref k_thread_foreach
654 * but unlocks interrupts when user_cb is executed.
655 *
656 * @param user_cb Pointer to the user callback function.
657 * @param user_data Pointer to user data.
658 *
659 * @note CONFIG_THREAD_MONITOR must be set for this function
660 * to be effective.
661 * @note This API uses @ref k_spin_lock only when accessing the _kernel.threads
662 * queue elements. It unlocks it during user callback function processing.
663 * If a new task is created when this @c foreach function is in progress,
664 * the added new task would not be included in the enumeration.
665 * If a task is aborted during this enumeration, there would be a race here
666 * and there is a possibility that this aborted task would be included in the
667 * enumeration.
668 * @note If the task is aborted and the memory occupied by its @c k_thread
669 * structure is reused when this @c k_thread_foreach_unlocked is in progress
670 * it might even lead to the system behave unstable.
671 * This function may never return, as it would follow some @c next task
672 * pointers treating given pointer as a pointer to the k_thread structure
673 * while it is something different right now.
674 * Do not reuse the memory that was occupied by k_thread structure of aborted
675 * task if it was aborted after this function was called in any context.
676 */
677extern void k_thread_foreach_unlocked(
678 k_thread_user_cb_t user_cb, void *user_data);
679
Anas Nashif166f5192018-02-25 08:02:36 -0600680/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100681
682/**
Allan Stephensc98da842016-11-11 15:45:03 -0500683 * @defgroup thread_apis Thread APIs
684 * @ingroup kernel_apis
685 * @{
686 */
687
Benjamin Walshed240f22017-01-22 13:05:08 -0500688#endif /* !_ASMLANGUAGE */
689
690
691/*
692 * Thread user options. May be needed by assembly code. Common part uses low
693 * bits, arch-specific use high bits.
694 */
695
Anas Nashifa541e932018-05-24 11:19:16 -0500696/**
697 * @brief system thread that must not abort
Anas Nashifa541e932018-05-24 11:19:16 -0500698 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700699#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500700
Stephanos Ioannidisaaf93202020-05-03 18:03:19 +0900701#if defined(CONFIG_FPU_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500702/**
703 * @brief thread uses floating point registers
704 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700705#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500706#endif
707
Anas Nashifa541e932018-05-24 11:19:16 -0500708/**
709 * @brief user mode thread
710 *
711 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700712 * has additional restrictions
713 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700714#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700715
Anas Nashifa541e932018-05-24 11:19:16 -0500716/**
717 * @brief Inherit Permissions
718 *
719 * @details
720 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700721 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
722 * is not enabled.
723 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700724#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700725
Benjamin Walshed240f22017-01-22 13:05:08 -0500726#ifdef CONFIG_X86
727/* x86 Bitmask definitions for threads user options */
728
Stephanos Ioannidisaaf93202020-05-03 18:03:19 +0900729#if defined(CONFIG_FPU_SHARING) && defined(CONFIG_SSE)
Benjamin Walshed240f22017-01-22 13:05:08 -0500730/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700731#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500732#endif
733#endif
734
735/* end - thread options */
736
737#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400738/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700739 * @brief Create a thread.
740 *
741 * This routine initializes a thread, then schedules it for execution.
742 *
743 * The new thread may be scheduled for immediate execution or a delayed start.
744 * If the newly spawned thread does not have a delayed start the kernel
745 * scheduler may preempt the current thread to allow the new thread to
746 * execute.
747 *
748 * Thread options are architecture-specific, and can include K_ESSENTIAL,
749 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
750 * them using "|" (the logical OR operator).
751 *
752 * Historically, users often would use the beginning of the stack memory region
753 * to store the struct k_thread data, although corruption will occur if the
754 * stack overflows this region and stack protection features may not detect this
755 * situation.
756 *
757 * @param new_thread Pointer to uninitialized struct k_thread
758 * @param stack Pointer to the stack space.
759 * @param stack_size Stack size in bytes.
760 * @param entry Thread entry function.
761 * @param p1 1st entry point parameter.
762 * @param p2 2nd entry point parameter.
763 * @param p3 3rd entry point parameter.
764 * @param prio Thread priority.
765 * @param options Thread options.
Andy Ross78327382020-03-05 15:18:14 -0800766 * @param delay Scheduling delay, or K_NO_WAIT (for no delay).
Andrew Boied26cf2d2017-03-30 13:07:02 -0700767 *
768 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400769 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700770 */
Andrew Boie662c3452017-10-02 10:51:18 -0700771__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700772 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700773 size_t stack_size,
774 k_thread_entry_t entry,
775 void *p1, void *p2, void *p3,
Andy Ross78327382020-03-05 15:18:14 -0800776 int prio, u32_t options, k_timeout_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700777
Andrew Boie3f091b52017-08-30 14:34:14 -0700778/**
779 * @brief Drop a thread's privileges permanently to user mode
780 *
781 * @param entry Function to start executing from
782 * @param p1 1st entry point parameter
783 * @param p2 2nd entry point parameter
784 * @param p3 3rd entry point parameter
785 */
786extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
787 void *p1, void *p2,
788 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700789
Andrew Boied26cf2d2017-03-30 13:07:02 -0700790/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530791 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700792 *
793 * This is a convenience function. For the provided thread, grant access to
794 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700795 *
796 * The thread object must be initialized (i.e. running). The objects don't
797 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530798 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700799 *
800 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530801 * @param ... list of kernel object pointers
Andrew Boiee12857a2017-10-17 11:38:26 -0700802 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530803#define k_thread_access_grant(thread, ...) \
804 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700805
806/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700807 * @brief Assign a resource memory pool to a thread
808 *
809 * By default, threads have no resource pool assigned unless their parent
810 * thread has a resource pool, in which case it is inherited. Multiple
811 * threads may be assigned to the same memory pool.
812 *
813 * Changing a thread's resource pool will not migrate allocations from the
814 * previous pool.
815 *
Jukka Rissanenfdf18482020-05-01 12:37:51 +0300816 * @param thread Target thread to assign a memory pool for resource requests.
817 * @param pool Memory pool to use for resources,
818 * or NULL if the thread should no longer have a memory pool.
Andrew Boie92e5bd72018-04-12 17:12:15 -0700819 */
820static inline void k_thread_resource_pool_assign(struct k_thread *thread,
821 struct k_mem_pool *pool)
822{
823 thread->resource_pool = pool;
824}
825
Andrew Boieefc5fe02020-02-05 10:41:58 -0800826#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
827/**
828 * @brief Obtain stack usage information for the specified thread
829 *
830 * User threads will need to have permission on the target thread object.
831 *
832 * Some hardware may prevent inspection of a stack buffer currently in use.
833 * If this API is called from supervisor mode, on the currently running thread,
834 * on a platform which selects CONFIG_NO_UNUSED_STACK_INSPECTION, an error
835 * will be generated.
836 *
837 * @param thread Thread to inspect stack information
838 * @param unused_ptr Output parameter, filled in with the unused stack space
839 * of the target thread in bytes.
840 * @return 0 on success
841 * @return -EBADF Bad thread object (user mode only)
842 * @return -EPERM No permissions on thread object (user mode only)
843 * #return -ENOTSUP Forbidden by hardware policy
844 * @return -EINVAL Thread is uninitialized or exited (user mode only)
845 * @return -EFAULT Bad memory address for unused_ptr (user mode only)
846 */
847__syscall int k_thread_stack_space_get(const struct k_thread *thread,
848 size_t *unused_ptr);
849#endif
850
Andrew Boie92e5bd72018-04-12 17:12:15 -0700851#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
852/**
853 * @brief Assign the system heap as a thread's resource pool
854 *
855 * Similar to k_thread_resource_pool_assign(), but the thread will use
856 * the kernel heap to draw memory.
857 *
858 * Use with caution, as a malicious thread could perform DoS attacks on the
859 * kernel heap.
860 *
861 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400862 *
Andrew Boie92e5bd72018-04-12 17:12:15 -0700863 */
864void k_thread_system_pool_assign(struct k_thread *thread);
865#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
866
867/**
Andrew Boie322816e2020-02-20 16:33:06 -0800868 * @brief Sleep until a thread exits
869 *
870 * The caller will be put to sleep until the target thread exits, either due
871 * to being aborted, self-exiting, or taking a fatal error. This API returns
872 * immediately if the thread isn't running.
873 *
874 * This API may only be called from ISRs with a K_NO_WAIT timeout.
875 *
876 * @param thread Thread to wait to exit
Andy Ross78327382020-03-05 15:18:14 -0800877 * @param timeout upper bound time to wait for the thread to exit.
Andrew Boie322816e2020-02-20 16:33:06 -0800878 * @retval 0 success, target thread has exited or wasn't running
879 * @retval -EBUSY returned without waiting
880 * @retval -EAGAIN waiting period timed out
881 * @retval -EDEADLK target thread is joining on the caller, or target thread
882 * is the caller
883 */
Andy Ross78327382020-03-05 15:18:14 -0800884__syscall int k_thread_join(struct k_thread *thread, k_timeout_t timeout);
885
886/**
887 * @brief Put the current thread to sleep.
888 *
889 * This routine puts the current thread to sleep for @a duration,
890 * specified as a k_timeout_t object.
891 *
892 * @param timeout Desired duration of sleep.
893 *
894 * @return Zero if the requested time has elapsed or the number of milliseconds
895 * left to sleep, if thread was woken up by \ref k_wakeup call.
896 */
897__syscall s32_t k_sleep(k_timeout_t timeout);
Andrew Boie322816e2020-02-20 16:33:06 -0800898
899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500900 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700902 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700904 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400905 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200906 * @return Zero if the requested time has elapsed or the number of milliseconds
907 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 */
Andy Ross78327382020-03-05 15:18:14 -0800909static inline s32_t k_msleep(s32_t ms)
910{
911 return k_sleep(Z_TIMEOUT_MS(ms));
912}
Charles E. Yousea5678312019-05-09 16:46:46 -0700913
914/**
915 * @brief Put the current thread to sleep with microsecond resolution.
916 *
917 * This function is unlikely to work as expected without kernel tuning.
918 * In particular, because the lower bound on the duration of a sleep is
919 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
920 * to achieve the resolution desired. The implications of doing this must
921 * be understood before attempting to use k_usleep(). Use with caution.
922 *
923 * @param us Number of microseconds to sleep.
924 *
925 * @return Zero if the requested time has elapsed or the number of microseconds
926 * left to sleep, if thread was woken up by \ref k_wakeup call.
927 */
928__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929
930/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500931 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400932 *
933 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 * @return N/A
937 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800938__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400939
940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400944 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500945 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400946 *
947 * @return N/A
948 */
Andrew Boie468190a2017-09-29 14:00:48 -0700949__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400950
951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500956 * If @a thread is not currently sleeping, the routine has no effect.
957 *
958 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959 *
960 * @return N/A
961 */
Andrew Boie468190a2017-09-29 14:00:48 -0700962__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963
964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500967 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400968 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700970__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971
972/**
Allan Stephensc98da842016-11-11 15:45:03 -0500973 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * This routine permanently stops execution of @a thread. The thread is taken
976 * off all kernel queues it is part of (i.e. the ready queue, the timeout
977 * queue, or a kernel object wait queue). However, any kernel resources the
978 * thread might currently own (such as mutexes or memory blocks) are not
979 * released. It is the responsibility of the caller of this routine to ensure
980 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400983 *
984 * @return N/A
985 */
Andrew Boie468190a2017-09-29 14:00:48 -0700986__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400987
Andrew Boie7d627c52017-08-30 11:01:56 -0700988
989/**
990 * @brief Start an inactive thread
991 *
992 * If a thread was created with K_FOREVER in the delay parameter, it will
993 * not be added to the scheduling queue until this function is called
994 * on it.
995 *
996 * @param thread thread to start
997 */
Andrew Boie468190a2017-09-29 14:00:48 -0700998__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700999
Andy Ross5a5d3da2020-03-09 13:59:15 -07001000extern k_ticks_t z_timeout_expires(struct _timeout *timeout);
1001extern k_ticks_t z_timeout_remaining(struct _timeout *timeout);
1002
1003#ifdef CONFIG_SYS_CLOCK_EXISTS
1004
1005/**
Andy Rosse39bf292020-03-19 10:30:33 -07001006 * @brief Get time when a thread wakes up, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -07001007 *
1008 * This routine computes the system uptime when a waiting thread next
1009 * executes, in units of system ticks. If the thread is not waiting,
1010 * it returns current system time.
1011 */
1012__syscall k_ticks_t k_thread_timeout_expires_ticks(struct k_thread *t);
1013
1014static inline k_ticks_t z_impl_k_thread_timeout_expires_ticks(
1015 struct k_thread *t)
1016{
1017 return z_timeout_expires(&t->base.timeout);
1018}
1019
1020/**
Andy Rosse39bf292020-03-19 10:30:33 -07001021 * @brief Get time remaining before a thread wakes up, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -07001022 *
1023 * This routine computes the time remaining before a waiting thread
1024 * next executes, in units of system ticks. If the thread is not
1025 * waiting, it returns zero.
1026 */
1027__syscall k_ticks_t k_thread_timeout_remaining_ticks(struct k_thread *t);
1028
1029static inline k_ticks_t z_impl_k_thread_timeout_remaining_ticks(
1030 struct k_thread *t)
1031{
1032 return z_timeout_remaining(&t->base.timeout);
1033}
1034
1035#endif /* CONFIG_SYS_CLOCK_EXISTS */
1036
Allan Stephensc98da842016-11-11 15:45:03 -05001037/**
1038 * @cond INTERNAL_HIDDEN
1039 */
1040
Benjamin Walshd211a522016-12-06 11:44:01 -05001041/* timeout has timed out and is not on _timeout_q anymore */
1042#define _EXPIRED (-2)
1043
Peter Mitsisa04c0d72016-09-28 19:26:00 -04001044struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -07001045 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -07001046 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001047 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -07001048 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -05001049 void *init_p1;
1050 void *init_p2;
1051 void *init_p3;
1052 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -05001053 u32_t init_options;
1054 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -05001055 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -06001056 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001057};
1058
Anas Nashif45a1d8a2020-04-24 11:29:17 -04001059#define Z_THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001060 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -06001061 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001062 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001063 .init_thread = (thread), \
1064 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001065 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -07001066 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001067 .init_p1 = (void *)p1, \
1068 .init_p2 = (void *)p2, \
1069 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001070 .init_prio = (prio), \
1071 .init_options = (options), \
1072 .init_delay = (delay), \
1073 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -06001074 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001075 }
1076
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001077/**
Allan Stephensc98da842016-11-11 15:45:03 -05001078 * INTERNAL_HIDDEN @endcond
1079 */
1080
1081/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * @brief Statically define and initialize a thread.
1083 *
1084 * The thread may be scheduled for immediate execution or a delayed start.
1085 *
1086 * Thread options are architecture-specific, and can include K_ESSENTIAL,
1087 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
1088 * them using "|" (the logical OR operator).
1089 *
1090 * The ID of the thread can be accessed using:
1091 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001092 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001093 *
1094 * @param name Name of the thread.
1095 * @param stack_size Stack size in bytes.
1096 * @param entry Thread entry function.
1097 * @param p1 1st entry point parameter.
1098 * @param p2 2nd entry point parameter.
1099 * @param p3 3rd entry point parameter.
1100 * @param prio Thread priority.
1101 * @param options Thread options.
Peter Bigot73c387c2020-04-20 08:55:20 -05001102 * @param delay Scheduling delay (in milliseconds), zero for no delay.
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001103 *
Anas Nashif47420d02018-05-24 14:20:56 -04001104 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001105 * @internal It has been observed that the x86 compiler by default aligns
1106 * these _static_thread_data structures to 32-byte boundaries, thereby
1107 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -04001108 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001109 */
Allan Stephens6cfe1322016-10-26 10:16:51 -05001110#define K_THREAD_DEFINE(name, stack_size, \
1111 entry, p1, p2, p3, \
1112 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -07001113 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001114 struct k_thread _k_thread_obj_##name; \
1115 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Anas Nashif45a1d8a2020-04-24 11:29:17 -04001116 Z_THREAD_INITIALIZER(&_k_thread_obj_##name, \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001117 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001118 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -06001119 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001120 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * @param thread ID of thread whose priority is needed.
1128 *
1129 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001131__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132
1133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
1138 * Rescheduling can occur immediately depending on the priority @a thread is
1139 * set to:
1140 *
1141 * - If its priority is raised above the priority of the caller of this
1142 * function, and the caller is preemptible, @a thread will be scheduled in.
1143 *
1144 * - If the caller operates on itself, it lowers its priority below that of
1145 * other threads in the system, and the caller is preemptible, the thread of
1146 * highest priority will be scheduled in.
1147 *
1148 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1149 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1150 * highest priority.
1151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001152 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 * @param prio New priority.
1154 *
1155 * @warning Changing the priority of a thread currently involved in mutex
1156 * priority inheritance may result in undefined behavior.
1157 *
1158 * @return N/A
1159 */
Andrew Boie468190a2017-09-29 14:00:48 -07001160__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161
Andy Ross4a2e50f2018-05-15 11:06:25 -07001162
1163#ifdef CONFIG_SCHED_DEADLINE
1164/**
1165 * @brief Set deadline expiration time for scheduler
1166 *
1167 * This sets the "deadline" expiration as a time delta from the
1168 * current time, in the same units used by k_cycle_get_32(). The
1169 * scheduler (when deadline scheduling is enabled) will choose the
1170 * next expiring thread when selecting between threads at the same
1171 * static priority. Threads at different priorities will be scheduled
1172 * according to their static priority.
1173 *
1174 * @note Deadlines that are negative (i.e. in the past) are still seen
1175 * as higher priority than others, even if the thread has "finished"
1176 * its work. If you don't want it scheduled anymore, you have to
1177 * reset the deadline into the future, block/pend the thread, or
1178 * modify its priority with k_thread_priority_set().
1179 *
1180 * @note Despite the API naming, the scheduler makes no guarantees the
1181 * the thread WILL be scheduled within that deadline, nor does it take
1182 * extra metadata (like e.g. the "runtime" and "period" parameters in
1183 * Linux sched_setattr()) that allows the kernel to validate the
1184 * scheduling for achievability. Such features could be implemented
1185 * above this call, which is simply input to the priority selection
1186 * logic.
1187 *
Anas Nashif240c5162019-06-10 12:25:50 -04001188 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001189 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001190 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1191 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001192 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001193 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001194 * @param thread A thread on which to set the deadline
1195 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001196 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001197 */
1198__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1199#endif
1200
Andy Rossab46b1b2019-01-30 15:00:42 -08001201#ifdef CONFIG_SCHED_CPU_MASK
1202/**
1203 * @brief Sets all CPU enable masks to zero
1204 *
1205 * After this returns, the thread will no longer be schedulable on any
1206 * CPUs. The thread must not be currently runnable.
1207 *
Anas Nashif240c5162019-06-10 12:25:50 -04001208 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001209 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001210 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1211 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001212 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001213 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001214 * @param thread Thread to operate upon
1215 * @return Zero on success, otherwise error code
1216 */
1217int k_thread_cpu_mask_clear(k_tid_t thread);
1218
1219/**
1220 * @brief Sets all CPU enable masks to one
1221 *
1222 * After this returns, the thread will be schedulable on any CPU. The
1223 * thread must not be currently runnable.
1224 *
Anas Nashif240c5162019-06-10 12:25:50 -04001225 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001226 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001227 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1228 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001229 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001230 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001231 * @param thread Thread to operate upon
1232 * @return Zero on success, otherwise error code
1233 */
1234int k_thread_cpu_mask_enable_all(k_tid_t thread);
1235
1236/**
1237 * @brief Enable thread to run on specified CPU
1238 *
1239 * The thread must not be currently runnable.
1240 *
Anas Nashif240c5162019-06-10 12:25:50 -04001241 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001242 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001243 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1244 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001245 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001246 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001247 * @param thread Thread to operate upon
1248 * @param cpu CPU index
1249 * @return Zero on success, otherwise error code
1250 */
1251int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1252
1253/**
1254 * @brief Prevent thread to run on specified CPU
1255 *
1256 * The thread must not be currently runnable.
1257 *
Anas Nashif240c5162019-06-10 12:25:50 -04001258 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001259 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001260 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1261 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001262 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001263 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001264 * @param thread Thread to operate upon
1265 * @param cpu CPU index
1266 * @return Zero on success, otherwise error code
1267 */
1268int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1269#endif
1270
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001271/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001273 *
Andy Ross50d09422019-11-19 11:20:07 -08001274 * This routine prevents the kernel scheduler from making @a thread
1275 * the current thread. All other internal operations on @a thread are
1276 * still performed; for example, kernel objects it is waiting on are
1277 * still handed to it. Note that any existing timeouts
1278 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1279 * will be canceled. On resume, the thread will begin running
1280 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * If @a thread is already suspended, the routine has no effect.
1283 *
1284 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001285 *
1286 * @return N/A
1287 */
Andrew Boie468190a2017-09-29 14:00:48 -07001288__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289
1290/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001291 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * This routine allows the kernel scheduler to make @a thread the current
1294 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * If @a thread is not currently suspended, the routine has no effect.
1297 *
1298 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299 *
1300 * @return N/A
1301 */
Andrew Boie468190a2017-09-29 14:00:48 -07001302__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001305 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001307 * This routine specifies how the scheduler will perform time slicing of
1308 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001310 * To enable time slicing, @a slice must be non-zero. The scheduler
1311 * ensures that no thread runs for more than the specified time limit
1312 * before other threads of that priority are given a chance to execute.
1313 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001314 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001316 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001317 * execute. Once the scheduler selects a thread for execution, there is no
1318 * minimum guaranteed time the thread will execute before threads of greater or
1319 * equal priority are scheduled.
1320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001321 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001322 * for execution, this routine has no effect; the thread is immediately
1323 * rescheduled after the slice period expires.
1324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001325 * To disable timeslicing, set both @a slice and @a prio to zero.
1326 *
1327 * @param slice Maximum time slice length (in milliseconds).
1328 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001329 *
1330 * @return N/A
1331 */
Kumar Galacc334c72017-04-21 10:55:34 -05001332extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001333
Anas Nashif166f5192018-02-25 08:02:36 -06001334/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001335
1336/**
1337 * @addtogroup isr_apis
1338 * @{
1339 */
1340
1341/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001342 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001343 *
Allan Stephensc98da842016-11-11 15:45:03 -05001344 * This routine allows the caller to customize its actions, depending on
1345 * whether it is a thread or an ISR.
1346 *
1347 * @note Can be called by ISRs.
1348 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001349 * @return false if invoked by a thread.
1350 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001351 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001352extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001353
Benjamin Walsh445830d2016-11-10 15:54:27 -05001354/**
1355 * @brief Determine if code is running in a preemptible thread.
1356 *
Allan Stephensc98da842016-11-11 15:45:03 -05001357 * This routine allows the caller to customize its actions, depending on
1358 * whether it can be preempted by another thread. The routine returns a 'true'
1359 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001360 *
Allan Stephensc98da842016-11-11 15:45:03 -05001361 * - The code is running in a thread, not at ISR.
1362 * - The thread's priority is in the preemptible range.
1363 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001364 *
Allan Stephensc98da842016-11-11 15:45:03 -05001365 * @note Can be called by ISRs.
1366 *
1367 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001368 * @return Non-zero if invoked by a preemptible thread.
1369 */
Andrew Boie468190a2017-09-29 14:00:48 -07001370__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001371
Allan Stephensc98da842016-11-11 15:45:03 -05001372/**
Peter Bigot74ef3952019-12-23 11:48:43 -06001373 * @brief Test whether startup is in the before-main-task phase.
1374 *
1375 * This routine allows the caller to customize its actions, depending on
1376 * whether it being invoked before the kernel is fully active.
1377 *
1378 * @note Can be called by ISRs.
1379 *
1380 * @return true if invoked before post-kernel initialization
1381 * @return false if invoked during/after post-kernel initialization
1382 */
1383static inline bool k_is_pre_kernel(void)
1384{
1385 extern bool z_sys_post_kernel; /* in init.c */
1386
1387 return !z_sys_post_kernel;
1388}
1389
1390/**
Anas Nashif166f5192018-02-25 08:02:36 -06001391 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001392 */
1393
1394/**
1395 * @addtogroup thread_apis
1396 * @{
1397 */
1398
1399/**
1400 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001401 *
Allan Stephensc98da842016-11-11 15:45:03 -05001402 * This routine prevents the current thread from being preempted by another
1403 * thread by instructing the scheduler to treat it as a cooperative thread.
1404 * If the thread subsequently performs an operation that makes it unready,
1405 * it will be context switched out in the normal manner. When the thread
1406 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001407 *
Allan Stephensc98da842016-11-11 15:45:03 -05001408 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001409 *
Allan Stephensc98da842016-11-11 15:45:03 -05001410 * @note k_sched_lock() and k_sched_unlock() should normally be used
1411 * when the operation being performed can be safely interrupted by ISRs.
1412 * However, if the amount of processing involved is very small, better
1413 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001414 *
1415 * @return N/A
1416 */
1417extern void k_sched_lock(void);
1418
Allan Stephensc98da842016-11-11 15:45:03 -05001419/**
1420 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001421 *
Allan Stephensc98da842016-11-11 15:45:03 -05001422 * This routine reverses the effect of a previous call to k_sched_lock().
1423 * A thread must call the routine once for each time it called k_sched_lock()
1424 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001425 *
1426 * @return N/A
1427 */
1428extern void k_sched_unlock(void);
1429
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001430/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001431 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001433 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001435 * Custom data is not used by the kernel itself, and is freely available
1436 * for a thread to use as it sees fit. It can be used as a framework
1437 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001439 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001440 *
1441 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001442 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001443 */
Andrew Boie468190a2017-09-29 14:00:48 -07001444__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001445
1446/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001447 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001449 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001451 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001452 */
Andrew Boie468190a2017-09-29 14:00:48 -07001453__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454
1455/**
Anas Nashif57554052018-03-03 02:31:05 -06001456 * @brief Set current thread name
1457 *
1458 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1459 * tracing and debugging.
1460 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001461 * @param thread_id Thread to set name, or NULL to set the current thread
1462 * @param value Name string
1463 * @retval 0 on success
1464 * @retval -EFAULT Memory access error with supplied string
1465 * @retval -ENOSYS Thread name configuration option not enabled
1466 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001467 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001468__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001469
1470/**
1471 * @brief Get thread name
1472 *
1473 * Get the name of a thread
1474 *
1475 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001476 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001477 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001478const char *k_thread_name_get(k_tid_t thread_id);
1479
1480/**
1481 * @brief Copy the thread name into a supplied buffer
1482 *
1483 * @param thread_id Thread to obtain name information
1484 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001485 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001486 * @retval -ENOSPC Destination buffer too small
1487 * @retval -EFAULT Memory access error
1488 * @retval -ENOSYS Thread name feature not enabled
1489 * @retval 0 Success
1490 */
1491__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1492 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001493
1494/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001495 * @brief Get thread state string
1496 *
1497 * Get the human friendly thread state string
1498 *
1499 * @param thread_id Thread ID
1500 * @retval Thread state string, empty if no state flag is set
1501 */
1502const char *k_thread_state_str(k_tid_t thread_id);
1503
1504/**
Andy Rosscfe62032018-09-29 07:34:55 -07001505 * @}
1506 */
1507
1508/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001509 * @addtogroup clock_apis
1510 * @{
1511 */
1512
1513/**
1514 * @brief Generate null timeout delay.
1515 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001516 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001517 * not to wait if the requested operation cannot be performed immediately.
1518 *
1519 * @return Timeout delay value.
1520 */
Andy Ross78327382020-03-05 15:18:14 -08001521#define K_NO_WAIT Z_TIMEOUT_NO_WAIT
Allan Stephensc2f15a42016-11-17 12:24:22 -05001522
1523/**
Andy Rosse1bc5952020-03-09 12:19:54 -07001524 * @brief Generate timeout delay from nanoseconds.
1525 *
1526 * This macro generates a timeout delay that instructs a kernel API to
1527 * wait up to @a t nanoseconds to perform the requested operation.
1528 * Note that timer precision is limited to the tick rate, not the
1529 * requested value.
1530 *
Andy Rosse39bf292020-03-19 10:30:33 -07001531 * @param t Duration in nanoseconds.
Andy Rosse1bc5952020-03-09 12:19:54 -07001532 *
1533 * @return Timeout delay value.
1534 */
1535#define K_NSEC(t) Z_TIMEOUT_NS(t)
1536
1537/**
1538 * @brief Generate timeout delay from microseconds.
1539 *
1540 * This macro generates a timeout delay that instructs a kernel API
1541 * to wait up to @a t microseconds to perform the requested operation.
1542 * Note that timer precision is limited to the tick rate, not the
1543 * requested value.
1544 *
Andy Rosse39bf292020-03-19 10:30:33 -07001545 * @param t Duration in microseconds.
Andy Rosse1bc5952020-03-09 12:19:54 -07001546 *
1547 * @return Timeout delay value.
1548 */
1549#define K_USEC(t) Z_TIMEOUT_US(t)
1550
1551/**
1552 * @brief Generate timeout delay from cycles.
1553 *
1554 * This macro generates a timeout delay that instructs a kernel API
1555 * to wait up to @a t cycles to perform the requested operation.
1556 *
Andy Rosse39bf292020-03-19 10:30:33 -07001557 * @param t Duration in cycles.
Andy Rosse1bc5952020-03-09 12:19:54 -07001558 *
1559 * @return Timeout delay value.
1560 */
1561#define K_CYC(t) Z_TIMEOUT_CYC(t)
1562
1563/**
1564 * @brief Generate timeout delay from system ticks.
1565 *
1566 * This macro generates a timeout delay that instructs a kernel API
1567 * to wait up to @a t ticks to perform the requested operation.
1568 *
Andy Rosse39bf292020-03-19 10:30:33 -07001569 * @param t Duration in system ticks.
Andy Rosse1bc5952020-03-09 12:19:54 -07001570 *
1571 * @return Timeout delay value.
1572 */
1573#define K_TICKS(t) Z_TIMEOUT_TICKS(t)
1574
1575/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001576 * @brief Generate timeout delay from milliseconds.
1577 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001578 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001579 * to wait up to @a ms milliseconds to perform the requested operation.
1580 *
1581 * @param ms Duration in milliseconds.
1582 *
1583 * @return Timeout delay value.
1584 */
Andy Ross78327382020-03-05 15:18:14 -08001585#define K_MSEC(ms) Z_TIMEOUT_MS(ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001586
1587/**
1588 * @brief Generate timeout delay from seconds.
1589 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001590 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001591 * to wait up to @a s seconds to perform the requested operation.
1592 *
1593 * @param s Duration in seconds.
1594 *
1595 * @return Timeout delay value.
1596 */
Johan Hedberg14471692016-11-13 10:52:15 +02001597#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001598
1599/**
1600 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001601
1602 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001603 * to wait up to @a m minutes to perform the requested operation.
1604 *
1605 * @param m Duration in minutes.
1606 *
1607 * @return Timeout delay value.
1608 */
Johan Hedberg14471692016-11-13 10:52:15 +02001609#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001610
1611/**
1612 * @brief Generate timeout delay from hours.
1613 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001614 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001615 * to wait up to @a h hours to perform the requested operation.
1616 *
1617 * @param h Duration in hours.
1618 *
1619 * @return Timeout delay value.
1620 */
Johan Hedberg14471692016-11-13 10:52:15 +02001621#define K_HOURS(h) K_MINUTES((h) * 60)
1622
Allan Stephensc98da842016-11-11 15:45:03 -05001623/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001624 * @brief Generate infinite timeout delay.
1625 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001626 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001627 * to wait as long as necessary to perform the requested operation.
1628 *
1629 * @return Timeout delay value.
1630 */
Andy Ross78327382020-03-05 15:18:14 -08001631#define K_FOREVER Z_FOREVER
Allan Stephensc2f15a42016-11-17 12:24:22 -05001632
Andy Rosse1bc5952020-03-09 12:19:54 -07001633#ifdef CONFIG_TIMEOUT_64BIT
1634
Allan Stephensc2f15a42016-11-17 12:24:22 -05001635/**
Andy Rosse39bf292020-03-19 10:30:33 -07001636 * @brief Generates an absolute/uptime timeout value from system ticks
Andy Ross4c7b77a2020-03-09 09:35:35 -07001637 *
1638 * This macro generates a timeout delay that represents an expiration
Andy Rosse39bf292020-03-19 10:30:33 -07001639 * at the absolute uptime value specified, in system ticks. That is, the
Andy Ross4c7b77a2020-03-09 09:35:35 -07001640 * timeout will expire immediately after the system uptime reaches the
1641 * specified tick count.
1642 *
1643 * @param t Tick uptime value
1644 * @return Timeout delay value
1645 */
1646#define K_TIMEOUT_ABS_TICKS(t) Z_TIMEOUT_TICKS(Z_TICK_ABS(MAX(t, 0)))
1647
1648/**
Andy Rosse39bf292020-03-19 10:30:33 -07001649 * @brief Generates an absolute/uptime timeout value from milliseconds
Andy Ross4c7b77a2020-03-09 09:35:35 -07001650 *
1651 * This macro generates a timeout delay that represents an expiration
1652 * at the absolute uptime value specified, in milliseconds. That is,
1653 * the timeout will expire immediately after the system uptime reaches
1654 * the specified tick count.
1655 *
1656 * @param t Millisecond uptime value
1657 * @return Timeout delay value
1658 */
1659#define K_TIMEOUT_ABS_MS(t) K_TIMEOUT_ABS_TICKS(k_ms_to_ticks_ceil64(t))
1660
1661/**
Andy Rosse39bf292020-03-19 10:30:33 -07001662 * @brief Generates an absolute/uptime timeout value from microseconds
Andy Rosse1bc5952020-03-09 12:19:54 -07001663 *
1664 * This macro generates a timeout delay that represents an expiration
1665 * at the absolute uptime value specified, in microseconds. That is,
1666 * the timeout will expire immediately after the system uptime reaches
1667 * the specified time. Note that timer precision is limited by the
1668 * system tick rate and not the requested timeout value.
1669 *
1670 * @param t Microsecond uptime value
1671 * @return Timeout delay value
1672 */
1673#define K_TIMEOUT_ABS_US(t) K_TIMEOUT_ABS_TICKS(k_us_to_ticks_ceil64(t))
1674
1675/**
Andy Rosse39bf292020-03-19 10:30:33 -07001676 * @brief Generates an absolute/uptime timeout value from nanoseconds
Andy Rosse1bc5952020-03-09 12:19:54 -07001677 *
1678 * This macro generates a timeout delay that represents an expiration
1679 * at the absolute uptime value specified, in nanoseconds. That is,
1680 * the timeout will expire immediately after the system uptime reaches
1681 * the specified time. Note that timer precision is limited by the
1682 * system tick rate and not the requested timeout value.
1683 *
1684 * @param t Nanosecond uptime value
1685 * @return Timeout delay value
1686 */
1687#define K_TIMEOUT_ABS_NS(t) K_TIMEOUT_ABS_TICKS(k_ns_to_ticks_ceil64(t))
1688
1689/**
Andy Rosse39bf292020-03-19 10:30:33 -07001690 * @brief Generates an absolute/uptime timeout value from system cycles
Andy Rosse1bc5952020-03-09 12:19:54 -07001691 *
1692 * This macro generates a timeout delay that represents an expiration
1693 * at the absolute uptime value specified, in cycles. That is, the
1694 * timeout will expire immediately after the system uptime reaches the
1695 * specified time. Note that timer precision is limited by the system
1696 * tick rate and not the requested timeout value.
1697 *
1698 * @param t Cycle uptime value
1699 * @return Timeout delay value
1700 */
1701#define K_TIMEOUT_ABS_CYC(t) K_TIMEOUT_ABS_TICKS(k_cyc_to_ticks_ceil64(t))
1702
1703#endif
1704
1705/**
Anas Nashif166f5192018-02-25 08:02:36 -06001706 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001707 */
1708
1709/**
Allan Stephensc98da842016-11-11 15:45:03 -05001710 * @cond INTERNAL_HIDDEN
1711 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001712
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001713struct k_timer {
1714 /*
1715 * _timeout structure must be first here if we want to use
1716 * dynamic timer allocation. timeout.node is used in the double-linked
1717 * list of free timers
1718 */
1719 struct _timeout timeout;
1720
Allan Stephens45bfa372016-10-12 12:39:42 -05001721 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001722 _wait_q_t wait_q;
1723
1724 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001725 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001726
1727 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001728 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001729
1730 /* timer period */
Andy Ross78327382020-03-05 15:18:14 -08001731 k_timeout_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001732
Allan Stephens45bfa372016-10-12 12:39:42 -05001733 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001734 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001735
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001736 /* user-specific data, also used to support legacy features */
1737 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001738
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001739 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001740 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001741};
1742
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001743#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001744 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001745 .timeout = { \
1746 .node = {},\
1747 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001748 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001749 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001750 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001751 .expiry_fn = expiry, \
1752 .stop_fn = stop, \
1753 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001754 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001755 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001756 }
1757
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001758#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001759
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001760/**
Allan Stephensc98da842016-11-11 15:45:03 -05001761 * INTERNAL_HIDDEN @endcond
1762 */
1763
1764/**
1765 * @defgroup timer_apis Timer APIs
1766 * @ingroup kernel_apis
1767 * @{
1768 */
1769
1770/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001771 * @typedef k_timer_expiry_t
1772 * @brief Timer expiry function type.
1773 *
1774 * A timer's expiry function is executed by the system clock interrupt handler
1775 * each time the timer expires. The expiry function is optional, and is only
1776 * invoked if the timer has been initialized with one.
1777 *
1778 * @param timer Address of timer.
1779 *
1780 * @return N/A
1781 */
1782typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1783
1784/**
1785 * @typedef k_timer_stop_t
1786 * @brief Timer stop function type.
1787 *
1788 * A timer's stop function is executed if the timer is stopped prematurely.
1789 * The function runs in the context of the thread that stops the timer.
1790 * The stop function is optional, and is only invoked if the timer has been
1791 * initialized with one.
1792 *
1793 * @param timer Address of timer.
1794 *
1795 * @return N/A
1796 */
1797typedef void (*k_timer_stop_t)(struct k_timer *timer);
1798
1799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001804 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
1806 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * @param expiry_fn Function to invoke each time the timer expires.
1808 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001810#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001811 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001812 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001813
Allan Stephens45bfa372016-10-12 12:39:42 -05001814/**
1815 * @brief Initialize a timer.
1816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001818 *
1819 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * @param expiry_fn Function to invoke each time the timer expires.
1821 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001822 *
1823 * @return N/A
1824 */
1825extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001826 k_timer_expiry_t expiry_fn,
1827 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001828
Allan Stephens45bfa372016-10-12 12:39:42 -05001829/**
1830 * @brief Start a timer.
1831 *
1832 * This routine starts a timer, and resets its status to zero. The timer
1833 * begins counting down using the specified duration and period values.
1834 *
1835 * Attempting to start a timer that is already running is permitted.
1836 * The timer's status is reset to zero and the timer begins counting down
1837 * using the new duration and period values.
1838 *
1839 * @param timer Address of timer.
Andy Ross78327382020-03-05 15:18:14 -08001840 * @param duration Initial timer duration.
1841 * @param period Timer period.
Allan Stephens45bfa372016-10-12 12:39:42 -05001842 *
1843 * @return N/A
1844 */
Andrew Boiea354d492017-09-29 16:22:28 -07001845__syscall void k_timer_start(struct k_timer *timer,
Andy Ross78327382020-03-05 15:18:14 -08001846 k_timeout_t duration, k_timeout_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001847
1848/**
1849 * @brief Stop a timer.
1850 *
1851 * This routine stops a running timer prematurely. The timer's stop function,
1852 * if one exists, is invoked by the caller.
1853 *
1854 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001855 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001856 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001857 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1858 * if @a k_timer_stop is to be called from ISRs.
1859 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001860 * @param timer Address of timer.
1861 *
1862 * @return N/A
1863 */
Andrew Boiea354d492017-09-29 16:22:28 -07001864__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001865
1866/**
1867 * @brief Read timer status.
1868 *
1869 * This routine reads the timer's status, which indicates the number of times
1870 * it has expired since its status was last read.
1871 *
1872 * Calling this routine resets the timer's status to zero.
1873 *
1874 * @param timer Address of timer.
1875 *
1876 * @return Timer status.
1877 */
Andrew Boiea354d492017-09-29 16:22:28 -07001878__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001879
1880/**
1881 * @brief Synchronize thread to timer expiration.
1882 *
1883 * This routine blocks the calling thread until the timer's status is non-zero
1884 * (indicating that it has expired at least once since it was last examined)
1885 * or the timer is stopped. If the timer status is already non-zero,
1886 * or the timer is already stopped, the caller continues without waiting.
1887 *
1888 * Calling this routine resets the timer's status to zero.
1889 *
1890 * This routine must not be used by interrupt handlers, since they are not
1891 * allowed to block.
1892 *
1893 * @param timer Address of timer.
1894 *
1895 * @return Timer status.
1896 */
Andrew Boiea354d492017-09-29 16:22:28 -07001897__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001898
Andy Ross5a5d3da2020-03-09 13:59:15 -07001899#ifdef CONFIG_SYS_CLOCK_EXISTS
1900
1901/**
Andy Rosse39bf292020-03-19 10:30:33 -07001902 * @brief Get next expiration time of a timer, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -07001903 *
1904 * This routine returns the future system uptime reached at the next
1905 * time of expiration of the timer, in units of system ticks. If the
1906 * timer is not running, current system time is returned.
1907 *
1908 * @param timer The timer object
1909 * @return Uptime of expiration, in ticks
1910 */
1911__syscall k_ticks_t k_timer_expires_ticks(struct k_timer *timer);
1912
1913static inline k_ticks_t z_impl_k_timer_expires_ticks(struct k_timer *timer)
1914{
1915 return z_timeout_expires(&timer->timeout);
1916}
1917
1918/**
Andy Rosse39bf292020-03-19 10:30:33 -07001919 * @brief Get time remaining before a timer next expires, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -07001920 *
1921 * This routine computes the time remaining before a running timer
1922 * next expires, in units of system ticks. If the timer is not
1923 * running, it returns zero.
1924 */
1925__syscall k_ticks_t k_timer_remaining_ticks(struct k_timer *timer);
1926
1927static inline k_ticks_t z_impl_k_timer_remaining_ticks(struct k_timer *timer)
1928{
1929 return z_timeout_remaining(&timer->timeout);
1930}
Andy Ross52e444b2018-09-28 09:06:37 -07001931
Allan Stephens45bfa372016-10-12 12:39:42 -05001932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001934 *
1935 * This routine computes the (approximate) time remaining before a running
1936 * timer next expires. If the timer is not running, it returns zero.
1937 *
1938 * @param timer Address of timer.
1939 *
1940 * @return Remaining time (in milliseconds).
1941 */
Andy Ross5a5d3da2020-03-09 13:59:15 -07001942static inline u32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001943{
Andy Ross5a5d3da2020-03-09 13:59:15 -07001944 return k_ticks_to_ms_floor32(k_timer_remaining_ticks(timer));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001945}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001946
Andy Ross5a5d3da2020-03-09 13:59:15 -07001947#endif /* CONFIG_SYS_CLOCK_EXISTS */
1948
Allan Stephensc98da842016-11-11 15:45:03 -05001949/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001950 * @brief Associate user-specific data with a timer.
1951 *
1952 * This routine records the @a user_data with the @a timer, to be retrieved
1953 * later.
1954 *
1955 * It can be used e.g. in a timer handler shared across multiple subsystems to
1956 * retrieve data specific to the subsystem this timer is associated with.
1957 *
1958 * @param timer Address of timer.
1959 * @param user_data User data to associate with the timer.
1960 *
1961 * @return N/A
1962 */
Andrew Boiea354d492017-09-29 16:22:28 -07001963__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1964
Anas Nashif954d5502018-02-25 08:37:28 -06001965/**
1966 * @internal
1967 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001968static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001969 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001970{
1971 timer->user_data = user_data;
1972}
1973
1974/**
1975 * @brief Retrieve the user-specific data from a timer.
1976 *
1977 * @param timer Address of timer.
1978 *
1979 * @return The user data.
1980 */
Andrew Boiea354d492017-09-29 16:22:28 -07001981__syscall void *k_timer_user_data_get(struct k_timer *timer);
1982
Patrik Flykt4344e272019-03-08 14:19:05 -07001983static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001984{
1985 return timer->user_data;
1986}
1987
Anas Nashif166f5192018-02-25 08:02:36 -06001988/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001989
Allan Stephensc98da842016-11-11 15:45:03 -05001990/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001991 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001992 * @{
1993 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001994
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001995/**
Andy Rosse39bf292020-03-19 10:30:33 -07001996 * @brief Get system uptime, in system ticks.
Andy Ross914205c2020-03-10 15:26:38 -07001997 *
1998 * This routine returns the elapsed time since the system booted, in
1999 * ticks (c.f. :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC`), which is the
2000 * fundamental unit of resolution of kernel timekeeping.
2001 *
2002 * @return Current uptime in ticks.
2003 */
2004__syscall s64_t k_uptime_ticks(void);
2005
2006/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * This routine returns the elapsed time since the system booted,
2010 * in milliseconds.
2011 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07002012 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07002013 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07002014 * While this function returns time in milliseconds, it does
2015 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07002016 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07002017 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03002018 *
2019 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002020 */
Andy Ross914205c2020-03-10 15:26:38 -07002021static inline s64_t k_uptime_get(void)
2022{
2023 return k_ticks_to_ms_floor64(k_uptime_ticks());
2024}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002025
Ramesh Thomas89ffd442017-02-05 19:37:19 -08002026/**
2027 * @brief Enable clock always on in tickless kernel
2028 *
Andy Ross1db9f182019-06-25 10:09:45 -07002029 * Deprecated. This does nothing (it was always just a hint). This
2030 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
2031 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08002032 *
2033 * @retval prev_status Previous status of always on flag
2034 */
Andy Ross1db9f182019-06-25 10:09:45 -07002035/* LCOV_EXCL_START */
2036__deprecated static inline int k_enable_sys_clock_always_on(void)
2037{
2038 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
2039 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
2040
2041 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
2042}
2043/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08002044
2045/**
2046 * @brief Disable clock always on in tickless kernel
2047 *
Andy Ross1db9f182019-06-25 10:09:45 -07002048 * Deprecated. This does nothing (it was always just a hint). This
2049 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
2050 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08002051 */
Andy Ross1db9f182019-06-25 10:09:45 -07002052/* LCOV_EXCL_START */
2053__deprecated static inline void k_disable_sys_clock_always_on(void)
2054{
2055 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
2056 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
2057}
2058/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08002059
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002060/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002061 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002062 *
Peter Bigota6067a32019-08-28 08:19:26 -05002063 * This routine returns the lower 32 bits of the system uptime in
2064 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002065 *
Peter Bigota6067a32019-08-28 08:19:26 -05002066 * Because correct conversion requires full precision of the system
2067 * clock there is no benefit to using this over k_uptime_get() unless
2068 * you know the application will never run long enough for the system
2069 * clock to approach 2^32 ticks. Calls to this function may involve
2070 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002071 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07002072 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07002073 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07002074 * While this function returns time in milliseconds, it does
2075 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07002076 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07002077 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03002078 *
Peter Bigota6067a32019-08-28 08:19:26 -05002079 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002080 */
Peter Bigota6067a32019-08-28 08:19:26 -05002081static inline u32_t k_uptime_get_32(void)
2082{
2083 return (u32_t)k_uptime_get();
2084}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002085
2086/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002087 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * This routine computes the elapsed time between the current system uptime
2090 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 * @param reftime Pointer to a reference time, which is updated to the current
2093 * uptime upon return.
2094 *
2095 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002096 */
Andy Ross987c0e52018-09-27 16:50:00 -07002097static inline s64_t k_uptime_delta(s64_t *reftime)
2098{
2099 s64_t uptime, delta;
2100
2101 uptime = k_uptime_get();
2102 delta = uptime - *reftime;
2103 *reftime = uptime;
2104
2105 return delta;
2106}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002107
2108/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002111 * This routine computes the elapsed time between the current system uptime
2112 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002113 *
Peter Bigot42f9d142020-03-20 08:39:31 -05002114 * This is a wrapper around k_uptime_delta().
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002116 * @param reftime Pointer to a reference time, which is updated to the current
2117 * uptime upon return.
2118 *
2119 * @return Elapsed time.
Peter Bigot42f9d142020-03-20 08:39:31 -05002120 *
2121 * @deprecated in 2.3 release, replace with k_uptime_delta()
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002122 */
Peter Bigot42f9d142020-03-20 08:39:31 -05002123__deprecated static inline u32_t k_uptime_delta_32(s64_t *reftime)
Andy Ross987c0e52018-09-27 16:50:00 -07002124{
2125 return (u32_t)k_uptime_delta(reftime);
2126}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04002127
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002129 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002131 * This routine returns the current time, as measured by the system's hardware
2132 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002134 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002135 */
Andrew Boie979b17f2019-10-03 15:20:41 -07002136static inline u32_t k_cycle_get_32(void)
2137{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08002138 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07002139}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002140
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002141/**
Anas Nashif166f5192018-02-25 08:02:36 -06002142 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143 */
2144
Allan Stephensc98da842016-11-11 15:45:03 -05002145/**
2146 * @cond INTERNAL_HIDDEN
2147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002149struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002150 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07002151 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03002152 union {
2153 _wait_q_t wait_q;
2154
2155 _POLL_EVENT;
2156 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002157
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002158 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002159 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002160};
2161
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002162#define Z_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002163 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002164 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09002165 .lock = { }, \
2166 { \
2167 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
2168 _POLL_EVENT_OBJ_INIT(obj) \
2169 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002170 _OBJECT_TRACING_INIT \
2171 }
2172
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002173#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO Z_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002174
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002175extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
2176
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002177/**
2178 * INTERNAL_HIDDEN @endcond
2179 */
2180
2181/**
2182 * @defgroup queue_apis Queue APIs
2183 * @ingroup kernel_apis
2184 * @{
2185 */
2186
2187/**
2188 * @brief Initialize a queue.
2189 *
2190 * This routine initializes a queue object, prior to its first use.
2191 *
2192 * @param queue Address of the queue.
2193 *
2194 * @return N/A
2195 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002196__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002197
2198/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002199 * @brief Cancel waiting on a queue.
2200 *
2201 * This routine causes first thread pending on @a queue, if any, to
2202 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03002203 * If the queue is being waited on by k_poll(), it will return with
2204 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
2205 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002206 *
2207 * @note Can be called by ISRs.
2208 *
2209 * @param queue Address of the queue.
2210 *
2211 * @return N/A
2212 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002213__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002214
2215/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002216 * @brief Append an element to the end of a queue.
2217 *
2218 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002219 * aligned on a word boundary, and the first word of the item is reserved
2220 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002221 *
2222 * @note Can be called by ISRs.
2223 *
2224 * @param queue Address of the queue.
2225 * @param data Address of the data item.
2226 *
2227 * @return N/A
2228 */
2229extern void k_queue_append(struct k_queue *queue, void *data);
2230
2231/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002232 * @brief Append an element to a queue.
2233 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002234 * This routine appends a data item to @a queue. There is an implicit memory
2235 * allocation to create an additional temporary bookkeeping data structure from
2236 * the calling thread's resource pool, which is automatically freed when the
2237 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002238 *
2239 * @note Can be called by ISRs.
2240 *
2241 * @param queue Address of the queue.
2242 * @param data Address of the data item.
2243 *
2244 * @retval 0 on success
2245 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2246 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302247__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002248
2249/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002250 * @brief Prepend an element to a queue.
2251 *
2252 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002253 * aligned on a word boundary, and the first word of the item is reserved
2254 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002255 *
2256 * @note Can be called by ISRs.
2257 *
2258 * @param queue Address of the queue.
2259 * @param data Address of the data item.
2260 *
2261 * @return N/A
2262 */
2263extern void k_queue_prepend(struct k_queue *queue, void *data);
2264
2265/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002266 * @brief Prepend an element to a queue.
2267 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002268 * This routine prepends a data item to @a queue. There is an implicit memory
2269 * allocation to create an additional temporary bookkeeping data structure from
2270 * the calling thread's resource pool, which is automatically freed when the
2271 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002272 *
2273 * @note Can be called by ISRs.
2274 *
2275 * @param queue Address of the queue.
2276 * @param data Address of the data item.
2277 *
2278 * @retval 0 on success
2279 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2280 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302281__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002282
2283/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002284 * @brief Inserts an element to a queue.
2285 *
2286 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002287 * data item must be aligned on a word boundary, and the first word of
2288 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002289 *
2290 * @note Can be called by ISRs.
2291 *
2292 * @param queue Address of the queue.
2293 * @param prev Address of the previous data item.
2294 * @param data Address of the data item.
2295 *
2296 * @return N/A
2297 */
2298extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2299
2300/**
2301 * @brief Atomically append a list of elements to a queue.
2302 *
2303 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002304 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002305 * in each data item pointing to the next data item; the list must be
2306 * NULL-terminated.
2307 *
2308 * @note Can be called by ISRs.
2309 *
2310 * @param queue Address of the queue.
2311 * @param head Pointer to first node in singly-linked list.
2312 * @param tail Pointer to last node in singly-linked list.
2313 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002314 * @retval 0 on success
2315 * @retval -EINVAL on invalid supplied data
2316 *
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002317 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002318extern int k_queue_append_list(struct k_queue *queue, void *head, void *tail);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002319
2320/**
2321 * @brief Atomically add a list of elements to a queue.
2322 *
2323 * This routine adds a list of data items to @a queue in one operation.
2324 * The data items must be in a singly-linked list implemented using a
2325 * sys_slist_t object. Upon completion, the original list is empty.
2326 *
2327 * @note Can be called by ISRs.
2328 *
2329 * @param queue Address of the queue.
2330 * @param list Pointer to sys_slist_t object.
2331 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002332 * @retval 0 on success
2333 * @retval -EINVAL on invalid data
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002334 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002335extern int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002336
2337/**
2338 * @brief Get an element from a queue.
2339 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002340 * This routine removes first data item from @a queue. The first word of the
2341 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002342 *
2343 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2344 *
2345 * @param queue Address of the queue.
Andy Ross78327382020-03-05 15:18:14 -08002346 * @param timeout Non-negative waiting period to obtain a data item
2347 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002348 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002349 *
2350 * @return Address of the data item if successful; NULL if returned
2351 * without waiting, or waiting period timed out.
2352 */
Andy Ross78327382020-03-05 15:18:14 -08002353__syscall void *k_queue_get(struct k_queue *queue, k_timeout_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002354
2355/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002356 * @brief Remove an element from a queue.
2357 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002358 * This routine removes data item from @a queue. The first word of the
2359 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002360 * rely on sys_slist_find_and_remove which is not a constant time operation.
2361 *
2362 * @note Can be called by ISRs
2363 *
2364 * @param queue Address of the queue.
2365 * @param data Address of the data item.
2366 *
2367 * @return true if data item was removed
2368 */
2369static inline bool k_queue_remove(struct k_queue *queue, void *data)
2370{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002371 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002372}
2373
2374/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002375 * @brief Append an element to a queue only if it's not present already.
2376 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002377 * This routine appends data item to @a queue. The first word of the data
2378 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002379 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2380 *
2381 * @note Can be called by ISRs
2382 *
2383 * @param queue Address of the queue.
2384 * @param data Address of the data item.
2385 *
2386 * @return true if data item was added, false if not
2387 */
2388static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2389{
2390 sys_sfnode_t *test;
2391
2392 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2393 if (test == (sys_sfnode_t *) data) {
2394 return false;
2395 }
2396 }
2397
2398 k_queue_append(queue, data);
2399 return true;
2400}
2401
2402/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002403 * @brief Query a queue to see if it has data available.
2404 *
2405 * Note that the data might be already gone by the time this function returns
2406 * if other threads are also trying to read from the queue.
2407 *
2408 * @note Can be called by ISRs.
2409 *
2410 * @param queue Address of the queue.
2411 *
2412 * @return Non-zero if the queue is empty.
2413 * @return 0 if data is available.
2414 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002415__syscall int k_queue_is_empty(struct k_queue *queue);
2416
Patrik Flykt4344e272019-03-08 14:19:05 -07002417static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002418{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002419 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002420}
2421
2422/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002423 * @brief Peek element at the head of queue.
2424 *
2425 * Return element from the head of queue without removing it.
2426 *
2427 * @param queue Address of the queue.
2428 *
2429 * @return Head element, or NULL if queue is empty.
2430 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002431__syscall void *k_queue_peek_head(struct k_queue *queue);
2432
Patrik Flykt4344e272019-03-08 14:19:05 -07002433static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002434{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002435 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002436}
2437
2438/**
2439 * @brief Peek element at the tail of queue.
2440 *
2441 * Return element from the tail of queue without removing it.
2442 *
2443 * @param queue Address of the queue.
2444 *
2445 * @return Tail element, or NULL if queue is empty.
2446 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002447__syscall void *k_queue_peek_tail(struct k_queue *queue);
2448
Patrik Flykt4344e272019-03-08 14:19:05 -07002449static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002450{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002451 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002452}
2453
2454/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002455 * @brief Statically define and initialize a queue.
2456 *
2457 * The queue can be accessed outside the module where it is defined using:
2458 *
2459 * @code extern struct k_queue <name>; @endcode
2460 *
2461 * @param name Name of the queue.
2462 */
2463#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002464 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002465 Z_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002466
Anas Nashif166f5192018-02-25 08:02:36 -06002467/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002468
Wentong Wu5611e922019-06-20 23:51:27 +08002469#ifdef CONFIG_USERSPACE
2470/**
2471 * @brief futex structure
2472 *
2473 * A k_futex is a lightweight mutual exclusion primitive designed
2474 * to minimize kernel involvement. Uncontended operation relies
2475 * only on atomic access to shared memory. k_futex are tracked as
2476 * kernel objects and can live in user memory so any access bypass
2477 * the kernel object permission management mechanism.
2478 */
2479struct k_futex {
2480 atomic_t val;
2481};
2482
2483/**
2484 * @brief futex kernel data structure
2485 *
2486 * z_futex_data are the helper data structure for k_futex to complete
2487 * futex contended operation on kernel side, structure z_futex_data
2488 * of every futex object is invisible in user mode.
2489 */
2490struct z_futex_data {
2491 _wait_q_t wait_q;
2492 struct k_spinlock lock;
2493};
2494
2495#define Z_FUTEX_DATA_INITIALIZER(obj) \
2496 { \
2497 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2498 }
2499
2500/**
2501 * @defgroup futex_apis FUTEX APIs
2502 * @ingroup kernel_apis
2503 * @{
2504 */
2505
2506/**
Wentong Wu5611e922019-06-20 23:51:27 +08002507 * @brief Pend the current thread on a futex
2508 *
2509 * Tests that the supplied futex contains the expected value, and if so,
2510 * goes to sleep until some other thread calls k_futex_wake() on it.
2511 *
2512 * @param futex Address of the futex.
2513 * @param expected Expected value of the futex, if it is different the caller
2514 * will not wait on it.
Andy Ross78327382020-03-05 15:18:14 -08002515 * @param timeout Non-negative waiting period on the futex, or
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002516 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002517 * @retval -EACCES Caller does not have read access to futex address.
2518 * @retval -EAGAIN If the futex value did not match the expected parameter.
2519 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2520 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2521 * @retval 0 if the caller went to sleep and was woken up. The caller
2522 * should check the futex's value on wakeup to determine if it needs
2523 * to block again.
2524 */
Andy Ross78327382020-03-05 15:18:14 -08002525__syscall int k_futex_wait(struct k_futex *futex, int expected,
2526 k_timeout_t timeout);
Wentong Wu5611e922019-06-20 23:51:27 +08002527
2528/**
2529 * @brief Wake one/all threads pending on a futex
2530 *
2531 * Wake up the highest priority thread pending on the supplied futex, or
2532 * wakeup all the threads pending on the supplied futex, and the behavior
2533 * depends on wake_all.
2534 *
2535 * @param futex Futex to wake up pending threads.
2536 * @param wake_all If true, wake up all pending threads; If false,
2537 * wakeup the highest priority thread.
2538 * @retval -EACCES Caller does not have access to the futex address.
2539 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2540 * @retval Number of threads that were woken up.
2541 */
2542__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2543
2544/** @} */
2545#endif
2546
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002548 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549};
2550
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002551/**
2552 * @cond INTERNAL_HIDDEN
2553 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002554#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002555 { \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002556 ._queue = Z_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002557 }
2558
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002559#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002560
Allan Stephensc98da842016-11-11 15:45:03 -05002561/**
2562 * INTERNAL_HIDDEN @endcond
2563 */
2564
2565/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002566 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002567 * @ingroup kernel_apis
2568 * @{
2569 */
2570
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002571/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002572 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002573 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002574 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002576 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002577 *
2578 * @return N/A
2579 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002580#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002581 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002582
2583/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002584 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002585 *
2586 * This routine causes first thread pending on @a fifo, if any, to
2587 * return from k_fifo_get() call with NULL value (as if timeout
2588 * expired).
2589 *
2590 * @note Can be called by ISRs.
2591 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002592 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002593 *
2594 * @return N/A
2595 */
2596#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002597 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002598
2599/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002600 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002602 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002603 * aligned on a word boundary, and the first word of the item is reserved
2604 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002606 * @note Can be called by ISRs.
2607 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002608 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002609 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002610 *
2611 * @return N/A
2612 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002613#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002614 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002615
2616/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002617 * @brief Add an element to a FIFO queue.
2618 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002619 * This routine adds a data item to @a fifo. There is an implicit memory
2620 * allocation to create an additional temporary bookkeeping data structure from
2621 * the calling thread's resource pool, which is automatically freed when the
2622 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002623 *
2624 * @note Can be called by ISRs.
2625 *
2626 * @param fifo Address of the FIFO.
2627 * @param data Address of the data item.
2628 *
2629 * @retval 0 on success
2630 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2631 */
2632#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002633 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002634
2635/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002636 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002639 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002640 * each data item pointing to the next data item; the list must be
2641 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002645 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @param head Pointer to first node in singly-linked list.
2647 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002648 *
2649 * @return N/A
2650 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002651#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002652 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002653
2654/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002655 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * This routine adds a list of data items to @a fifo in one operation.
2658 * The data items must be in a singly-linked list implemented using a
2659 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660 * and must be re-initialized via sys_slist_init().
2661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 * @note Can be called by ISRs.
2663 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002664 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
2667 * @return N/A
2668 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002669#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002670 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002671
2672/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002673 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002676 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002678 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2679 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002680 * @param fifo Address of the FIFO queue.
Andy Ross78327382020-03-05 15:18:14 -08002681 * @param timeout Waiting period to obtain a data item,
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002682 * or one of the special values K_NO_WAIT and K_FOREVER.
2683 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002684 * @return Address of the data item if successful; NULL if returned
2685 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002687#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002688 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002691 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002692 *
2693 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002694 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002695 *
2696 * @note Can be called by ISRs.
2697 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002698 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002699 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002700 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002701 * @return 0 if data is available.
2702 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002703#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002704 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002705
2706/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002707 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002708 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002709 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302710 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002711 * on each iteration of processing, a head container will be peeked,
2712 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002713 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002714 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002715 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002716 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002717 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002718 */
2719#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002720 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002721
2722/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002723 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002724 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002725 * Return element from the tail of FIFO queue (without removing it). A usecase
2726 * for this is if elements of the FIFO queue are themselves containers. Then
2727 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002728 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002729 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002730 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002731 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002732 */
2733#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002734 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002735
2736/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002737 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002738 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002739 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002741 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002743 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002746 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002747 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748
Anas Nashif166f5192018-02-25 08:02:36 -06002749/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002750
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002752 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753};
2754
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002755/**
2756 * @cond INTERNAL_HIDDEN
2757 */
2758
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002759#define Z_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002760 { \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002761 ._queue = Z_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002762 }
2763
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002764#define K_LIFO_INITIALIZER __DEPRECATED_MACRO Z_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002765
Allan Stephensc98da842016-11-11 15:45:03 -05002766/**
2767 * INTERNAL_HIDDEN @endcond
2768 */
2769
2770/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002771 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002772 * @ingroup kernel_apis
2773 * @{
2774 */
2775
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002776/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002777 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002778 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002779 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002780 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002781 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782 *
2783 * @return N/A
2784 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002785#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002786 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787
2788/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002789 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002790 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002791 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002792 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 * @note Can be called by ISRs.
2796 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002797 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002799 *
2800 * @return N/A
2801 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002802#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002803 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002804
2805/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002806 * @brief Add an element to a LIFO queue.
2807 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002808 * This routine adds a data item to @a lifo. There is an implicit memory
2809 * allocation to create an additional temporary bookkeeping data structure from
2810 * the calling thread's resource pool, which is automatically freed when the
2811 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002812 *
2813 * @note Can be called by ISRs.
2814 *
2815 * @param lifo Address of the LIFO.
2816 * @param data Address of the data item.
2817 *
2818 * @retval 0 on success
2819 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2820 */
2821#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002822 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002823
2824/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002825 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002828 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2831 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002832 * @param lifo Address of the LIFO queue.
Andy Ross78327382020-03-05 15:18:14 -08002833 * @param timeout Waiting period to obtain a data item,
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002834 * or one of the special values K_NO_WAIT and K_FOREVER.
2835 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002836 * @return Address of the data item if successful; NULL if returned
2837 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002838 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002839#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002840 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002842/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002843 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002844 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002845 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002847 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002851#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002852 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002853 Z_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854
Anas Nashif166f5192018-02-25 08:02:36 -06002855/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002856
2857/**
2858 * @cond INTERNAL_HIDDEN
2859 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302860#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002861
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002862typedef uintptr_t stack_data_t;
2863
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864struct k_stack {
2865 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002866 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002867 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002869 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002870 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002871 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872};
2873
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002874#define Z_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002875 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002876 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002877 .base = stack_buffer, \
2878 .next = stack_buffer, \
2879 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002880 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002881 }
2882
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002883#define K_STACK_INITIALIZER __DEPRECATED_MACRO Z_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002884
Allan Stephensc98da842016-11-11 15:45:03 -05002885/**
2886 * INTERNAL_HIDDEN @endcond
2887 */
2888
2889/**
2890 * @defgroup stack_apis Stack APIs
2891 * @ingroup kernel_apis
2892 * @{
2893 */
2894
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895/**
2896 * @brief Initialize a stack.
2897 *
2898 * This routine initializes a stack object, prior to its first use.
2899 *
2900 * @param stack Address of the stack.
2901 * @param buffer Address of array used to hold stacked values.
2902 * @param num_entries Maximum number of values that can be stacked.
2903 *
2904 * @return N/A
2905 */
Andrew Boief3bee952018-05-02 17:44:39 -07002906void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002907 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002908
2909
2910/**
2911 * @brief Initialize a stack.
2912 *
2913 * This routine initializes a stack object, prior to its first use. Internal
2914 * buffers will be allocated from the calling thread's resource pool.
2915 * This memory will be released if k_stack_cleanup() is called, or
2916 * userspace is enabled and the stack object loses all references to it.
2917 *
2918 * @param stack Address of the stack.
2919 * @param num_entries Maximum number of values that can be stacked.
2920 *
2921 * @return -ENOMEM if memory couldn't be allocated
2922 */
2923
Adithya Baglody28080d32018-10-15 11:48:51 +05302924__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2925 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002926
2927/**
2928 * @brief Release a stack's allocated buffer
2929 *
2930 * If a stack object was given a dynamically allocated buffer via
2931 * k_stack_alloc_init(), this will free it. This function does nothing
2932 * if the buffer wasn't dynamically allocated.
2933 *
2934 * @param stack Address of the stack.
Anas Nashif1ed67d12019-06-16 08:58:10 -04002935 * @retval 0 on success
2936 * @retval -EAGAIN when object is still in use
Andrew Boief3bee952018-05-02 17:44:39 -07002937 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002938int k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939
2940/**
2941 * @brief Push an element onto a stack.
2942 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002943 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 *
2945 * @note Can be called by ISRs.
2946 *
2947 * @param stack Address of the stack.
2948 * @param data Value to push onto the stack.
2949 *
Anas Nashif1ed67d12019-06-16 08:58:10 -04002950 * @retval 0 on success
2951 * @retval -ENOMEM if stack is full
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002953__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954
2955/**
2956 * @brief Pop an element from a stack.
2957 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002958 * This routine removes a stack_data_t value from @a stack in a "last in,
2959 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 *
2961 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2962 *
2963 * @param stack Address of the stack.
2964 * @param data Address of area to hold the value popped from the stack.
Andy Ross78327382020-03-05 15:18:14 -08002965 * @param timeout Waiting period to obtain a value,
2966 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002967 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002969 * @retval 0 Element popped from stack.
2970 * @retval -EBUSY Returned without waiting.
2971 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002973__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
Andy Ross78327382020-03-05 15:18:14 -08002974 k_timeout_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002975
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002980 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002981 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @param name Name of the stack.
2984 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002985 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002986#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002987 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002988 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002989 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002990 Z_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002991 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002992
Anas Nashif166f5192018-02-25 08:02:36 -06002993/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002994
Allan Stephens6bba9b02016-11-16 14:56:54 -05002995struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002996struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002997
Piotr Zięcik19d83492019-09-27 09:16:25 +02002998/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002999typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
3000struct _poller {
3001 volatile bool is_polling;
3002 struct k_thread *thread;
3003 _poller_cb_t cb;
3004};
3005
Allan Stephensc98da842016-11-11 15:45:03 -05003006/**
Anas Nashif29f37f02019-01-21 14:30:35 -05003007 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05003008 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009 */
3010
Allan Stephens6bba9b02016-11-16 14:56:54 -05003011/**
3012 * @typedef k_work_handler_t
3013 * @brief Work item handler function type.
3014 *
3015 * A work item's handler function is executed by a workqueue's thread
3016 * when the work item is processed by the workqueue.
3017 *
3018 * @param work Address of the work item.
3019 *
3020 * @return N/A
3021 */
3022typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023
3024/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003025 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05003027
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03003029 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07003030 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003031};
3032
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03003034 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003035};
3036
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03003038 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039 k_work_handler_t handler;
3040 atomic_t flags[1];
3041};
3042
Allan Stephens6bba9b02016-11-16 14:56:54 -05003043struct k_delayed_work {
3044 struct k_work work;
3045 struct _timeout timeout;
3046 struct k_work_q *work_q;
3047};
3048
Piotr Zięcik19d83492019-09-27 09:16:25 +02003049struct k_work_poll {
3050 struct k_work work;
3051 struct _poller poller;
3052 struct k_poll_event *events;
3053 int num_events;
3054 k_work_handler_t real_handler;
3055 struct _timeout timeout;
3056 int poll_result;
3057};
3058
Allan Stephens6bba9b02016-11-16 14:56:54 -05003059extern struct k_work_q k_sys_work_q;
3060
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003062 * INTERNAL_HIDDEN @endcond
3063 */
3064
Patrik Flykt4344e272019-03-08 14:19:05 -07003065#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003066 { \
3067 ._reserved = NULL, \
3068 .handler = work_handler, \
3069 .flags = { 0 } \
3070 }
3071
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003072#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003073
Allan Stephens6bba9b02016-11-16 14:56:54 -05003074/**
3075 * @brief Initialize a statically-defined work item.
3076 *
3077 * This macro can be used to initialize a statically-defined workqueue work
3078 * item, prior to its first use. For example,
3079 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003080 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05003081 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003082 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05003083 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003085#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07003086 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087
3088/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003089 * @brief Initialize a work item.
3090 *
3091 * This routine initializes a workqueue work item, prior to its first use.
3092 *
3093 * @param work Address of work item.
3094 * @param handler Function to invoke each time work item is processed.
3095 *
3096 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 */
3098static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
3099{
Patrik Flykt4344e272019-03-08 14:19:05 -07003100 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101}
3102
3103/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003104 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03003105 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003106 * This routine submits work item @a work to be processed by workqueue
3107 * @a work_q. If the work item is already pending in the workqueue's queue
3108 * as a result of an earlier submission, this routine has no effect on the
3109 * work item. If the work item has already been processed, or is currently
3110 * being processed, its work is considered complete and the work item can be
3111 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03003112 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003113 * @warning
3114 * A submitted work item must not be modified until it has been processed
3115 * by the workqueue.
3116 *
3117 * @note Can be called by ISRs.
3118 *
3119 * @param work_q Address of workqueue.
3120 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03003121 *
3122 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123 */
3124static inline void k_work_submit_to_queue(struct k_work_q *work_q,
3125 struct k_work *work)
3126{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03003127 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03003128 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129 }
3130}
3131
3132/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003133 * @brief Submit a work item to a user mode workqueue
3134 *
David B. Kinder06d78352018-12-17 14:32:40 -08003135 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003136 * memory allocation is made from the caller's resource pool which is freed
3137 * once the worker thread consumes the k_work item. The workqueue
3138 * thread must have memory access to the k_work item being submitted. The caller
3139 * must have permission granted on the work_q parameter's queue object.
3140 *
3141 * Otherwise this works the same as k_work_submit_to_queue().
3142 *
3143 * @note Can be called by ISRs.
3144 *
3145 * @param work_q Address of workqueue.
3146 * @param work Address of work item.
3147 *
3148 * @retval -EBUSY if the work item was already in some workqueue
3149 * @retval -ENOMEM if no memory for thread resource pool allocation
3150 * @retval 0 Success
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003151 */
3152static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
3153 struct k_work *work)
3154{
3155 int ret = -EBUSY;
3156
3157 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
3158 ret = k_queue_alloc_append(&work_q->queue, work);
3159
3160 /* Couldn't insert into the queue. Clear the pending bit
3161 * so the work item can be submitted again
3162 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08003163 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003164 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
3165 }
3166 }
3167
3168 return ret;
3169}
3170
3171/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003172 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003174 * This routine indicates if work item @a work is pending in a workqueue's
3175 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003176 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003177 * @note Can be called by ISRs.
3178 *
3179 * @param work Address of work item.
3180 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08003181 * @return true if work item is pending, or false if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03003182 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08003183static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03003184{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03003185 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03003186}
3187
3188/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003189 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003190 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003191 * This routine starts workqueue @a work_q. The workqueue spawns its work
3192 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003193 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003194 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07003195 * @param stack Pointer to work queue thread's stack space, as defined by
3196 * K_THREAD_STACK_DEFINE()
3197 * @param stack_size Size of the work queue thread's stack (in bytes), which
3198 * should either be the same constant passed to
3199 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05003200 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003201 *
3202 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203 */
Andrew Boie507852a2017-07-25 18:47:07 -07003204extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07003205 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05003206 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003208/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003209 * @brief Start a workqueue in user mode
3210 *
3211 * This works identically to k_work_q_start() except it is callable from user
3212 * mode, and the worker thread created will run in user mode.
3213 * The caller must have permissions granted on both the work_q parameter's
3214 * thread and queue objects, and the same restrictions on priority apply as
3215 * k_thread_create().
3216 *
3217 * @param work_q Address of workqueue.
3218 * @param stack Pointer to work queue thread's stack space, as defined by
3219 * K_THREAD_STACK_DEFINE()
3220 * @param stack_size Size of the work queue thread's stack (in bytes), which
3221 * should either be the same constant passed to
3222 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
3223 * @param prio Priority of the work queue's thread.
3224 *
3225 * @return N/A
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003226 */
3227extern void k_work_q_user_start(struct k_work_q *work_q,
3228 k_thread_stack_t *stack,
3229 size_t stack_size, int prio);
3230
3231/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003232 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003233 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003234 * This routine initializes a workqueue delayed work item, prior to
3235 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003236 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003237 * @param work Address of delayed work item.
3238 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003239 *
3240 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003242extern void k_delayed_work_init(struct k_delayed_work *work,
3243 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244
3245/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003246 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003248 * This routine schedules work item @a work to be processed by workqueue
3249 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003250 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003251 * Only when the countdown completes is the work item actually submitted to
3252 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003253 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003254 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08003255 * counting down cancels the existing submission and restarts the
3256 * countdown using the new delay. Note that this behavior is
3257 * inherently subject to race conditions with the pre-existing
3258 * timeouts and work queue, so care must be taken to synchronize such
3259 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003261 * @warning
3262 * A delayed work item must not be modified until it has been processed
3263 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003265 * @note Can be called by ISRs.
3266 *
3267 * @param work_q Address of workqueue.
3268 * @param work Address of delayed work item.
Andy Ross78327382020-03-05 15:18:14 -08003269 * @param delay Delay before submitting the work item
Allan Stephens6bba9b02016-11-16 14:56:54 -05003270 *
3271 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003272 * @retval -EINVAL Work item is being processed or has completed its work.
3273 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003275extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3276 struct k_delayed_work *work,
Andy Ross78327382020-03-05 15:18:14 -08003277 k_timeout_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003278
3279/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003280 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003282 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003283 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003284 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003286 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003288 * @note The result of calling this on a k_delayed_work item that has
3289 * not been submitted (i.e. before the return of the
3290 * k_delayed_work_submit_to_queue() call) is undefined.
3291 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003292 * @param work Address of delayed work item.
3293 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003294 * @retval 0 Work item countdown canceled.
Luiz Augusto von Dentz038d7272020-02-13 11:57:38 -08003295 * @retval -EINVAL Work item is being processed.
3296 * @retval -EALREADY Work item has already been completed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003297 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003298extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003300/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 * @brief Submit a work item to the system workqueue.
3302 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003303 * This routine submits work item @a work to be processed by the system
3304 * workqueue. If the work item is already pending in the workqueue's queue
3305 * as a result of an earlier submission, this routine has no effect on the
3306 * work item. If the work item has already been processed, or is currently
3307 * being processed, its work is considered complete and the work item can be
3308 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003310 * @warning
3311 * Work items submitted to the system workqueue should avoid using handlers
3312 * that block or yield since this may prevent the system workqueue from
3313 * processing other work items in a timely manner.
3314 *
3315 * @note Can be called by ISRs.
3316 *
3317 * @param work Address of work item.
3318 *
3319 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003320 */
3321static inline void k_work_submit(struct k_work *work)
3322{
3323 k_work_submit_to_queue(&k_sys_work_q, work);
3324}
3325
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003326/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327 * @brief Submit a delayed work item to the system workqueue.
3328 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003329 * This routine schedules work item @a work to be processed by the system
3330 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003331 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003332 * Only when the countdown completes is the work item actually submitted to
3333 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003335 * Submitting a previously submitted delayed work item that is still
3336 * counting down cancels the existing submission and restarts the countdown
3337 * using the new delay. If the work item is currently pending on the
3338 * workqueue's queue because the countdown has completed it is too late to
3339 * resubmit the item, and resubmission fails without impacting the work item.
3340 * If the work item has already been processed, or is currently being processed,
3341 * its work is considered complete and the work item can be resubmitted.
3342 *
3343 * @warning
3344 * Work items submitted to the system workqueue should avoid using handlers
3345 * that block or yield since this may prevent the system workqueue from
3346 * processing other work items in a timely manner.
3347 *
3348 * @note Can be called by ISRs.
3349 *
3350 * @param work Address of delayed work item.
Andy Ross78327382020-03-05 15:18:14 -08003351 * @param delay Delay before submitting the work item
Allan Stephens6bba9b02016-11-16 14:56:54 -05003352 *
3353 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003354 * @retval -EINVAL Work item is being processed or has completed its work.
3355 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356 */
3357static inline int k_delayed_work_submit(struct k_delayed_work *work,
Andy Ross78327382020-03-05 15:18:14 -08003358 k_timeout_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003360 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361}
3362
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363/**
Andy Ross5a5d3da2020-03-09 13:59:15 -07003364 * @brief Get time when a delayed work will be scheduled
3365 *
3366 * This routine computes the system uptime when a delayed work gets
3367 * executed. If the delayed work is not waiting to be scheduled, it
3368 * returns current system time.
3369 *
3370 * @param work Delayed work item.
3371 *
3372 * @return Uptime of execution (in ticks).
Andy Ross5a5d3da2020-03-09 13:59:15 -07003373 */
3374static inline k_ticks_t k_delayed_work_expires_ticks(
3375 struct k_delayed_work *work)
3376{
3377 return z_timeout_expires(&work->timeout);
3378}
3379
3380/**
Andy Rosse39bf292020-03-19 10:30:33 -07003381 * @brief Get time remaining before a delayed work gets scheduled, in
3382 * system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -07003383 *
3384 * This routine computes the time remaining before a delayed work gets
3385 * executed. If the delayed work is not waiting to be scheduled, it
3386 * returns zero.
3387 *
3388 * @param work Delayed work item.
3389 *
3390 * @return Remaining time (in ticks).
Andy Ross5a5d3da2020-03-09 13:59:15 -07003391 */
3392static inline k_ticks_t k_delayed_work_remaining_ticks(
3393 struct k_delayed_work *work)
3394{
3395 return z_timeout_remaining(&work->timeout);
3396}
3397
3398/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003399 * @brief Get time remaining before a delayed work gets scheduled.
3400 *
3401 * This routine computes the (approximate) time remaining before a
3402 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003403 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003404 *
3405 * @param work Delayed work item.
3406 *
3407 * @return Remaining time (in milliseconds).
3408 */
Kumar Galacc334c72017-04-21 10:55:34 -05003409static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003410{
Andy Ross5a5d3da2020-03-09 13:59:15 -07003411 return k_ticks_to_ms_floor32(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003412}
3413
Piotr Zięcik19d83492019-09-27 09:16:25 +02003414/**
3415 * @brief Initialize a triggered work item.
3416 *
3417 * This routine initializes a workqueue triggered work item, prior to
3418 * its first use.
3419 *
3420 * @param work Address of triggered work item.
3421 * @param handler Function to invoke each time work item is processed.
3422 *
3423 * @return N/A
3424 */
3425extern void k_work_poll_init(struct k_work_poll *work,
3426 k_work_handler_t handler);
3427
3428/**
3429 * @brief Submit a triggered work item.
3430 *
3431 * This routine schedules work item @a work to be processed by workqueue
3432 * @a work_q when one of the given @a events is signaled. The routine
3433 * initiates internal poller for the work item and then returns to the caller.
3434 * Only when one of the watched events happen the work item is actually
3435 * submitted to the workqueue and becomes pending.
3436 *
3437 * Submitting a previously submitted triggered work item that is still
3438 * waiting for the event cancels the existing submission and reschedules it
3439 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003440 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003441 * so care must be taken to synchronize such resubmissions externally.
3442 *
3443 * @note Can be called by ISRs.
3444 *
3445 * @warning
3446 * Provided array of events as well as a triggered work item must be placed
3447 * in persistent memory (valid until work handler execution or work
3448 * cancellation) and cannot be modified after submission.
3449 *
3450 * @param work_q Address of workqueue.
3451 * @param work Address of delayed work item.
3452 * @param events An array of pointers to events which trigger the work.
3453 * @param num_events The number of events in the array.
Andy Ross78327382020-03-05 15:18:14 -08003454 * @param timeout Timeout after which the work will be scheduled
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003455 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003456 *
3457 *
3458 * @retval 0 Work item started watching for events.
3459 * @retval -EINVAL Work item is being processed or has completed its work.
3460 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3461 */
3462extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3463 struct k_work_poll *work,
3464 struct k_poll_event *events,
3465 int num_events,
Andy Ross78327382020-03-05 15:18:14 -08003466 k_timeout_t timeout);
Piotr Zięcik19d83492019-09-27 09:16:25 +02003467
3468/**
3469 * @brief Submit a triggered work item to the system workqueue.
3470 *
3471 * This routine schedules work item @a work to be processed by system
3472 * workqueue when one of the given @a events is signaled. The routine
3473 * initiates internal poller for the work item and then returns to the caller.
3474 * Only when one of the watched events happen the work item is actually
3475 * submitted to the workqueue and becomes pending.
3476 *
3477 * Submitting a previously submitted triggered work item that is still
3478 * waiting for the event cancels the existing submission and reschedules it
3479 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003480 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003481 * so care must be taken to synchronize such resubmissions externally.
3482 *
3483 * @note Can be called by ISRs.
3484 *
3485 * @warning
3486 * Provided array of events as well as a triggered work item must not be
3487 * modified until the item has been processed by the workqueue.
3488 *
3489 * @param work Address of delayed work item.
3490 * @param events An array of pointers to events which trigger the work.
3491 * @param num_events The number of events in the array.
Andy Ross78327382020-03-05 15:18:14 -08003492 * @param timeout Timeout after which the work will be scheduled
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003493 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003494 *
3495 * @retval 0 Work item started watching for events.
3496 * @retval -EINVAL Work item is being processed or has completed its work.
3497 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3498 */
3499static inline int k_work_poll_submit(struct k_work_poll *work,
3500 struct k_poll_event *events,
3501 int num_events,
Andy Ross78327382020-03-05 15:18:14 -08003502 k_timeout_t timeout)
Piotr Zięcik19d83492019-09-27 09:16:25 +02003503{
3504 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3505 events, num_events, timeout);
3506}
3507
3508/**
3509 * @brief Cancel a triggered work item.
3510 *
3511 * This routine cancels the submission of triggered work item @a work.
3512 * A triggered work item can only be canceled if no event triggered work
3513 * submission.
3514 *
3515 * @note Can be called by ISRs.
3516 *
3517 * @param work Address of delayed work item.
3518 *
David B. Kinder73896c02019-10-28 16:27:57 -07003519 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003520 * @retval -EINVAL Work item is being processed or has completed its work.
3521 */
3522extern int k_work_poll_cancel(struct k_work_poll *work);
3523
Anas Nashif166f5192018-02-25 08:02:36 -06003524/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003525/**
Anas Nashifce78d162018-05-24 12:43:11 -05003526 * @defgroup mutex_apis Mutex APIs
3527 * @ingroup kernel_apis
3528 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003529 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003530
Anas Nashifce78d162018-05-24 12:43:11 -05003531/**
3532 * Mutex Structure
3533 * @ingroup mutex_apis
3534 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003535struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05003536 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003537 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003538 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003539 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05003540
3541 /** Current lock count */
Kumar Galacc334c72017-04-21 10:55:34 -05003542 u32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05003543
3544 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003545 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003547 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003548 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003549};
3550
Anas Nashifce78d162018-05-24 12:43:11 -05003551/**
3552 * @cond INTERNAL_HIDDEN
3553 */
Anas Nashif45a1d8a2020-04-24 11:29:17 -04003554#define Z_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003555 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003556 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557 .owner = NULL, \
3558 .lock_count = 0, \
3559 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003560 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561 }
3562
Anas Nashif45a1d8a2020-04-24 11:29:17 -04003563#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO Z_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003564
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003565/**
Allan Stephensc98da842016-11-11 15:45:03 -05003566 * INTERNAL_HIDDEN @endcond
3567 */
3568
3569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003573 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003574 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003576 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003577 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003578#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003579 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04003580 Z_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003581
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003582/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003587 * Upon completion, the mutex is available and does not have an owner.
3588 *
3589 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003590 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003591 * @retval 0 Mutex object created
3592 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003593 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003594__syscall int k_mutex_init(struct k_mutex *mutex);
3595
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003596
3597/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003600 * This routine locks @a mutex. If the mutex is locked by another thread,
3601 * the calling thread waits until the mutex becomes available or until
3602 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * A thread is permitted to lock a mutex it has already locked. The operation
3605 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003607 * @param mutex Address of the mutex.
Andy Ross78327382020-03-05 15:18:14 -08003608 * @param timeout Waiting period to lock the mutex,
3609 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003610 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003611 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003612 * @retval 0 Mutex locked.
3613 * @retval -EBUSY Returned without waiting.
3614 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003615 */
Andy Ross78327382020-03-05 15:18:14 -08003616__syscall int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003617
3618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 * This routine unlocks @a mutex. The mutex must already be locked by the
3622 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003623 *
3624 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003626 * thread.
3627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003628 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003629 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003630 * @retval 0 Mutex unlocked.
3631 * @retval -EPERM The current thread does not own the mutex
3632 * @retval -EINVAL The mutex is not locked
3633 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003634 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003635__syscall int k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003636
Allan Stephensc98da842016-11-11 15:45:03 -05003637/**
Anas Nashif166f5192018-02-25 08:02:36 -06003638 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003639 */
3640
3641/**
3642 * @cond INTERNAL_HIDDEN
3643 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003644
3645struct k_sem {
3646 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303647 u32_t count;
3648 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003649 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003650
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003651 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003652 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003653};
3654
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003655#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003656 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003657 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003658 .count = initial_count, \
3659 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003660 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003661 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003662 }
3663
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003664#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003665
Allan Stephensc98da842016-11-11 15:45:03 -05003666/**
3667 * INTERNAL_HIDDEN @endcond
3668 */
3669
3670/**
3671 * @defgroup semaphore_apis Semaphore APIs
3672 * @ingroup kernel_apis
3673 * @{
3674 */
3675
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003677 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003679 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003681 * @param sem Address of the semaphore.
3682 * @param initial_count Initial semaphore count.
3683 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003684 *
Anas Nashif928af3c2019-05-04 10:36:14 -04003685 * @retval 0 Semaphore created successfully
3686 * @retval -EINVAL Invalid values
3687 *
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003688 */
Anas Nashif928af3c2019-05-04 10:36:14 -04003689__syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count,
Andrew Boie99280232017-09-29 14:17:47 -07003690 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003691
3692/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003693 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003695 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003697 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3698 *
3699 * @param sem Address of the semaphore.
Andy Ross78327382020-03-05 15:18:14 -08003700 * @param timeout Waiting period to take the semaphore,
3701 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003702 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003703 * @retval 0 Semaphore taken.
3704 * @retval -EBUSY Returned without waiting.
3705 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003706 */
Andy Ross78327382020-03-05 15:18:14 -08003707__syscall int k_sem_take(struct k_sem *sem, k_timeout_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003708
3709/**
3710 * @brief Give a semaphore.
3711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003712 * This routine gives @a sem, unless the semaphore is already at its maximum
3713 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003715 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003718 *
3719 * @return N/A
3720 */
Andrew Boie99280232017-09-29 14:17:47 -07003721__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003722
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003723/**
3724 * @brief Reset a semaphore's count to zero.
3725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003726 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003728 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003729 *
3730 * @return N/A
3731 */
Andrew Boie990bf162017-10-03 12:36:49 -07003732__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003733
Anas Nashif954d5502018-02-25 08:37:28 -06003734/**
3735 * @internal
3736 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003737static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738{
Patrik Flykt24d71432019-03-26 19:57:45 -06003739 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003740}
3741
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003742/**
3743 * @brief Get a semaphore's count.
3744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003745 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003747 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003749 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003750 */
Andrew Boie990bf162017-10-03 12:36:49 -07003751__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003752
Anas Nashif954d5502018-02-25 08:37:28 -06003753/**
3754 * @internal
3755 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003756static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003757{
3758 return sem->count;
3759}
3760
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003761/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003762 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003764 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003765 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003766 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003768 * @param name Name of the semaphore.
3769 * @param initial_count Initial semaphore count.
3770 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003771 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003772#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003773 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003774 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303775 BUILD_ASSERT(((count_limit) != 0) && \
3776 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003777
Anas Nashif166f5192018-02-25 08:02:36 -06003778/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003779
3780/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003781 * @defgroup msgq_apis Message Queue APIs
3782 * @ingroup kernel_apis
3783 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003784 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003786/**
3787 * @brief Message Queue Structure
3788 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003789struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05003790 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003791 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05003792 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07003793 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05003794 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003795 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003796 /** Maximal number of messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003797 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003798 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05003800 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05003802 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003803 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003804 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003805 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003806 /** Number of used messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003807 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003808
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003809 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003810 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05003811
3812 /** Message queue */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003813 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003815/**
3816 * @cond INTERNAL_HIDDEN
3817 */
3818
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819
Anas Nashif45a1d8a2020-04-24 11:29:17 -04003820#define Z_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003822 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003823 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003824 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003825 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003826 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827 .read_ptr = q_buffer, \
3828 .write_ptr = q_buffer, \
3829 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003830 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003831 }
Anas Nashif45a1d8a2020-04-24 11:29:17 -04003832#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO Z_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003833/**
3834 * INTERNAL_HIDDEN @endcond
3835 */
3836
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003837
Andrew Boie0fe789f2018-04-12 18:35:56 -07003838#define K_MSGQ_FLAG_ALLOC BIT(0)
3839
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003840/**
3841 * @brief Message Queue Attributes
3842 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303843struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05003844 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303845 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003846 /** Maximal number of messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303847 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003848 /** Used messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303849 u32_t used_msgs;
3850};
3851
Allan Stephensc98da842016-11-11 15:45:03 -05003852
3853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003854 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003856 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3857 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003858 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3859 * message is similarly aligned to this boundary, @a q_msg_size must also be
3860 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * The message queue can be accessed outside the module where it is defined
3863 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003864 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003865 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003867 * @param q_name Name of the message queue.
3868 * @param q_msg_size Message size (in bytes).
3869 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003870 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003871 *
Peter Mitsis1da807e2016-10-06 11:36:59 -04003872 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003873#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3874 static char __noinit __aligned(q_align) \
3875 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3876 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04003877 Z_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003878 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003879
Peter Mitsisd7a37502016-10-13 11:37:40 -04003880/**
3881 * @brief Initialize a message queue.
3882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003883 * This routine initializes a message queue object, prior to its first use.
3884 *
Allan Stephensda827222016-11-09 14:23:58 -06003885 * The message queue's ring buffer must contain space for @a max_msgs messages,
3886 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3887 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3888 * that each message is similarly aligned to this boundary, @a q_msg_size
3889 * must also be a multiple of N.
3890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003891 * @param q Address of the message queue.
3892 * @param buffer Pointer to ring buffer that holds queued messages.
3893 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003894 * @param max_msgs Maximum number of messages that can be queued.
3895 *
3896 * @return N/A
3897 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003898void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3899 u32_t max_msgs);
3900
3901/**
3902 * @brief Initialize a message queue.
3903 *
3904 * This routine initializes a message queue object, prior to its first use,
3905 * allocating its internal ring buffer from the calling thread's resource
3906 * pool.
3907 *
3908 * Memory allocated for the ring buffer can be released by calling
3909 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3910 * all of its references.
3911 *
Anas Nashif4b386592019-11-25 09:30:47 -05003912 * @param msgq Address of the message queue.
Andrew Boie0fe789f2018-04-12 18:35:56 -07003913 * @param msg_size Message size (in bytes).
3914 * @param max_msgs Maximum number of messages that can be queued.
3915 *
3916 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3917 * thread's resource pool, or -EINVAL if the size parameters cause
3918 * an integer overflow.
3919 */
Anas Nashif4b386592019-11-25 09:30:47 -05003920__syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size,
Andrew Boie0fe789f2018-04-12 18:35:56 -07003921 u32_t max_msgs);
3922
Anas Nashife71293e2019-12-04 20:00:14 -05003923/**
Anas Nashif4b386592019-11-25 09:30:47 -05003924 * @brief Release allocated buffer for a queue
Anas Nashife71293e2019-12-04 20:00:14 -05003925 *
3926 * Releases memory allocated for the ring buffer.
Anas Nashif4b386592019-11-25 09:30:47 -05003927 *
3928 * @param msgq message queue to cleanup
3929 *
Anas Nashif11b93652019-06-16 08:43:48 -04003930 * @retval 0 on success
3931 * @retval -EBUSY Queue not empty
Anas Nashife71293e2019-12-04 20:00:14 -05003932 */
Anas Nashif11b93652019-06-16 08:43:48 -04003933int k_msgq_cleanup(struct k_msgq *msgq);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003934
3935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003938 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003939 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003940 * @note Can be called by ISRs.
3941 *
Anas Nashif4b386592019-11-25 09:30:47 -05003942 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003943 * @param data Pointer to the message.
Andy Ross78327382020-03-05 15:18:14 -08003944 * @param timeout Non-negative waiting period to add the message,
3945 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003946 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003947 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003948 * @retval 0 Message sent.
3949 * @retval -ENOMSG Returned without waiting or queue purged.
3950 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003951 */
Andy Ross78327382020-03-05 15:18:14 -08003952__syscall int k_msgq_put(struct k_msgq *msgq, void *data, k_timeout_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003953
3954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003957 * This routine receives a message from message queue @a q in a "first in,
3958 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003959 *
Allan Stephensc98da842016-11-11 15:45:03 -05003960 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003961 *
Anas Nashif4b386592019-11-25 09:30:47 -05003962 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * @param data Address of area to hold the received message.
Andy Ross78327382020-03-05 15:18:14 -08003964 * @param timeout Waiting period to receive the message,
3965 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003966 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003967 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003968 * @retval 0 Message received.
3969 * @retval -ENOMSG Returned without waiting.
3970 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003971 */
Andy Ross78327382020-03-05 15:18:14 -08003972__syscall int k_msgq_get(struct k_msgq *msgq, void *data, k_timeout_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003973
3974/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003975 * @brief Peek/read a message from a message queue.
3976 *
3977 * This routine reads a message from message queue @a q in a "first in,
3978 * first out" manner and leaves the message in the queue.
3979 *
3980 * @note Can be called by ISRs.
3981 *
Anas Nashif4b386592019-11-25 09:30:47 -05003982 * @param msgq Address of the message queue.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003983 * @param data Address of area to hold the message read from the queue.
3984 *
3985 * @retval 0 Message read.
3986 * @retval -ENOMSG Returned when the queue has no message.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003987 */
Anas Nashif4b386592019-11-25 09:30:47 -05003988__syscall int k_msgq_peek(struct k_msgq *msgq, void *data);
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003989
3990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003991 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003993 * This routine discards all unreceived messages in a message queue's ring
3994 * buffer. Any threads that are blocked waiting to send a message to the
3995 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003996 *
Anas Nashif4b386592019-11-25 09:30:47 -05003997 * @param msgq Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003998 *
3999 * @return N/A
4000 */
Anas Nashif4b386592019-11-25 09:30:47 -05004001__syscall void k_msgq_purge(struct k_msgq *msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004002
Peter Mitsis67be2492016-10-07 11:44:34 -04004003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004004 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04004005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004006 * This routine returns the number of unused entries in a message queue's
4007 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04004008 *
Anas Nashif4b386592019-11-25 09:30:47 -05004009 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004010 *
4011 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04004012 */
Anas Nashif4b386592019-11-25 09:30:47 -05004013__syscall u32_t k_msgq_num_free_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07004014
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304015/**
4016 * @brief Get basic attributes of a message queue.
4017 *
4018 * This routine fetches basic attributes of message queue into attr argument.
4019 *
Anas Nashif4b386592019-11-25 09:30:47 -05004020 * @param msgq Address of the message queue.
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304021 * @param attrs pointer to message queue attribute structure.
4022 *
4023 * @return N/A
4024 */
Anas Nashif4b386592019-11-25 09:30:47 -05004025__syscall void k_msgq_get_attrs(struct k_msgq *msgq,
4026 struct k_msgq_attrs *attrs);
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304027
4028
Anas Nashif4b386592019-11-25 09:30:47 -05004029static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq)
Peter Mitsis67be2492016-10-07 11:44:34 -04004030{
Anas Nashif4b386592019-11-25 09:30:47 -05004031 return msgq->max_msgs - msgq->used_msgs;
Peter Mitsis67be2492016-10-07 11:44:34 -04004032}
4033
Peter Mitsisd7a37502016-10-13 11:37:40 -04004034/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004035 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004037 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004038 *
Anas Nashif4b386592019-11-25 09:30:47 -05004039 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004040 *
4041 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004042 */
Anas Nashif4b386592019-11-25 09:30:47 -05004043__syscall u32_t k_msgq_num_used_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07004044
Anas Nashif4b386592019-11-25 09:30:47 -05004045static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004046{
Anas Nashif4b386592019-11-25 09:30:47 -05004047 return msgq->used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004048}
4049
Anas Nashif166f5192018-02-25 08:02:36 -06004050/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004051
4052/**
Allan Stephensc98da842016-11-11 15:45:03 -05004053 * @defgroup mailbox_apis Mailbox APIs
4054 * @ingroup kernel_apis
4055 * @{
4056 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004057
Anas Nashife71293e2019-12-04 20:00:14 -05004058/**
4059 * @brief Mailbox Message Structure
4060 *
4061 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004062struct k_mbox_msg {
4063 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05004064 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004065 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04004066 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004067 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05004068 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004069 /** sender's message data buffer */
4070 void *tx_data;
4071 /** internal use only - needed for legacy API support */
4072 void *_rx_data;
4073 /** message data block descriptor */
4074 struct k_mem_block tx_block;
4075 /** source thread id */
4076 k_tid_t rx_source_thread;
4077 /** target thread id */
4078 k_tid_t tx_target_thread;
4079 /** internal use only - thread waiting on send (may be a dummy) */
4080 k_tid_t _syncing_thread;
4081#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
4082 /** internal use only - semaphore used during asynchronous send */
4083 struct k_sem *_async_sem;
4084#endif
4085};
Anas Nashife71293e2019-12-04 20:00:14 -05004086/**
4087 * @brief Mailbox Structure
4088 *
4089 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004090struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05004091 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004092 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05004093 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004094 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07004095 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004096
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004097 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004098 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004099};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004100/**
4101 * @cond INTERNAL_HIDDEN
4102 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004103
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004104#define Z_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004106 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
4107 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05004108 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004109 }
4110
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004111#define K_MBOX_INITIALIZER __DEPRECATED_MACRO Z_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004112
Peter Mitsis12092702016-10-14 12:57:23 -04004113/**
Allan Stephensc98da842016-11-11 15:45:03 -05004114 * INTERNAL_HIDDEN @endcond
4115 */
4116
4117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004118 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04004119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004120 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004121 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004122 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004124 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04004125 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004126#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004127 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004128 Z_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004129
Peter Mitsis12092702016-10-14 12:57:23 -04004130/**
4131 * @brief Initialize a mailbox.
4132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004133 * This routine initializes a mailbox object, prior to its first use.
4134 *
4135 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04004136 *
4137 * @return N/A
4138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004139extern void k_mbox_init(struct k_mbox *mbox);
4140
Peter Mitsis12092702016-10-14 12:57:23 -04004141/**
4142 * @brief Send a mailbox message in a synchronous manner.
4143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004144 * This routine sends a message to @a mbox and waits for a receiver to both
4145 * receive and process it. The message data may be in a buffer, in a memory
4146 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04004147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004148 * @param mbox Address of the mailbox.
4149 * @param tx_msg Address of the transmit message descriptor.
Andy Ross78327382020-03-05 15:18:14 -08004150 * @param timeout Waiting period for the message to be received,
4151 * or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004152 * and K_FOREVER. Once the message has been received,
4153 * this routine waits as long as necessary for the message
4154 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04004155 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004156 * @retval 0 Message sent.
4157 * @retval -ENOMSG Returned without waiting.
4158 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04004159 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004160extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Andy Ross78327382020-03-05 15:18:14 -08004161 k_timeout_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004162
Peter Mitsis12092702016-10-14 12:57:23 -04004163/**
4164 * @brief Send a mailbox message in an asynchronous manner.
4165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004166 * This routine sends a message to @a mbox without waiting for a receiver
4167 * to process it. The message data may be in a buffer, in a memory pool block,
4168 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
4169 * will be given when the message has been both received and completely
4170 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04004171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004172 * @param mbox Address of the mailbox.
4173 * @param tx_msg Address of the transmit message descriptor.
4174 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04004175 *
4176 * @return N/A
4177 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004178extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004179 struct k_sem *sem);
4180
Peter Mitsis12092702016-10-14 12:57:23 -04004181/**
4182 * @brief Receive a mailbox message.
4183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004184 * This routine receives a message from @a mbox, then optionally retrieves
4185 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004187 * @param mbox Address of the mailbox.
4188 * @param rx_msg Address of the receive message descriptor.
4189 * @param buffer Address of the buffer to receive data, or NULL to defer data
4190 * retrieval and message disposal until later.
Andy Ross78327382020-03-05 15:18:14 -08004191 * @param timeout Waiting period for a message to be received,
4192 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004193 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004194 * @retval 0 Message received.
4195 * @retval -ENOMSG Returned without waiting.
4196 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04004197 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004198extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Andy Ross78327382020-03-05 15:18:14 -08004199 void *buffer, k_timeout_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004200
4201/**
4202 * @brief Retrieve mailbox message data into a buffer.
4203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004204 * This routine completes the processing of a received message by retrieving
4205 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004206 *
4207 * Alternatively, this routine can be used to dispose of a received message
4208 * without retrieving its data.
4209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004210 * @param rx_msg Address of the receive message descriptor.
4211 * @param buffer Address of the buffer to receive data, or NULL to discard
4212 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04004213 *
4214 * @return N/A
4215 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004216extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04004217
4218/**
4219 * @brief Retrieve mailbox message data into a memory pool block.
4220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004221 * This routine completes the processing of a received message by retrieving
4222 * its data into a memory pool block, then disposing of the message.
4223 * The memory pool block that results from successful retrieval must be
4224 * returned to the pool once the data has been processed, even in cases
4225 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04004226 *
4227 * Alternatively, this routine can be used to dispose of a received message
4228 * without retrieving its data. In this case there is no need to return a
4229 * memory pool block to the pool.
4230 *
4231 * This routine allocates a new memory pool block for the data only if the
4232 * data is not already in one. If a new block cannot be allocated, the routine
4233 * returns a failure code and the received message is left unchanged. This
4234 * permits the caller to reattempt data retrieval at a later time or to dispose
4235 * of the received message without retrieving its data.
4236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004237 * @param rx_msg Address of a receive message descriptor.
4238 * @param pool Address of memory pool, or NULL to discard data.
4239 * @param block Address of the area to hold memory pool block info.
Andy Ross78327382020-03-05 15:18:14 -08004240 * @param timeout Time to wait for a memory pool block,
4241 * or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004242 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004243 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004244 * @retval 0 Data retrieved.
4245 * @retval -ENOMEM Returned without waiting.
4246 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04004247 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004248extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04004249 struct k_mem_pool *pool,
Andy Ross78327382020-03-05 15:18:14 -08004250 struct k_mem_block *block,
4251 k_timeout_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004252
Anas Nashif166f5192018-02-25 08:02:36 -06004253/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004254
4255/**
Anas Nashifce78d162018-05-24 12:43:11 -05004256 * @defgroup pipe_apis Pipe APIs
4257 * @ingroup kernel_apis
4258 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004259 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004260
Anas Nashifce78d162018-05-24 12:43:11 -05004261/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004262struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004263 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4264 size_t size; /**< Buffer size */
4265 size_t bytes_used; /**< # bytes used in buffer */
4266 size_t read_index; /**< Where in buffer to read from */
4267 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004268 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004269
4270 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004271 _wait_q_t readers; /**< Reader wait queue */
4272 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004273 } wait_q;
4274
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004275 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004276 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05004277 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004278};
4279
Anas Nashifce78d162018-05-24 12:43:11 -05004280/**
4281 * @cond INTERNAL_HIDDEN
4282 */
4283#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4284
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004285#define Z_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004286 { \
4287 .buffer = pipe_buffer, \
4288 .size = pipe_buffer_size, \
4289 .bytes_used = 0, \
4290 .read_index = 0, \
4291 .write_index = 0, \
4292 .lock = {}, \
4293 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004294 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4295 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004296 }, \
4297 _OBJECT_TRACING_INIT \
4298 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004299 }
4300
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004301#define K_PIPE_INITIALIZER __DEPRECATED_MACRO Z_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004302
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004303/**
Allan Stephensc98da842016-11-11 15:45:03 -05004304 * INTERNAL_HIDDEN @endcond
4305 */
4306
4307/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004308 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004310 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004311 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004312 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004314 * @param name Name of the pipe.
4315 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4316 * or zero if no ring buffer is used.
4317 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004318 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004319 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004320#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004321 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004322 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004323 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004324 Z_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004325
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004327 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004329 * This routine initializes a pipe object, prior to its first use.
4330 *
4331 * @param pipe Address of the pipe.
4332 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4333 * is used.
4334 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4335 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004336 *
4337 * @return N/A
4338 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004339void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4340
4341/**
4342 * @brief Release a pipe's allocated buffer
4343 *
4344 * If a pipe object was given a dynamically allocated buffer via
4345 * k_pipe_alloc_init(), this will free it. This function does nothing
4346 * if the buffer wasn't dynamically allocated.
4347 *
4348 * @param pipe Address of the pipe.
Anas Nashif361a84d2019-06-16 08:22:08 -04004349 * @retval 0 on success
4350 * @retval -EAGAIN nothing to cleanup
Andrew Boie44fe8122018-04-12 17:38:12 -07004351 */
Anas Nashif361a84d2019-06-16 08:22:08 -04004352int k_pipe_cleanup(struct k_pipe *pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07004353
4354/**
4355 * @brief Initialize a pipe and allocate a buffer for it
4356 *
4357 * Storage for the buffer region will be allocated from the calling thread's
4358 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4359 * or userspace is enabled and the pipe object loses all references to it.
4360 *
4361 * This function should only be called on uninitialized pipe objects.
4362 *
4363 * @param pipe Address of the pipe.
4364 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4365 * buffer is used.
4366 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004367 * @retval -ENOMEM if memory couldn't be allocated
Andrew Boie44fe8122018-04-12 17:38:12 -07004368 */
4369__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004370
4371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004372 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004374 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004376 * @param pipe Address of the pipe.
4377 * @param data Address of data to write.
4378 * @param bytes_to_write Size of data (in bytes).
4379 * @param bytes_written Address of area to hold the number of bytes written.
4380 * @param min_xfer Minimum number of bytes to write.
Andy Ross78327382020-03-05 15:18:14 -08004381 * @param timeout Waiting period to wait for the data to be written,
4382 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004383 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004384 * @retval 0 At least @a min_xfer bytes of data were written.
4385 * @retval -EIO Returned without waiting; zero data bytes were written.
4386 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004387 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004388 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004389__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4390 size_t bytes_to_write, size_t *bytes_written,
Andy Ross78327382020-03-05 15:18:14 -08004391 size_t min_xfer, k_timeout_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004392
4393/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004394 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004396 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004398 * @param pipe Address of the pipe.
4399 * @param data Address to place the data read from pipe.
4400 * @param bytes_to_read Maximum number of data bytes to read.
4401 * @param bytes_read Address of area to hold the number of bytes read.
4402 * @param min_xfer Minimum number of data bytes to read.
Andy Ross78327382020-03-05 15:18:14 -08004403 * @param timeout Waiting period to wait for the data to be read,
4404 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004405 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004406 * @retval 0 At least @a min_xfer bytes of data were read.
Anas Nashif361a84d2019-06-16 08:22:08 -04004407 * @retval -EINVAL invalid parameters supplied
Allan Stephens9ef50f42016-11-16 15:33:31 -05004408 * @retval -EIO Returned without waiting; zero data bytes were read.
4409 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004410 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004411 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004412__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4413 size_t bytes_to_read, size_t *bytes_read,
Andy Ross78327382020-03-05 15:18:14 -08004414 size_t min_xfer, k_timeout_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004415
4416/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004417 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004419 * This routine writes the data contained in a memory block to @a pipe.
4420 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004421 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004423 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004424 * @param block Memory block containing data to send
4425 * @param size Number of data bytes in memory block to send
4426 * @param sem Semaphore to signal upon completion (else NULL)
4427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004428 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004429 */
4430extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4431 size_t size, struct k_sem *sem);
4432
Christopher Friedt3315f8f2020-05-06 18:43:58 -04004433/**
4434 * @brief Query the number of bytes that may be read from @a pipe.
4435 *
4436 * @param pipe Address of the pipe.
4437 *
4438 * @retval a number n such that 0 <= n <= @ref k_pipe.size; the
4439 * result is zero for unbuffered pipes.
4440 */
4441__syscall size_t k_pipe_read_avail(struct k_pipe *pipe);
4442
4443/**
4444 * @brief Query the number of bytes that may be written to @a pipe
4445 *
4446 * @param pipe Address of the pipe.
4447 *
4448 * @retval a number n such that 0 <= n <= @ref k_pipe.size; the
4449 * result is zero for unbuffered pipes.
4450 */
4451__syscall size_t k_pipe_write_avail(struct k_pipe *pipe);
4452
Anas Nashif166f5192018-02-25 08:02:36 -06004453/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004454
Allan Stephensc98da842016-11-11 15:45:03 -05004455/**
4456 * @cond INTERNAL_HIDDEN
4457 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004458
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004459struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004460 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004461 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004462 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004463 char *buffer;
4464 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004465 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004466
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004467 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004468 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004469};
4470
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004471#define Z_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004472 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004473 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004474 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004475 .num_blocks = slab_num_blocks, \
4476 .block_size = slab_block_size, \
4477 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004478 .free_list = NULL, \
4479 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004480 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004481 }
4482
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004483#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO Z_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004484
4485
Peter Mitsis578f9112016-10-07 13:50:31 -04004486/**
Allan Stephensc98da842016-11-11 15:45:03 -05004487 * INTERNAL_HIDDEN @endcond
4488 */
4489
4490/**
4491 * @defgroup mem_slab_apis Memory Slab APIs
4492 * @ingroup kernel_apis
4493 * @{
4494 */
4495
4496/**
Allan Stephensda827222016-11-09 14:23:58 -06004497 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004498 *
Allan Stephensda827222016-11-09 14:23:58 -06004499 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004500 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004501 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4502 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004503 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004504 *
Allan Stephensda827222016-11-09 14:23:58 -06004505 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004506 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004507 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004508 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004510 * @param name Name of the memory slab.
4511 * @param slab_block_size Size of each memory block (in bytes).
4512 * @param slab_num_blocks Number memory blocks.
4513 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04004514 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004515#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004516 char __noinit __aligned(WB_UP(slab_align)) \
4517 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004518 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004519 Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004520 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004521
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004522/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004523 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004525 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004526 *
Allan Stephensda827222016-11-09 14:23:58 -06004527 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4528 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004529 * N-byte boundary matching a word boundary, where N is a power of 2
4530 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004531 * To ensure that each memory block is similarly aligned to this boundary,
4532 * @a slab_block_size must also be a multiple of N.
4533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004534 * @param slab Address of the memory slab.
4535 * @param buffer Pointer to buffer used for the memory blocks.
4536 * @param block_size Size of each memory block (in bytes).
4537 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004538 *
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004539 * @retval 0 on success
4540 * @retval -EINVAL invalid data supplied
4541 *
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004542 */
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004543extern int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004544 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004545
4546/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004547 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004549 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004551 * @param slab Address of the memory slab.
4552 * @param mem Pointer to block address area.
Andy Ross78327382020-03-05 15:18:14 -08004553 * @param timeout Non-negative waiting period to wait for operation to complete.
4554 * Use K_NO_WAIT to return without waiting,
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004555 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004556 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004557 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004558 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004559 * @retval -ENOMEM Returned without waiting.
4560 * @retval -EAGAIN Waiting period timed out.
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004561 * @retval -EINVAL Invalid data supplied
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004562 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004563extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Andy Ross78327382020-03-05 15:18:14 -08004564 k_timeout_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004565
4566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004567 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004569 * This routine releases a previously allocated memory block back to its
4570 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004572 * @param slab Address of the memory slab.
4573 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004574 *
4575 * @return N/A
4576 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004577extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004578
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004579/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004580 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004582 * This routine gets the number of memory blocks that are currently
4583 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004585 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004587 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004588 */
Kumar Galacc334c72017-04-21 10:55:34 -05004589static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004590{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004591 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004592}
4593
Peter Mitsisc001aa82016-10-13 13:53:37 -04004594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004595 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004597 * This routine gets the number of memory blocks that are currently
4598 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004600 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004602 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004603 */
Kumar Galacc334c72017-04-21 10:55:34 -05004604static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004605{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004606 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004607}
4608
Anas Nashif166f5192018-02-25 08:02:36 -06004609/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004610
4611/**
Allan Stephensc98da842016-11-11 15:45:03 -05004612 * @addtogroup mem_pool_apis
4613 * @{
4614 */
4615
4616/**
Andy Ross0dd83b82020-04-03 10:01:03 -07004617 * @brief Initialize a k_heap
4618 *
4619 * This constructs a synchronized k_heap object over a memory region
4620 * specified by the user. Note that while any alignment and size can
4621 * be passed as valid parameters, internal alignment restrictions
4622 * inside the inner sys_heap mean that not all bytes may be usable as
4623 * allocated memory.
4624 *
4625 * @param h Heap struct to initialize
4626 * @param mem Pointer to memory.
4627 * @param bytes Size of memory region, in bytes
4628 */
4629void k_heap_init(struct k_heap *h, void *mem, size_t bytes);
4630
4631/**
4632 * @brief Allocate memory from a k_heap
4633 *
4634 * Allocates and returns a memory buffer from the memory region owned
4635 * by the heap. If no memory is available immediately, the call will
4636 * block for the specified timeout (constructed via the standard
4637 * timeout API, or K_NO_WAIT or K_FOREVER) waiting for memory to be
4638 * freed. If the allocation cannot be performed by the expiration of
4639 * the timeout, NULL will be returned.
4640 *
4641 * @param h Heap from which to allocate
4642 * @param bytes Desired size of block to allocate
4643 * @param timeout How long to wait, or K_NO_WAIT
4644 * @return A pointer to valid heap memory, or NULL
4645 */
4646void *k_heap_alloc(struct k_heap *h, size_t bytes, k_timeout_t timeout);
4647
4648/**
4649 * @brief Free memory allocated by k_heap_alloc()
4650 *
4651 * Returns the specified memory block, which must have been returned
4652 * from k_heap_alloc(), to the heap for use by other callers. Passing
4653 * a NULL block is legal, and has no effect.
4654 *
4655 * @param h Heap to which to return the memory
4656 * @param mem A valid memory block, or NULL
4657 */
4658void k_heap_free(struct k_heap *h, void *mem);
4659
4660/**
4661 * @brief Define a static k_heap
4662 *
4663 * This macro defines and initializes a static memory region and
4664 * k_heap of the requested size. After kernel start, &name can be
4665 * used as if k_heap_init() had been called.
4666 *
4667 * @param name Symbol name for the struct k_heap object
4668 * @param bytes Size of memory region, in bytes
4669 */
4670#define K_HEAP_DEFINE(name, bytes) \
4671 char __aligned(sizeof(void *)) kheap_##name[bytes]; \
4672 Z_STRUCT_SECTION_ITERABLE(k_heap, name) = { \
4673 .heap = { \
4674 .init_mem = kheap_##name, \
4675 .init_bytes = (bytes), \
4676 }, \
4677 }
4678
4679/**
Allan Stephensc98da842016-11-11 15:45:03 -05004680 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004682 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4683 * long. The memory pool allows blocks to be repeatedly partitioned into
4684 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004685 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004686 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004687 * If the pool is to be accessed outside the module where it is defined, it
4688 * can be declared via
4689 *
Andy Ross8f0959c2020-04-03 15:39:25 -07004690 * @note When CONFIG_MEM_POOL_HEAP_BACKEND is enabled, the k_mem_pool
4691 * API is implemented on top of a k_heap, which is a more general
4692 * purpose allocator which does not make the same promises about
4693 * splitting or alignment detailed above. Blocks will be aligned only
4694 * to the 8 byte chunk stride of the underlying heap and may point
4695 * anywhere within the heap; they are not split into four as
4696 * described.
4697 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004698 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004700 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004701 * @param minsz Size of the smallest blocks in the pool (in bytes).
4702 * @param maxsz Size of the largest blocks in the pool (in bytes).
4703 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004704 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004705 */
Andy Rosse96ac902020-03-31 09:23:11 -07004706#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4707 Z_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004708
Peter Mitsis937042c2016-10-13 13:18:26 -04004709/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004710 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004712 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004714 * @param pool Address of the memory pool.
4715 * @param block Pointer to block descriptor for the allocated memory.
4716 * @param size Amount of memory to allocate (in bytes).
Andy Ross78327382020-03-05 15:18:14 -08004717 * @param timeout Waiting period to wait for operation to complete.
4718 * Use K_NO_WAIT to return without waiting,
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004719 * or K_FOREVER to wait as long as necessary.
4720 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004721 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004722 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004723 * @retval -ENOMEM Returned without waiting.
4724 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04004725 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004726extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Andy Ross78327382020-03-05 15:18:14 -08004727 size_t size, k_timeout_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004728
4729/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004730 * @brief Allocate memory from a memory pool with malloc() semantics
4731 *
4732 * Such memory must be released using k_free().
4733 *
4734 * @param pool Address of the memory pool.
4735 * @param size Amount of memory to allocate (in bytes).
4736 * @return Address of the allocated memory if successful, otherwise NULL
4737 */
4738extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4739
4740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004741 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004743 * This routine releases a previously allocated memory block back to its
4744 * memory pool.
4745 *
4746 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004747 *
4748 * @return N/A
4749 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004750extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004751
4752/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004753 * @brief Free memory allocated from a memory pool.
4754 *
4755 * This routine releases a previously allocated memory block back to its
4756 * memory pool
4757 *
4758 * @param id Memory block identifier.
4759 *
4760 * @return N/A
4761 */
4762extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4763
4764/**
Anas Nashif166f5192018-02-25 08:02:36 -06004765 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004766 */
4767
4768/**
4769 * @defgroup heap_apis Heap Memory Pool APIs
4770 * @ingroup kernel_apis
4771 * @{
4772 */
4773
4774/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004775 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004777 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004778 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004780 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004782 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04004783 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004784extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004785
4786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004787 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004788 *
4789 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004790 * returned must have been allocated from the heap memory pool or
4791 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004792 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004793 * If @a ptr is NULL, no operation is performed.
4794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004795 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004796 *
4797 * @return N/A
4798 */
4799extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004800
Allan Stephensc98da842016-11-11 15:45:03 -05004801/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004802 * @brief Allocate memory from heap, array style
4803 *
4804 * This routine provides traditional calloc() semantics. Memory is
4805 * allocated from the heap memory pool and zeroed.
4806 *
4807 * @param nmemb Number of elements in the requested array
4808 * @param size Size of each array element (in bytes).
4809 *
4810 * @return Address of the allocated memory if successful; otherwise NULL.
4811 */
4812extern void *k_calloc(size_t nmemb, size_t size);
4813
Anas Nashif166f5192018-02-25 08:02:36 -06004814/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004815
Benjamin Walshacc68c12017-01-29 18:57:45 -05004816/* polling API - PRIVATE */
4817
Benjamin Walshb0179862017-02-02 16:39:57 -05004818#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004819#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004820#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004821#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004822#endif
4823
Benjamin Walshacc68c12017-01-29 18:57:45 -05004824/* private - types bit positions */
4825enum _poll_types_bits {
4826 /* can be used to ignore an event */
4827 _POLL_TYPE_IGNORE,
4828
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004829 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004830 _POLL_TYPE_SIGNAL,
4831
4832 /* semaphore availability */
4833 _POLL_TYPE_SEM_AVAILABLE,
4834
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004835 /* queue/fifo/lifo data availability */
4836 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004837
4838 _POLL_NUM_TYPES
4839};
4840
Patrik Flykt4344e272019-03-08 14:19:05 -07004841#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004842
4843/* private - states bit positions */
4844enum _poll_states_bits {
4845 /* default state when creating event */
4846 _POLL_STATE_NOT_READY,
4847
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004848 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004849 _POLL_STATE_SIGNALED,
4850
4851 /* semaphore is available */
4852 _POLL_STATE_SEM_AVAILABLE,
4853
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004854 /* data is available to read on queue/fifo/lifo */
4855 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004856
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004857 /* queue/fifo/lifo wait was cancelled */
4858 _POLL_STATE_CANCELLED,
4859
Benjamin Walshacc68c12017-01-29 18:57:45 -05004860 _POLL_NUM_STATES
4861};
4862
Patrik Flykt4344e272019-03-08 14:19:05 -07004863#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004864
4865#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004866 (32 - (0 \
4867 + 8 /* tag */ \
4868 + _POLL_NUM_TYPES \
4869 + _POLL_NUM_STATES \
4870 + 1 /* modes */ \
4871 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004872
Benjamin Walshacc68c12017-01-29 18:57:45 -05004873/* end of polling API - PRIVATE */
4874
4875
4876/**
4877 * @defgroup poll_apis Async polling APIs
4878 * @ingroup kernel_apis
4879 * @{
4880 */
4881
4882/* Public polling API */
4883
4884/* public - values for k_poll_event.type bitfield */
4885#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004886#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4887#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4888#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004889#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004890
4891/* public - polling modes */
4892enum k_poll_modes {
4893 /* polling thread does not take ownership of objects when available */
4894 K_POLL_MODE_NOTIFY_ONLY = 0,
4895
4896 K_POLL_NUM_MODES
4897};
4898
4899/* public - values for k_poll_event.state bitfield */
4900#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004901#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4902#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4903#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004904#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004905#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004906
4907/* public - poll signal object */
4908struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05004909 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004910 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004911
Anas Nashife71293e2019-12-04 20:00:14 -05004912 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004913 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4914 * user resets it to 0.
4915 */
4916 unsigned int signaled;
4917
Anas Nashife71293e2019-12-04 20:00:14 -05004918 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004919 int result;
4920};
4921
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004922#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004923 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004924 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004925 .signaled = 0, \
4926 .result = 0, \
4927 }
Anas Nashife71293e2019-12-04 20:00:14 -05004928/**
4929 * @brief Poll Event
4930 *
4931 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004932struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05004933 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004934 sys_dnode_t _node;
4935
Anas Nashife71293e2019-12-04 20:00:14 -05004936 /** PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004937 struct _poller *poller;
4938
Anas Nashife71293e2019-12-04 20:00:14 -05004939 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004940 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004941
Anas Nashife71293e2019-12-04 20:00:14 -05004942 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004943 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004944
Anas Nashife71293e2019-12-04 20:00:14 -05004945 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004946 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004947
Anas Nashife71293e2019-12-04 20:00:14 -05004948 /** mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004949 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004950
Anas Nashife71293e2019-12-04 20:00:14 -05004951 /** unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004952 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004953
Anas Nashife71293e2019-12-04 20:00:14 -05004954 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004955 union {
4956 void *obj;
4957 struct k_poll_signal *signal;
4958 struct k_sem *sem;
4959 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004960 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004961 };
4962};
4963
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004964#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004965 { \
4966 .poller = NULL, \
4967 .type = event_type, \
4968 .state = K_POLL_STATE_NOT_READY, \
4969 .mode = event_mode, \
4970 .unused = 0, \
Peter A. Bigot008f9c02020-04-26 12:30:58 -05004971 .obj = event_obj, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004972 }
4973
4974#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4975 event_tag) \
4976 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004977 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004978 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004979 .state = K_POLL_STATE_NOT_READY, \
4980 .mode = event_mode, \
4981 .unused = 0, \
Peter A. Bigot008f9c02020-04-26 12:30:58 -05004982 .obj = event_obj, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004983 }
4984
4985/**
4986 * @brief Initialize one struct k_poll_event instance
4987 *
4988 * After this routine is called on a poll event, the event it ready to be
4989 * placed in an event array to be passed to k_poll().
4990 *
4991 * @param event The event to initialize.
4992 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4993 * values. Only values that apply to the same object being polled
4994 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4995 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004996 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004997 * @param obj Kernel object or poll signal.
4998 *
4999 * @return N/A
5000 */
5001
Kumar Galacc334c72017-04-21 10:55:34 -05005002extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05005003 int mode, void *obj);
5004
5005/**
5006 * @brief Wait for one or many of multiple poll events to occur
5007 *
5008 * This routine allows a thread to wait concurrently for one or many of
5009 * multiple poll events to have occurred. Such events can be a kernel object
5010 * being available, like a semaphore, or a poll signal event.
5011 *
5012 * When an event notifies that a kernel object is available, the kernel object
5013 * is not "given" to the thread calling k_poll(): it merely signals the fact
5014 * that the object was available when the k_poll() call was in effect. Also,
5015 * all threads trying to acquire an object the regular way, i.e. by pending on
5016 * the object, have precedence over the thread polling on the object. This
5017 * means that the polling thread will never get the poll event on an object
5018 * until the object becomes available and its pend queue is empty. For this
5019 * reason, the k_poll() call is more effective when the objects being polled
5020 * only have one thread, the polling thread, trying to acquire them.
5021 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03005022 * When k_poll() returns 0, the caller should loop on all the events that were
5023 * passed to k_poll() and check the state field for the values that were
5024 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005025 *
5026 * Before being reused for another call to k_poll(), the user has to reset the
5027 * state field to K_POLL_STATE_NOT_READY.
5028 *
Andrew Boie3772f772018-05-07 16:52:57 -07005029 * When called from user mode, a temporary memory allocation is required from
5030 * the caller's resource pool.
5031 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05005032 * @param events An array of pointers to events to be polled for.
5033 * @param num_events The number of events in the array.
Andy Ross78327382020-03-05 15:18:14 -08005034 * @param timeout Waiting period for an event to be ready,
5035 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005036 *
5037 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005038 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03005039 * @retval -EINTR Polling has been interrupted, e.g. with
5040 * k_queue_cancel_wait(). All output events are still set and valid,
5041 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
5042 * words, -EINTR status means that at least one of output events is
5043 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07005044 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
5045 * @retval -EINVAL Bad parameters (user mode only)
Benjamin Walshacc68c12017-01-29 18:57:45 -05005046 */
5047
Andrew Boie3772f772018-05-07 16:52:57 -07005048__syscall int k_poll(struct k_poll_event *events, int num_events,
Andy Ross78327382020-03-05 15:18:14 -08005049 k_timeout_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05005050
5051/**
Benjamin Walsha304f162017-02-02 16:46:09 -05005052 * @brief Initialize a poll signal object.
5053 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07005054 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05005055 *
5056 * @param signal A poll signal.
5057 *
5058 * @return N/A
5059 */
5060
Andrew Boie3772f772018-05-07 16:52:57 -07005061__syscall void k_poll_signal_init(struct k_poll_signal *signal);
5062
5063/*
5064 * @brief Reset a poll signal object's state to unsignaled.
5065 *
5066 * @param signal A poll signal object
5067 */
5068__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
5069
Patrik Flykt4344e272019-03-08 14:19:05 -07005070static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07005071{
Patrik Flykt24d71432019-03-26 19:57:45 -06005072 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07005073}
5074
5075/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07005076 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07005077 *
5078 * @param signal A poll signal object
5079 * @param signaled An integer buffer which will be written nonzero if the
5080 * object was signaled
5081 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07005082 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07005083 * value if it was not.
5084 */
5085__syscall void k_poll_signal_check(struct k_poll_signal *signal,
5086 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05005087
5088/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05005089 * @brief Signal a poll signal object.
5090 *
5091 * This routine makes ready a poll signal, which is basically a poll event of
5092 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
5093 * made ready to run. A @a result value can be specified.
5094 *
5095 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07005096 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07005097 * k_poll_signal_reset(). It thus has to be reset by the user before being
5098 * passed again to k_poll() or k_poll() will consider it being signaled, and
5099 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005100 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05005101 * @note The result is stored and the 'signaled' field is set even if
5102 * this function returns an error indicating that an expiring poll was
5103 * not notified. The next k_poll() will detect the missed raise.
5104 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05005105 * @param signal A poll signal.
5106 * @param result The value to store in the result field of the signal.
5107 *
5108 * @retval 0 The signal was delivered successfully.
5109 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
5110 */
5111
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07005112__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05005113
Anas Nashif954d5502018-02-25 08:37:28 -06005114/**
5115 * @internal
5116 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005117extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05005118
Anas Nashif166f5192018-02-25 08:02:36 -06005119/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05005120
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005121/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05005122 * @defgroup cpu_idle_apis CPU Idling APIs
5123 * @ingroup kernel_apis
5124 * @{
5125 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05005126/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005127 * @brief Make the CPU idle.
5128 *
5129 * This function makes the CPU idle until an event wakes it up.
5130 *
5131 * In a regular system, the idle thread should be the only thread responsible
5132 * for making the CPU idle and triggering any type of power management.
5133 * However, in some more constrained systems, such as a single-threaded system,
5134 * the only thread would be responsible for this if needed.
5135 *
Ioannis Glaropoulos91f6d982020-03-18 23:56:56 +01005136 * @note In some architectures, before returning, the function unmasks interrupts
5137 * unconditionally.
5138 *
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005139 * @return N/A
5140 */
Andrew Boie07525a32019-09-21 16:17:23 -07005141static inline void k_cpu_idle(void)
5142{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005143 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07005144}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005145
5146/**
5147 * @brief Make the CPU idle in an atomic fashion.
5148 *
5149 * Similar to k_cpu_idle(), but called with interrupts locked if operations
5150 * must be done atomically before making the CPU idle.
5151 *
5152 * @param key Interrupt locking key obtained from irq_lock().
5153 *
5154 * @return N/A
5155 */
Andrew Boie07525a32019-09-21 16:17:23 -07005156static inline void k_cpu_atomic_idle(unsigned int key)
5157{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005158 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07005159}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005160
Anas Nashif30c3cff2019-01-22 08:18:13 -05005161/**
5162 * @}
5163 */
Anas Nashif954d5502018-02-25 08:37:28 -06005164
5165/**
5166 * @internal
5167 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005168extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08005169
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005170#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02005171/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005172#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005173#else
5174
Joakim Anderssone04e4c22019-12-20 15:42:38 +01005175#if !defined(CONFIG_ASSERT_NO_FILE_INFO)
5176#define __EXCEPT_LOC() __ASSERT_PRINT("@ %s:%d\n", __FILE__, __LINE__)
5177#else
5178#define __EXCEPT_LOC()
5179#endif
5180
Andrew Boiecdb94d62017-04-18 15:22:05 -07005181/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005182 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005183 *
5184 * We won't have a real exception frame to determine the PC value when
5185 * the oops occurred, so print file and line number before we jump into
5186 * the fatal error handler.
5187 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005188#define z_except_reason(reason) do { \
Joakim Anderssone04e4c22019-12-20 15:42:38 +01005189 __EXCEPT_LOC(); \
Andrew Boie56236372019-07-15 15:22:29 -07005190 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07005191 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005192
5193#endif /* _ARCH__EXCEPT */
5194
5195/**
5196 * @brief Fatally terminate a thread
5197 *
5198 * This should be called when a thread has encountered an unrecoverable
5199 * runtime condition and needs to terminate. What this ultimately
5200 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005201 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005202 *
5203 * If this is called from ISR context, the default system fatal error handler
5204 * will treat it as an unrecoverable system error, just like k_panic().
5205 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005206#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005207
5208/**
5209 * @brief Fatally terminate the system
5210 *
5211 * This should be called when the Zephyr kernel has encountered an
5212 * unrecoverable runtime condition and needs to terminate. What this ultimately
5213 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005214 * will be called will reason code K_ERR_KERNEL_PANIC.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005215 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005216#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005217
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005218/*
5219 * private APIs that are utilized by one or more public APIs
5220 */
5221
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09005222/**
5223 * @internal
5224 */
5225extern void z_init_thread_base(struct _thread_base *thread_base,
5226 int priority, u32_t initial_state,
5227 unsigned int options);
5228
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005229#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06005230/**
5231 * @internal
5232 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005233extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005234#else
Anas Nashif954d5502018-02-25 08:37:28 -06005235/**
5236 * @internal
5237 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005238#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005239#endif
5240
Anas Nashif954d5502018-02-25 08:37:28 -06005241/**
5242 * @internal
5243 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005244extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06005245/**
5246 * @internal
5247 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005248extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005249
Chunlin Hane9c97022017-07-07 20:29:30 +08005250/**
5251 * @defgroup mem_domain_apis Memory domain APIs
5252 * @ingroup kernel_apis
5253 * @{
5254 */
5255
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005256/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005257 * @def K_MEM_PARTITION_DEFINE
5258 * @brief Used to declare a memory partition
Chunlin Hane9c97022017-07-07 20:29:30 +08005259 */
5260#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5261#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5262 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005263 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005264 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005265#else
5266#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005267 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005268 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005269#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5270
Chunlin Hane9c97022017-07-07 20:29:30 +08005271/* memory partition */
5272struct k_mem_partition {
Anas Nashife71293e2019-12-04 20:00:14 -05005273 /** start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005274 uintptr_t start;
Anas Nashife71293e2019-12-04 20:00:14 -05005275 /** size of memory partition */
Andrew Boiea8248212019-11-13 12:10:56 -08005276 size_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005277#if defined(CONFIG_MEMORY_PROTECTION)
Anas Nashife71293e2019-12-04 20:00:14 -05005278 /** attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305279 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005280#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005281};
5282
Anas Nashife71293e2019-12-04 20:00:14 -05005283/**
5284 * @brief Memory Domain
5285 *
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305286 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005287struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305288#ifdef CONFIG_USERSPACE
Anas Nashife71293e2019-12-04 20:00:14 -05005289 /** partitions in the domain */
Chunlin Hane9c97022017-07-07 20:29:30 +08005290 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305291#endif /* CONFIG_USERSPACE */
Anas Nashife71293e2019-12-04 20:00:14 -05005292 /** domain q */
Chunlin Hane9c97022017-07-07 20:29:30 +08005293 sys_dlist_t mem_domain_q;
Anas Nashife71293e2019-12-04 20:00:14 -05005294 /** number of partitions in the domain */
Leandro Pereira08de6582018-02-28 14:22:57 -08005295 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005296};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305297
Chunlin Hane9c97022017-07-07 20:29:30 +08005298
5299/**
5300 * @brief Initialize a memory domain.
5301 *
5302 * Initialize a memory domain with given name and memory partitions.
5303 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005304 * See documentation for k_mem_domain_add_partition() for details about
5305 * partition constraints.
5306 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005307 * @param domain The memory domain to be initialized.
5308 * @param num_parts The number of array items of "parts" parameter.
5309 * @param parts An array of pointers to the memory partitions. Can be NULL
5310 * if num_parts is zero.
5311 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005312extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005313 struct k_mem_partition *parts[]);
5314/**
5315 * @brief Destroy a memory domain.
5316 *
5317 * Destroy a memory domain.
5318 *
5319 * @param domain The memory domain to be destroyed.
5320 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005321extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5322
5323/**
5324 * @brief Add a memory partition into a memory domain.
5325 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005326 * Add a memory partition into a memory domain. Partitions must conform to
5327 * the following constraints:
5328 *
5329 * - Partition bounds must be within system RAM boundaries on MMU-based
5330 * systems.
5331 * - Partitions in the same memory domain may not overlap each other.
5332 * - Partitions must not be defined which expose private kernel
5333 * data structures or kernel objects.
5334 * - The starting address alignment, and the partition size must conform to
5335 * the constraints of the underlying memory management hardware, which
5336 * varies per architecture.
5337 * - Memory domain partitions are only intended to control access to memory
5338 * from user mode threads.
5339 *
5340 * Violating these constraints may lead to CPU exceptions or undefined
5341 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005342 *
5343 * @param domain The memory domain to be added a memory partition.
5344 * @param part The memory partition to be added
5345 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005346extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5347 struct k_mem_partition *part);
5348
5349/**
5350 * @brief Remove a memory partition from a memory domain.
5351 *
5352 * Remove a memory partition from a memory domain.
5353 *
5354 * @param domain The memory domain to be removed a memory partition.
5355 * @param part The memory partition to be removed
5356 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005357extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5358 struct k_mem_partition *part);
5359
5360/**
5361 * @brief Add a thread into a memory domain.
5362 *
5363 * Add a thread into a memory domain.
5364 *
5365 * @param domain The memory domain that the thread is going to be added into.
5366 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005367 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005368 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005369extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5370 k_tid_t thread);
5371
5372/**
5373 * @brief Remove a thread from its memory domain.
5374 *
5375 * Remove a thread from its memory domain.
5376 *
5377 * @param thread ID of thread going to be removed from its memory domain.
5378 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005379extern void k_mem_domain_remove_thread(k_tid_t thread);
5380
Anas Nashif166f5192018-02-25 08:02:36 -06005381/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005382
Andrew Boied76ae462020-01-02 11:57:43 -08005383#ifdef CONFIG_PRINTK
Andrew Boie756f9072017-10-10 16:01:49 -07005384/**
5385 * @brief Emit a character buffer to the console device
5386 *
5387 * @param c String of characters to print
5388 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005389 *
Andrew Boie756f9072017-10-10 16:01:49 -07005390 */
5391__syscall void k_str_out(char *c, size_t n);
Andrew Boied76ae462020-01-02 11:57:43 -08005392#endif
Andrew Boie756f9072017-10-10 16:01:49 -07005393
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005394/**
5395 * @brief Disable preservation of floating point context information.
5396 *
5397 * This routine informs the kernel that the specified thread
5398 * will no longer be using the floating point registers.
5399 *
5400 * @warning
5401 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005402 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005403 *
5404 * @warning
5405 * This routine should only be used to disable floating point support for
5406 * a thread that currently has such support enabled.
5407 *
5408 * @param thread ID of thread.
5409 *
5410 * @retval 0 On success.
5411 * @retval -ENOSYS If the floating point disabling is not implemented.
5412 * -EINVAL If the floating point disabling could not be performed.
5413 */
5414__syscall int k_float_disable(struct k_thread *thread);
5415
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005416#ifdef __cplusplus
5417}
5418#endif
5419
Anas Nashif73008b42020-02-06 09:14:51 -05005420#include <tracing/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005421#include <syscalls/kernel.h>
5422
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005423#endif /* !_ASMLANGUAGE */
5424
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005425#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */