blob: 4272e15578fc4aad37a8c1934e5bb7dea41efa15 [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
Andy Ross225c74b2018-06-27 11:20:50 -070079#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070080
81typedef struct {
82 struct _priq_rb waitq;
83} _wait_q_t;
84
Patrik Flykt4344e272019-03-08 14:19:05 -070085extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070086
Patrik Flykt4344e272019-03-08 14:19:05 -070087#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070088
89#else
90
Andy Rossccf3bf72018-05-10 11:10:34 -070091typedef struct {
92 sys_dlist_t waitq;
93} _wait_q_t;
94
Patrik Flykt4344e272019-03-08 14:19:05 -070095#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Andy Ross1acd8c22018-05-03 14:51:49 -070097#endif
98
Anas Nashif2f203c22016-12-18 06:57:45 -050099#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800100#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +0800101#define _OBJECT_TRACING_LINKED_FLAG u8_t __linked;
102#define _OBJECT_TRACING_INIT \
103 .__next = NULL, \
104 .__linked = 0,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500106#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800107#define _OBJECT_TRACING_NEXT_PTR(type)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +0800108#define _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400109#endif
110
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300112#define _POLL_EVENT_OBJ_INIT(obj) \
113 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
114#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500115#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300116#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500117#define _POLL_EVENT
118#endif
119
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500120struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mutex;
122struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_msgq;
124struct k_mbox;
125struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200126struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127struct k_fifo;
128struct k_lifo;
129struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400130struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400131struct k_mem_pool;
132struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500133struct k_poll_event;
134struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800135struct k_mem_domain;
136struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800137struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400138
Anas Nashife71293e2019-12-04 20:00:14 -0500139/**
140 * @brief Kernel Object Types
141 *
142 * This enumeration needs to be kept in sync with the lists of kernel objects
Andrew Boie5bd891d2017-09-27 12:59:28 -0700143 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
144 * function in kernel/userspace.c
145 */
Andrew Boie945af952017-08-22 13:15:23 -0700146enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700147 K_OBJ_ANY,
148
Leandro Pereirac2003672018-04-04 13:50:32 -0700149 /** @cond
150 * Doxygen should ignore this build-time generated include file
151 * when genrating API documentation. Enumeration values are
152 * generated during build by gen_kobject_list.py. It includes
153 * basic kernel objects (e.g. pipes and mutexes) and driver types.
154 */
155#include <kobj-types-enum.h>
156 /** @endcond
157 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700158
Andrew Boie945af952017-08-22 13:15:23 -0700159 K_OBJ_LAST
160};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500161/**
162 * @defgroup usermode_apis User Mode APIs
163 * @ingroup kernel_apis
164 * @{
165 */
Andrew Boie945af952017-08-22 13:15:23 -0700166
167#ifdef CONFIG_USERSPACE
168/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700169 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700170struct _k_object {
Andrew Boie22553a72019-11-19 18:27:42 -0800171 void *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700172 u8_t perms[CONFIG_MAX_THREAD_BYTES];
173 u8_t type;
174 u8_t flags;
Andrew Boiee48ed6a2019-11-13 12:52:00 -0800175 uintptr_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700176} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700177
Andrew Boie877f82e2017-10-17 11:20:22 -0700178struct _k_object_assignment {
179 struct k_thread *thread;
180 void * const *objects;
181};
182
183/**
184 * @brief Grant a static thread access to a list of kernel objects
185 *
186 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
187 * a set of kernel objects. These objects do not need to be in an initialized
188 * state. The permissions will be granted when the threads are initialized
189 * in the early boot sequence.
190 *
191 * All arguments beyond the first must be pointers to kernel objects.
192 *
193 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
194 */
195#define K_THREAD_ACCESS_GRANT(name_, ...) \
196 static void * const _CONCAT(_object_list_, name_)[] = \
197 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400198 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
199 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700200 { (&_k_thread_obj_ ## name_), \
201 (_CONCAT(_object_list_, name_)) }
202
Anas Nashife71293e2019-12-04 20:00:14 -0500203/** Object initialized */
Andrew Boie945af952017-08-22 13:15:23 -0700204#define K_OBJ_FLAG_INITIALIZED BIT(0)
Anas Nashife71293e2019-12-04 20:00:14 -0500205/** Object is Public */
Andrew Boie04caa672017-10-13 13:57:07 -0700206#define K_OBJ_FLAG_PUBLIC BIT(1)
Anas Nashife71293e2019-12-04 20:00:14 -0500207/** Object allocated */
Andrew Boie97bf0012018-04-24 17:01:37 -0700208#define K_OBJ_FLAG_ALLOC BIT(2)
Anas Nashife71293e2019-12-04 20:00:14 -0500209/** Driver Object */
Andrew Boie78757072019-07-23 13:29:30 -0700210#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700211
212/**
213 * Lookup a kernel object and init its metadata if it exists
214 *
215 * Calling this on an object will make it usable from userspace.
216 * Intended to be called as the last statement in kernel object init
217 * functions.
218 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500219 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700220 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700221void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700222#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700223/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700224#define K_THREAD_ACCESS_GRANT(thread, ...)
225
Anas Nashif954d5502018-02-25 08:37:28 -0600226/**
227 * @internal
228 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700229static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700230{
231 ARG_UNUSED(obj);
232}
233
Anas Nashif954d5502018-02-25 08:37:28 -0600234/**
235 * @internal
236 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700237static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700238 struct k_thread *thread)
239{
240 ARG_UNUSED(object);
241 ARG_UNUSED(thread);
242}
243
Anas Nashif954d5502018-02-25 08:37:28 -0600244/**
245 * @internal
246 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700247static inline void k_object_access_revoke(void *object,
248 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700249{
250 ARG_UNUSED(object);
251 ARG_UNUSED(thread);
252}
253
Andrew Boiee9cfc542018-04-13 13:15:28 -0700254/**
255 * @internal
256 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700257static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700258{
259 ARG_UNUSED(object);
260}
261
Andrew Boie41bab6e2017-10-14 14:42:23 -0700262static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700263{
264 ARG_UNUSED(object);
265}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700266/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700267#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700268
269/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600270 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700271 *
272 * The thread will be granted access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700274 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700275 *
276 * @param object Address of kernel object
277 * @param thread Thread to grant access to the object
278 */
Andrew Boie743e4682017-10-04 12:25:50 -0700279__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700280
Andrew Boiea89bf012017-10-09 14:47:55 -0700281/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600282 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700283 *
284 * The thread will lose access to the object if the caller is from
285 * supervisor mode, or the caller is from user mode AND has permissions
286 * on both the object and the thread whose access is being revoked.
287 *
288 * @param object Address of kernel object
289 * @param thread Thread to remove access to the object
290 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700291void k_object_access_revoke(void *object, struct k_thread *thread);
292
Anas Nashife71293e2019-12-04 20:00:14 -0500293/**
294 * @brief Release an object
295 *
296 * Allows user threads to drop their own permission on an object
297 * Their permissions are automatically cleared when a thread terminates.
298 *
299 * @param object The object to be released
300 *
301 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700302__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700303
304/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600305 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700306 *
307 * If the caller is from supervisor mode, or the caller is from user mode and
308 * have sufficient permissions on the object, then that object will have
309 * permissions granted to it for *all* current and future threads running in
310 * the system, effectively becoming a public kernel object.
311 *
312 * Use of this API should be avoided on systems that are running untrusted code
313 * as it is possible for such code to derive the addresses of kernel objects
314 * and perform unwanted operations on them.
315 *
Andrew Boie04caa672017-10-13 13:57:07 -0700316 * It is not possible to revoke permissions on public objects; once public,
317 * any thread may use it.
318 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700319 * @param object Address of kernel object
320 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700321void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700322
Andrew Boie31bdfc02017-11-08 16:38:03 -0800323/**
324 * Allocate a kernel object of a designated type
325 *
326 * This will instantiate at runtime a kernel object of the specified type,
327 * returning a pointer to it. The object will be returned in an uninitialized
328 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700329 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800330 *
331 * Currently, allocation of thread stacks is not supported.
332 *
333 * @param otype Requested kernel object type
334 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
335 * available
336 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700337__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338
Andrew Boie97bf0012018-04-24 17:01:37 -0700339#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800340/**
341 * Free a kernel object previously allocated with k_object_alloc()
342 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700343 * This will return memory for a kernel object back to resource pool it was
344 * allocated from. Care must be exercised that the object will not be used
345 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800346 *
347 * @param obj Pointer to the kernel object memory address.
348 */
349void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700350#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700351/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700352static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700353{
Kumar Gala85699f72018-05-17 09:26:03 -0500354 ARG_UNUSED(otype);
355
Andrew Boie97bf0012018-04-24 17:01:37 -0700356 return NULL;
357}
Anas Nashife71293e2019-12-04 20:00:14 -0500358/**
359 * @brief Free an object
360 *
361 * @param obj
362 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700363static inline void k_obj_free(void *obj)
364{
365 ARG_UNUSED(obj);
366}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700367/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800368#endif /* CONFIG_DYNAMIC_OBJECTS */
369
Anas Nashif4bcb2942019-01-23 23:06:29 -0500370/** @} */
371
Andrew Boiebca15da2017-10-15 14:17:48 -0700372/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700373 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700374 *
375 * The purpose of this data type is to clearly distinguish between the
376 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
377 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400378 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700379 * stack buffer region with guard areas that trigger a MPU or MMU fault
380 * if written to.
381 *
382 * APIs that want to work with the buffer inside should continue to use
383 * char *.
384 *
385 * Stacks should always be created with K_THREAD_STACK_DEFINE().
386 */
387struct __packed _k_thread_stack_element {
388 char data;
389};
Daniel Leung7476a6e2019-11-25 13:58:40 -0800390
391/**
392 * @typedef k_thread_stack_t
393 * @brief Typedef of struct _k_thread_stack_element
394 *
395 * @see _k_thread_stack_element
396 */
Andrew Boiebca15da2017-10-15 14:17:48 -0700397
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700398/**
399 * @typedef k_thread_entry_t
400 * @brief Thread entry point function type.
401 *
402 * A thread's entry point function is invoked when the thread starts executing.
403 * Up to 3 argument values can be passed to the function.
404 *
405 * The thread terminates execution permanently if the entry point function
406 * returns. The thread is responsible for releasing any shared resources
407 * it may own (such as mutexes and dynamically allocated memory), prior to
408 * returning.
409 *
410 * @param p1 First argument.
411 * @param p2 Second argument.
412 * @param p3 Third argument.
413 *
414 * @return N/A
415 */
Andrew Boie73abd322017-04-04 13:19:13 -0700416
417#ifdef CONFIG_THREAD_MONITOR
418struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700419 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700420 void *parameter1;
421 void *parameter2;
422 void *parameter3;
423};
424#endif
425
426/* can be used for creating 'dummy' threads, e.g. for pending on objects */
427struct _thread_base {
428
429 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700430 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600431 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700432 struct rbnode qnode_rb;
433 };
434
Andy Ross1acd8c22018-05-03 14:51:49 -0700435 /* wait queue on which the thread is pended (needed only for
436 * trees, not dumb lists)
437 */
438 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700439
440 /* user facing 'thread options'; values defined in include/kernel.h */
441 u8_t user_options;
442
443 /* thread state */
444 u8_t thread_state;
445
446 /*
447 * scheduler lock count and thread priority
448 *
449 * These two fields control the preemptibility of a thread.
450 *
451 * When the scheduler is locked, sched_locked is decremented, which
452 * means that the scheduler is locked for values from 0xff to 0x01. A
453 * thread is coop if its prio is negative, thus 0x80 to 0xff when
454 * looked at the value as unsigned.
455 *
456 * By putting them end-to-end, this means that a thread is
457 * non-preemptible if the bundled value is greater than or equal to
458 * 0x0080.
459 */
460 union {
461 struct {
462#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
463 u8_t sched_locked;
464 s8_t prio;
465#else /* LITTLE and PDP */
466 s8_t prio;
467 u8_t sched_locked;
468#endif
469 };
470 u16_t preempt;
471 };
472
Andy Ross4a2e50f2018-05-15 11:06:25 -0700473#ifdef CONFIG_SCHED_DEADLINE
474 int prio_deadline;
475#endif
476
Andy Ross1acd8c22018-05-03 14:51:49 -0700477 u32_t order_key;
478
Andy Ross2724fd12018-01-29 14:55:20 -0800479#ifdef CONFIG_SMP
480 /* True for the per-CPU idle threads */
481 u8_t is_idle;
482
Andy Ross2724fd12018-01-29 14:55:20 -0800483 /* CPU index on which thread was last run */
484 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700485
486 /* Recursive count of irq_lock() calls */
487 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800488
489#endif
490
491#ifdef CONFIG_SCHED_CPU_MASK
492 /* "May run on" bits for each CPU */
493 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800494#endif
495
Andrew Boie73abd322017-04-04 13:19:13 -0700496 /* data returned by APIs */
497 void *swap_data;
498
499#ifdef CONFIG_SYS_CLOCK_EXISTS
500 /* this thread's entry in a timeout queue */
501 struct _timeout timeout;
502#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700503};
504
505typedef struct _thread_base _thread_base_t;
506
507#if defined(CONFIG_THREAD_STACK_INFO)
508/* Contains the stack information of a thread */
509struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700510 /* Stack start - Represents the start address of the thread-writable
511 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700512 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400513 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700514
515 /* Stack Size - Thread writable stack buffer size. Represents
516 * the size of the actual area, starting from the start member,
517 * that should be writable by the thread
518 */
Andrew Boie528233e2019-12-11 14:54:15 -0800519 size_t size;
Andrew Boie73abd322017-04-04 13:19:13 -0700520};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700521
522typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700523#endif /* CONFIG_THREAD_STACK_INFO */
524
Chunlin Hane9c97022017-07-07 20:29:30 +0800525#if defined(CONFIG_USERSPACE)
526struct _mem_domain_info {
Anas Nashife71293e2019-12-04 20:00:14 -0500527 /** memory domain queue node */
Chunlin Hane9c97022017-07-07 20:29:30 +0800528 sys_dnode_t mem_domain_q_node;
Anas Nashife71293e2019-12-04 20:00:14 -0500529 /** memory domain of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800530 struct k_mem_domain *mem_domain;
531};
532
533#endif /* CONFIG_USERSPACE */
534
Daniel Leungfc182432018-08-16 15:42:28 -0700535#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
536struct _thread_userspace_local_data {
537 int errno_var;
538};
539#endif
540
Anas Nashifce78d162018-05-24 12:43:11 -0500541/**
542 * @ingroup thread_apis
543 * Thread Structure
544 */
Andrew Boie73abd322017-04-04 13:19:13 -0700545struct k_thread {
546
547 struct _thread_base base;
548
Anas Nashifce78d162018-05-24 12:43:11 -0500549 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700550 struct _callee_saved callee_saved;
551
Anas Nashifce78d162018-05-24 12:43:11 -0500552 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700553 void *init_data;
554
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /**
556 * abort function
557 * @req K-THREAD-002
558 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700559 void (*fn_abort)(void);
560
561#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700563 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700564
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700566 struct k_thread *next_thread;
567#endif
568
Anas Nashif57554052018-03-03 02:31:05 -0600569#if defined(CONFIG_THREAD_NAME)
Anas Nashife71293e2019-12-04 20:00:14 -0500570 /** Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700571 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600572#endif
573
Andrew Boie73abd322017-04-04 13:19:13 -0700574#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500575 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700576 void *custom_data;
577#endif
578
Daniel Leungfc182432018-08-16 15:42:28 -0700579#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
580 struct _thread_userspace_local_data *userspace_local_data;
581#endif
582
Andrew Boie73abd322017-04-04 13:19:13 -0700583#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700584#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700586 int errno_var;
587#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700588#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700589
590#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500591 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700592 struct _thread_stack_info stack_info;
593#endif /* CONFIG_THREAD_STACK_INFO */
594
Chunlin Hane9c97022017-07-07 20:29:30 +0800595#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500596 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800597 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500598 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700599 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800600#endif /* CONFIG_USERSPACE */
601
Andy Ross042d8ec2017-12-09 08:37:20 -0800602#if defined(CONFIG_USE_SWITCH)
603 /* When using __switch() a few previously arch-specific items
604 * become part of the core OS
605 */
606
Patrik Flykt4344e272019-03-08 14:19:05 -0700607 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800608 int swap_retval;
609
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800610 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800611 void *switch_handle;
612#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500613 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700614 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800615
Anas Nashifce78d162018-05-24 12:43:11 -0500616 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700617 struct _thread_arch arch;
618};
619
620typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400621typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400623enum execution_context_types {
624 K_ISR = 0,
625 K_COOP_THREAD,
626 K_PREEMPT_THREAD,
627};
628
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400629/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500630 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100631 * @{
632 */
Anas Nashife71293e2019-12-04 20:00:14 -0500633
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530634typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
635 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100636
637/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530638 * @brief Iterate over all the threads in the system.
639 *
640 * This routine iterates over all the threads in the system and
641 * calls the user_cb function for each thread.
642 *
643 * @param user_cb Pointer to the user callback function.
644 * @param user_data Pointer to user data.
645 *
646 * @note CONFIG_THREAD_MONITOR must be set for this function
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100647 * to be effective.
648 * @note This API uses @ref k_spin_lock to protect the _kernel.threads
649 * list which means creation of new threads and terminations of existing
650 * threads are blocked until this API returns.
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530651 *
652 * @return N/A
653 */
654extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
655
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100656/**
657 * @brief Iterate over all the threads in the system without locking.
658 *
659 * This routine works exactly the same like @ref k_thread_foreach
660 * but unlocks interrupts when user_cb is executed.
661 *
662 * @param user_cb Pointer to the user callback function.
663 * @param user_data Pointer to user data.
664 *
665 * @note CONFIG_THREAD_MONITOR must be set for this function
666 * to be effective.
667 * @note This API uses @ref k_spin_lock only when accessing the _kernel.threads
668 * queue elements. It unlocks it during user callback function processing.
669 * If a new task is created when this @c foreach function is in progress,
670 * the added new task would not be included in the enumeration.
671 * If a task is aborted during this enumeration, there would be a race here
672 * and there is a possibility that this aborted task would be included in the
673 * enumeration.
674 * @note If the task is aborted and the memory occupied by its @c k_thread
675 * structure is reused when this @c k_thread_foreach_unlocked is in progress
676 * it might even lead to the system behave unstable.
677 * This function may never return, as it would follow some @c next task
678 * pointers treating given pointer as a pointer to the k_thread structure
679 * while it is something different right now.
680 * Do not reuse the memory that was occupied by k_thread structure of aborted
681 * task if it was aborted after this function was called in any context.
682 */
683extern void k_thread_foreach_unlocked(
684 k_thread_user_cb_t user_cb, void *user_data);
685
Anas Nashif166f5192018-02-25 08:02:36 -0600686/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100687
688/**
Allan Stephensc98da842016-11-11 15:45:03 -0500689 * @defgroup thread_apis Thread APIs
690 * @ingroup kernel_apis
691 * @{
692 */
693
Benjamin Walshed240f22017-01-22 13:05:08 -0500694#endif /* !_ASMLANGUAGE */
695
696
697/*
698 * Thread user options. May be needed by assembly code. Common part uses low
699 * bits, arch-specific use high bits.
700 */
701
Anas Nashifa541e932018-05-24 11:19:16 -0500702/**
703 * @brief system thread that must not abort
704 * @req K-THREAD-000
705 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700706#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500707
708#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500709/**
710 * @brief thread uses floating point registers
711 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700712#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500713#endif
714
Anas Nashifa541e932018-05-24 11:19:16 -0500715/**
716 * @brief user mode thread
717 *
718 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700719 * has additional restrictions
720 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700721#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700722
Anas Nashifa541e932018-05-24 11:19:16 -0500723/**
724 * @brief Inherit Permissions
725 *
726 * @details
727 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700728 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
729 * is not enabled.
730 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700731#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700732
Benjamin Walshed240f22017-01-22 13:05:08 -0500733#ifdef CONFIG_X86
734/* x86 Bitmask definitions for threads user options */
735
736#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
737/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700738#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500739#endif
740#endif
741
742/* end - thread options */
743
744#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700746 * @brief Create a thread.
747 *
748 * This routine initializes a thread, then schedules it for execution.
749 *
750 * The new thread may be scheduled for immediate execution or a delayed start.
751 * If the newly spawned thread does not have a delayed start the kernel
752 * scheduler may preempt the current thread to allow the new thread to
753 * execute.
754 *
755 * Thread options are architecture-specific, and can include K_ESSENTIAL,
756 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
757 * them using "|" (the logical OR operator).
758 *
759 * Historically, users often would use the beginning of the stack memory region
760 * to store the struct k_thread data, although corruption will occur if the
761 * stack overflows this region and stack protection features may not detect this
762 * situation.
763 *
764 * @param new_thread Pointer to uninitialized struct k_thread
765 * @param stack Pointer to the stack space.
766 * @param stack_size Stack size in bytes.
767 * @param entry Thread entry function.
768 * @param p1 1st entry point parameter.
769 * @param p2 2nd entry point parameter.
770 * @param p3 3rd entry point parameter.
771 * @param prio Thread priority.
772 * @param options Thread options.
773 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
774 *
775 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400776 *
777 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700778 */
Andrew Boie662c3452017-10-02 10:51:18 -0700779__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700780 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700781 size_t stack_size,
782 k_thread_entry_t entry,
783 void *p1, void *p2, void *p3,
784 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700785
Andrew Boie3f091b52017-08-30 14:34:14 -0700786/**
787 * @brief Drop a thread's privileges permanently to user mode
788 *
789 * @param entry Function to start executing from
790 * @param p1 1st entry point parameter
791 * @param p2 2nd entry point parameter
792 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400793 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700794 */
795extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
796 void *p1, void *p2,
797 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700798
Andrew Boied26cf2d2017-03-30 13:07:02 -0700799/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530800 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700801 *
802 * This is a convenience function. For the provided thread, grant access to
803 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700804 *
805 * The thread object must be initialized (i.e. running). The objects don't
806 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530807 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700808 *
809 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530810 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400811 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700812 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530813#define k_thread_access_grant(thread, ...) \
814 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700815
816/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700817 * @brief Assign a resource memory pool to a thread
818 *
819 * By default, threads have no resource pool assigned unless their parent
820 * thread has a resource pool, in which case it is inherited. Multiple
821 * threads may be assigned to the same memory pool.
822 *
823 * Changing a thread's resource pool will not migrate allocations from the
824 * previous pool.
825 *
826 * @param thread Target thread to assign a memory pool for resource requests,
827 * or NULL if the thread should no longer have a memory pool.
828 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400829 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700830 */
831static inline void k_thread_resource_pool_assign(struct k_thread *thread,
832 struct k_mem_pool *pool)
833{
834 thread->resource_pool = pool;
835}
836
837#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
838/**
839 * @brief Assign the system heap as a thread's resource pool
840 *
841 * Similar to k_thread_resource_pool_assign(), but the thread will use
842 * the kernel heap to draw memory.
843 *
844 * Use with caution, as a malicious thread could perform DoS attacks on the
845 * kernel heap.
846 *
847 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400848 *
849 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700850 */
851void k_thread_system_pool_assign(struct k_thread *thread);
852#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
853
854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700857 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700859 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200861 * @return Zero if the requested time has elapsed or the number of milliseconds
862 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700864__syscall s32_t k_sleep(s32_t ms);
865
866/**
867 * @brief Put the current thread to sleep with microsecond resolution.
868 *
869 * This function is unlikely to work as expected without kernel tuning.
870 * In particular, because the lower bound on the duration of a sleep is
871 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
872 * to achieve the resolution desired. The implications of doing this must
873 * be understood before attempting to use k_usleep(). Use with caution.
874 *
875 * @param us Number of microseconds to sleep.
876 *
877 * @return Zero if the requested time has elapsed or the number of microseconds
878 * left to sleep, if thread was woken up by \ref k_wakeup call.
879 */
880__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881
882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
885 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 * @return N/A
889 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800890__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891
892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 *
899 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400900 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 */
Andrew Boie468190a2017-09-29 14:00:48 -0700902__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903
904/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500907 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * If @a thread is not currently sleeping, the routine has no effect.
910 *
911 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400912 *
913 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400914 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 */
Andrew Boie468190a2017-09-29 14:00:48 -0700916__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917
918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500919 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400922 *
923 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700925__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926
927/**
Allan Stephensc98da842016-11-11 15:45:03 -0500928 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930 * This routine permanently stops execution of @a thread. The thread is taken
931 * off all kernel queues it is part of (i.e. the ready queue, the timeout
932 * queue, or a kernel object wait queue). However, any kernel resources the
933 * thread might currently own (such as mutexes or memory blocks) are not
934 * released. It is the responsibility of the caller of this routine to ensure
935 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 *
939 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400940 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400941 */
Andrew Boie468190a2017-09-29 14:00:48 -0700942__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943
Andrew Boie7d627c52017-08-30 11:01:56 -0700944
945/**
946 * @brief Start an inactive thread
947 *
948 * If a thread was created with K_FOREVER in the delay parameter, it will
949 * not be added to the scheduling queue until this function is called
950 * on it.
951 *
952 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400953 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700954 */
Andrew Boie468190a2017-09-29 14:00:48 -0700955__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700956
Allan Stephensc98da842016-11-11 15:45:03 -0500957/**
958 * @cond INTERNAL_HIDDEN
959 */
960
Benjamin Walshd211a522016-12-06 11:44:01 -0500961/* timeout has timed out and is not on _timeout_q anymore */
962#define _EXPIRED (-2)
963
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400964struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700965 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700966 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400967 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700968 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500969 void *init_p1;
970 void *init_p2;
971 void *init_p3;
972 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500973 u32_t init_options;
974 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500975 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600976 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977};
978
Andrew Boied26cf2d2017-03-30 13:07:02 -0700979#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400980 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600981 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500982 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700983 .init_thread = (thread), \
984 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500985 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700986 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400987 .init_p1 = (void *)p1, \
988 .init_p2 = (void *)p2, \
989 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500990 .init_prio = (prio), \
991 .init_options = (options), \
992 .init_delay = (delay), \
993 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600994 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400995 }
996
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400997/**
Allan Stephensc98da842016-11-11 15:45:03 -0500998 * INTERNAL_HIDDEN @endcond
999 */
1000
1001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * @brief Statically define and initialize a thread.
1003 *
1004 * The thread may be scheduled for immediate execution or a delayed start.
1005 *
1006 * Thread options are architecture-specific, and can include K_ESSENTIAL,
1007 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
1008 * them using "|" (the logical OR operator).
1009 *
1010 * The ID of the thread can be accessed using:
1011 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001012 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 *
1014 * @param name Name of the thread.
1015 * @param stack_size Stack size in bytes.
1016 * @param entry Thread entry function.
1017 * @param p1 1st entry point parameter.
1018 * @param p2 2nd entry point parameter.
1019 * @param p3 3rd entry point parameter.
1020 * @param prio Thread priority.
1021 * @param options Thread options.
1022 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001023 *
Anas Nashif47420d02018-05-24 14:20:56 -04001024 * @req K-THREAD-010
1025 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001026 * @internal It has been observed that the x86 compiler by default aligns
1027 * these _static_thread_data structures to 32-byte boundaries, thereby
1028 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -04001029 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001030 */
Allan Stephens6cfe1322016-10-26 10:16:51 -05001031#define K_THREAD_DEFINE(name, stack_size, \
1032 entry, p1, p2, p3, \
1033 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -07001034 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001035 struct k_thread _k_thread_obj_##name; \
1036 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -07001037 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
1038 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001039 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -06001040 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001041 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001042
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * @param thread ID of thread whose priority is needed.
1049 *
1050 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001051 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001053__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 *
1060 * Rescheduling can occur immediately depending on the priority @a thread is
1061 * set to:
1062 *
1063 * - If its priority is raised above the priority of the caller of this
1064 * function, and the caller is preemptible, @a thread will be scheduled in.
1065 *
1066 * - If the caller operates on itself, it lowers its priority below that of
1067 * other threads in the system, and the caller is preemptible, the thread of
1068 * highest priority will be scheduled in.
1069 *
1070 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1071 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1072 * highest priority.
1073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 * @param prio New priority.
1076 *
1077 * @warning Changing the priority of a thread currently involved in mutex
1078 * priority inheritance may result in undefined behavior.
1079 *
1080 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001081 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 */
Andrew Boie468190a2017-09-29 14:00:48 -07001083__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001084
Andy Ross4a2e50f2018-05-15 11:06:25 -07001085
1086#ifdef CONFIG_SCHED_DEADLINE
1087/**
1088 * @brief Set deadline expiration time for scheduler
1089 *
1090 * This sets the "deadline" expiration as a time delta from the
1091 * current time, in the same units used by k_cycle_get_32(). The
1092 * scheduler (when deadline scheduling is enabled) will choose the
1093 * next expiring thread when selecting between threads at the same
1094 * static priority. Threads at different priorities will be scheduled
1095 * according to their static priority.
1096 *
1097 * @note Deadlines that are negative (i.e. in the past) are still seen
1098 * as higher priority than others, even if the thread has "finished"
1099 * its work. If you don't want it scheduled anymore, you have to
1100 * reset the deadline into the future, block/pend the thread, or
1101 * modify its priority with k_thread_priority_set().
1102 *
1103 * @note Despite the API naming, the scheduler makes no guarantees the
1104 * the thread WILL be scheduled within that deadline, nor does it take
1105 * extra metadata (like e.g. the "runtime" and "period" parameters in
1106 * Linux sched_setattr()) that allows the kernel to validate the
1107 * scheduling for achievability. Such features could be implemented
1108 * above this call, which is simply input to the priority selection
1109 * logic.
1110 *
Anas Nashif240c5162019-06-10 12:25:50 -04001111 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001112 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001113 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1114 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001115 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001116 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001117 * @param thread A thread on which to set the deadline
1118 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001119 *
1120 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001121 */
1122__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1123#endif
1124
Andy Rossab46b1b2019-01-30 15:00:42 -08001125#ifdef CONFIG_SCHED_CPU_MASK
1126/**
1127 * @brief Sets all CPU enable masks to zero
1128 *
1129 * After this returns, the thread will no longer be schedulable on any
1130 * CPUs. The thread must not be currently runnable.
1131 *
Anas Nashif240c5162019-06-10 12:25:50 -04001132 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001133 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001134 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1135 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001136 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001137 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001138 * @param thread Thread to operate upon
1139 * @return Zero on success, otherwise error code
1140 */
1141int k_thread_cpu_mask_clear(k_tid_t thread);
1142
1143/**
1144 * @brief Sets all CPU enable masks to one
1145 *
1146 * After this returns, the thread will be schedulable on any CPU. The
1147 * thread must not be currently runnable.
1148 *
Anas Nashif240c5162019-06-10 12:25:50 -04001149 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001150 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001151 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1152 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001153 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001154 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001155 * @param thread Thread to operate upon
1156 * @return Zero on success, otherwise error code
1157 */
1158int k_thread_cpu_mask_enable_all(k_tid_t thread);
1159
1160/**
1161 * @brief Enable thread to run on specified CPU
1162 *
1163 * The thread must not be currently runnable.
1164 *
Anas Nashif240c5162019-06-10 12:25:50 -04001165 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001166 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001167 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1168 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001169 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001170 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001171 * @param thread Thread to operate upon
1172 * @param cpu CPU index
1173 * @return Zero on success, otherwise error code
1174 */
1175int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1176
1177/**
1178 * @brief Prevent thread to run on specified CPU
1179 *
1180 * The thread must not be currently runnable.
1181 *
Anas Nashif240c5162019-06-10 12:25:50 -04001182 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001183 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001184 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1185 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001186 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001187 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001188 * @param thread Thread to operate upon
1189 * @param cpu CPU index
1190 * @return Zero on success, otherwise error code
1191 */
1192int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1193#endif
1194
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001196 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 *
Andy Ross50d09422019-11-19 11:20:07 -08001198 * This routine prevents the kernel scheduler from making @a thread
1199 * the current thread. All other internal operations on @a thread are
1200 * still performed; for example, kernel objects it is waiting on are
1201 * still handed to it. Note that any existing timeouts
1202 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1203 * will be canceled. On resume, the thread will begin running
1204 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * If @a thread is already suspended, the routine has no effect.
1207 *
1208 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 *
1210 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001211 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001212 */
Andrew Boie468190a2017-09-29 14:00:48 -07001213__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214
1215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001216 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001218 * This routine allows the kernel scheduler to make @a thread the current
1219 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * If @a thread is not currently suspended, the routine has no effect.
1222 *
1223 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
1225 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001226 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 */
Andrew Boie468190a2017-09-29 14:00:48 -07001228__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * This routine specifies how the scheduler will perform time slicing of
1234 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * To enable time slicing, @a slice must be non-zero. The scheduler
1237 * ensures that no thread runs for more than the specified time limit
1238 * before other threads of that priority are given a chance to execute.
1239 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001240 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001243 * execute. Once the scheduler selects a thread for execution, there is no
1244 * minimum guaranteed time the thread will execute before threads of greater or
1245 * equal priority are scheduled.
1246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 * for execution, this routine has no effect; the thread is immediately
1249 * rescheduled after the slice period expires.
1250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * To disable timeslicing, set both @a slice and @a prio to zero.
1252 *
1253 * @param slice Maximum time slice length (in milliseconds).
1254 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001255 *
1256 * @return N/A
1257 */
Kumar Galacc334c72017-04-21 10:55:34 -05001258extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001259
Anas Nashif166f5192018-02-25 08:02:36 -06001260/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001261
1262/**
1263 * @addtogroup isr_apis
1264 * @{
1265 */
1266
1267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269 *
Allan Stephensc98da842016-11-11 15:45:03 -05001270 * This routine allows the caller to customize its actions, depending on
1271 * whether it is a thread or an ISR.
1272 *
1273 * @note Can be called by ISRs.
1274 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001275 * @return false if invoked by a thread.
1276 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001277 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001278extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279
Benjamin Walsh445830d2016-11-10 15:54:27 -05001280/**
1281 * @brief Determine if code is running in a preemptible thread.
1282 *
Allan Stephensc98da842016-11-11 15:45:03 -05001283 * This routine allows the caller to customize its actions, depending on
1284 * whether it can be preempted by another thread. The routine returns a 'true'
1285 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001286 *
Allan Stephensc98da842016-11-11 15:45:03 -05001287 * - The code is running in a thread, not at ISR.
1288 * - The thread's priority is in the preemptible range.
1289 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001290 *
Allan Stephensc98da842016-11-11 15:45:03 -05001291 * @note Can be called by ISRs.
1292 *
1293 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001294 * @return Non-zero if invoked by a preemptible thread.
1295 */
Andrew Boie468190a2017-09-29 14:00:48 -07001296__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001297
Allan Stephensc98da842016-11-11 15:45:03 -05001298/**
Peter Bigot74ef3952019-12-23 11:48:43 -06001299 * @brief Test whether startup is in the before-main-task phase.
1300 *
1301 * This routine allows the caller to customize its actions, depending on
1302 * whether it being invoked before the kernel is fully active.
1303 *
1304 * @note Can be called by ISRs.
1305 *
1306 * @return true if invoked before post-kernel initialization
1307 * @return false if invoked during/after post-kernel initialization
1308 */
1309static inline bool k_is_pre_kernel(void)
1310{
1311 extern bool z_sys_post_kernel; /* in init.c */
1312
1313 return !z_sys_post_kernel;
1314}
1315
1316/**
Anas Nashif166f5192018-02-25 08:02:36 -06001317 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001318 */
1319
1320/**
1321 * @addtogroup thread_apis
1322 * @{
1323 */
1324
1325/**
1326 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001327 *
Allan Stephensc98da842016-11-11 15:45:03 -05001328 * This routine prevents the current thread from being preempted by another
1329 * thread by instructing the scheduler to treat it as a cooperative thread.
1330 * If the thread subsequently performs an operation that makes it unready,
1331 * it will be context switched out in the normal manner. When the thread
1332 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001333 *
Allan Stephensc98da842016-11-11 15:45:03 -05001334 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001335 *
Allan Stephensc98da842016-11-11 15:45:03 -05001336 * @note k_sched_lock() and k_sched_unlock() should normally be used
1337 * when the operation being performed can be safely interrupted by ISRs.
1338 * However, if the amount of processing involved is very small, better
1339 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001340 *
1341 * @return N/A
1342 */
1343extern void k_sched_lock(void);
1344
Allan Stephensc98da842016-11-11 15:45:03 -05001345/**
1346 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001347 *
Allan Stephensc98da842016-11-11 15:45:03 -05001348 * This routine reverses the effect of a previous call to k_sched_lock().
1349 * A thread must call the routine once for each time it called k_sched_lock()
1350 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001351 *
1352 * @return N/A
1353 */
1354extern void k_sched_unlock(void);
1355
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001357 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * Custom data is not used by the kernel itself, and is freely available
1362 * for a thread to use as it sees fit. It can be used as a framework
1363 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 *
1367 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001368 *
1369 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001370 */
Andrew Boie468190a2017-09-29 14:00:48 -07001371__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001372
1373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001374 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001379 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001380 */
Andrew Boie468190a2017-09-29 14:00:48 -07001381__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001382
1383/**
Anas Nashif57554052018-03-03 02:31:05 -06001384 * @brief Set current thread name
1385 *
1386 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1387 * tracing and debugging.
1388 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001389 * @param thread_id Thread to set name, or NULL to set the current thread
1390 * @param value Name string
1391 * @retval 0 on success
1392 * @retval -EFAULT Memory access error with supplied string
1393 * @retval -ENOSYS Thread name configuration option not enabled
1394 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001395 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001396__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001397
1398/**
1399 * @brief Get thread name
1400 *
1401 * Get the name of a thread
1402 *
1403 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001404 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001405 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001406const char *k_thread_name_get(k_tid_t thread_id);
1407
1408/**
1409 * @brief Copy the thread name into a supplied buffer
1410 *
1411 * @param thread_id Thread to obtain name information
1412 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001413 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001414 * @retval -ENOSPC Destination buffer too small
1415 * @retval -EFAULT Memory access error
1416 * @retval -ENOSYS Thread name feature not enabled
1417 * @retval 0 Success
1418 */
1419__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1420 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001421
1422/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001423 * @brief Get thread state string
1424 *
1425 * Get the human friendly thread state string
1426 *
1427 * @param thread_id Thread ID
1428 * @retval Thread state string, empty if no state flag is set
1429 */
1430const char *k_thread_state_str(k_tid_t thread_id);
1431
1432/**
Andy Rosscfe62032018-09-29 07:34:55 -07001433 * @}
1434 */
1435
1436/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001437 * @addtogroup clock_apis
1438 * @{
1439 */
1440
1441/**
1442 * @brief Generate null timeout delay.
1443 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001444 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001445 * not to wait if the requested operation cannot be performed immediately.
1446 *
1447 * @return Timeout delay value.
1448 */
1449#define K_NO_WAIT 0
1450
1451/**
1452 * @brief Generate timeout delay from milliseconds.
1453 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001454 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001455 * to wait up to @a ms milliseconds to perform the requested operation.
1456 *
1457 * @param ms Duration in milliseconds.
1458 *
1459 * @return Timeout delay value.
1460 */
Johan Hedberg14471692016-11-13 10:52:15 +02001461#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001462
1463/**
1464 * @brief Generate timeout delay from seconds.
1465 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001466 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001467 * to wait up to @a s seconds to perform the requested operation.
1468 *
1469 * @param s Duration in seconds.
1470 *
1471 * @return Timeout delay value.
1472 */
Johan Hedberg14471692016-11-13 10:52:15 +02001473#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001474
1475/**
1476 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001477
1478 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001479 * to wait up to @a m minutes to perform the requested operation.
1480 *
1481 * @param m Duration in minutes.
1482 *
1483 * @return Timeout delay value.
1484 */
Johan Hedberg14471692016-11-13 10:52:15 +02001485#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001486
1487/**
1488 * @brief Generate timeout delay from hours.
1489 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001490 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001491 * to wait up to @a h hours to perform the requested operation.
1492 *
1493 * @param h Duration in hours.
1494 *
1495 * @return Timeout delay value.
1496 */
Johan Hedberg14471692016-11-13 10:52:15 +02001497#define K_HOURS(h) K_MINUTES((h) * 60)
1498
Allan Stephensc98da842016-11-11 15:45:03 -05001499/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001500 * @brief Generate infinite timeout delay.
1501 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001502 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001503 * to wait as long as necessary to perform the requested operation.
1504 *
1505 * @return Timeout delay value.
1506 */
1507#define K_FOREVER (-1)
1508
1509/**
Anas Nashif166f5192018-02-25 08:02:36 -06001510 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001511 */
1512
1513/**
Allan Stephensc98da842016-11-11 15:45:03 -05001514 * @cond INTERNAL_HIDDEN
1515 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001516
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001517struct k_timer {
1518 /*
1519 * _timeout structure must be first here if we want to use
1520 * dynamic timer allocation. timeout.node is used in the double-linked
1521 * list of free timers
1522 */
1523 struct _timeout timeout;
1524
Allan Stephens45bfa372016-10-12 12:39:42 -05001525 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001526 _wait_q_t wait_q;
1527
1528 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001529 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530
1531 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001532 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533
1534 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001535 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536
Allan Stephens45bfa372016-10-12 12:39:42 -05001537 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001538 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001539
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001540 /* user-specific data, also used to support legacy features */
1541 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001543 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001544 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545};
1546
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001547#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001548 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001549 .timeout = { \
1550 .node = {},\
1551 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001552 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001553 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001554 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001555 .expiry_fn = expiry, \
1556 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001557 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001558 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001559 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001560 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561 }
1562
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001563#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001564
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001565/**
Allan Stephensc98da842016-11-11 15:45:03 -05001566 * INTERNAL_HIDDEN @endcond
1567 */
1568
1569/**
1570 * @defgroup timer_apis Timer APIs
1571 * @ingroup kernel_apis
1572 * @{
1573 */
1574
1575/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001576 * @typedef k_timer_expiry_t
1577 * @brief Timer expiry function type.
1578 *
1579 * A timer's expiry function is executed by the system clock interrupt handler
1580 * each time the timer expires. The expiry function is optional, and is only
1581 * invoked if the timer has been initialized with one.
1582 *
1583 * @param timer Address of timer.
1584 *
1585 * @return N/A
1586 */
1587typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1588
1589/**
1590 * @typedef k_timer_stop_t
1591 * @brief Timer stop function type.
1592 *
1593 * A timer's stop function is executed if the timer is stopped prematurely.
1594 * The function runs in the context of the thread that stops the timer.
1595 * The stop function is optional, and is only invoked if the timer has been
1596 * initialized with one.
1597 *
1598 * @param timer Address of timer.
1599 *
1600 * @return N/A
1601 */
1602typedef void (*k_timer_stop_t)(struct k_timer *timer);
1603
1604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001608 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001609 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001610 *
1611 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @param expiry_fn Function to invoke each time the timer expires.
1613 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001614 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001615#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001616 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001617 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001618
Allan Stephens45bfa372016-10-12 12:39:42 -05001619/**
1620 * @brief Initialize a timer.
1621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001623 *
1624 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @param expiry_fn Function to invoke each time the timer expires.
1626 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001627 *
1628 * @return N/A
1629 */
1630extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001631 k_timer_expiry_t expiry_fn,
1632 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001633
Allan Stephens45bfa372016-10-12 12:39:42 -05001634/**
1635 * @brief Start a timer.
1636 *
1637 * This routine starts a timer, and resets its status to zero. The timer
1638 * begins counting down using the specified duration and period values.
1639 *
1640 * Attempting to start a timer that is already running is permitted.
1641 * The timer's status is reset to zero and the timer begins counting down
1642 * using the new duration and period values.
1643 *
1644 * @param timer Address of timer.
1645 * @param duration Initial timer duration (in milliseconds).
1646 * @param period Timer period (in milliseconds).
1647 *
1648 * @return N/A
1649 */
Andrew Boiea354d492017-09-29 16:22:28 -07001650__syscall void k_timer_start(struct k_timer *timer,
1651 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001652
1653/**
1654 * @brief Stop a timer.
1655 *
1656 * This routine stops a running timer prematurely. The timer's stop function,
1657 * if one exists, is invoked by the caller.
1658 *
1659 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001660 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001661 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001662 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1663 * if @a k_timer_stop is to be called from ISRs.
1664 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001665 * @param timer Address of timer.
1666 *
1667 * @return N/A
1668 */
Andrew Boiea354d492017-09-29 16:22:28 -07001669__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001670
1671/**
1672 * @brief Read timer status.
1673 *
1674 * This routine reads the timer's status, which indicates the number of times
1675 * it has expired since its status was last read.
1676 *
1677 * Calling this routine resets the timer's status to zero.
1678 *
1679 * @param timer Address of timer.
1680 *
1681 * @return Timer status.
1682 */
Andrew Boiea354d492017-09-29 16:22:28 -07001683__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001684
1685/**
1686 * @brief Synchronize thread to timer expiration.
1687 *
1688 * This routine blocks the calling thread until the timer's status is non-zero
1689 * (indicating that it has expired at least once since it was last examined)
1690 * or the timer is stopped. If the timer status is already non-zero,
1691 * or the timer is already stopped, the caller continues without waiting.
1692 *
1693 * Calling this routine resets the timer's status to zero.
1694 *
1695 * This routine must not be used by interrupt handlers, since they are not
1696 * allowed to block.
1697 *
1698 * @param timer Address of timer.
1699 *
1700 * @return Timer status.
1701 */
Andrew Boiea354d492017-09-29 16:22:28 -07001702__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001703
Andy Ross52e444b2018-09-28 09:06:37 -07001704extern s32_t z_timeout_remaining(struct _timeout *timeout);
1705
Allan Stephens45bfa372016-10-12 12:39:42 -05001706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001707 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001708 *
1709 * This routine computes the (approximate) time remaining before a running
1710 * timer next expires. If the timer is not running, it returns zero.
1711 *
1712 * @param timer Address of timer.
1713 *
1714 * @return Remaining time (in milliseconds).
1715 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001716__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001717
Patrik Flykt4344e272019-03-08 14:19:05 -07001718static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001719{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001720 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001721 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001722}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001723
Allan Stephensc98da842016-11-11 15:45:03 -05001724/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001725 * @brief Associate user-specific data with a timer.
1726 *
1727 * This routine records the @a user_data with the @a timer, to be retrieved
1728 * later.
1729 *
1730 * It can be used e.g. in a timer handler shared across multiple subsystems to
1731 * retrieve data specific to the subsystem this timer is associated with.
1732 *
1733 * @param timer Address of timer.
1734 * @param user_data User data to associate with the timer.
1735 *
1736 * @return N/A
1737 */
Andrew Boiea354d492017-09-29 16:22:28 -07001738__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1739
Anas Nashif954d5502018-02-25 08:37:28 -06001740/**
1741 * @internal
1742 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001743static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001744 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001745{
1746 timer->user_data = user_data;
1747}
1748
1749/**
1750 * @brief Retrieve the user-specific data from a timer.
1751 *
1752 * @param timer Address of timer.
1753 *
1754 * @return The user data.
1755 */
Andrew Boiea354d492017-09-29 16:22:28 -07001756__syscall void *k_timer_user_data_get(struct k_timer *timer);
1757
Patrik Flykt4344e272019-03-08 14:19:05 -07001758static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001759{
1760 return timer->user_data;
1761}
1762
Anas Nashif166f5192018-02-25 08:02:36 -06001763/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001764
Allan Stephensc98da842016-11-11 15:45:03 -05001765/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001766 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001767 * @{
1768 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001769
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * This routine returns the elapsed time since the system booted,
1774 * in milliseconds.
1775 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001776 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001777 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001778 * While this function returns time in milliseconds, it does
1779 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001780 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001781 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001782 *
1783 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001784 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001785__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001786
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001787/**
1788 * @brief Enable clock always on in tickless kernel
1789 *
Andy Ross1db9f182019-06-25 10:09:45 -07001790 * Deprecated. This does nothing (it was always just a hint). This
1791 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1792 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001793 *
1794 * @retval prev_status Previous status of always on flag
1795 */
Andy Ross1db9f182019-06-25 10:09:45 -07001796/* LCOV_EXCL_START */
1797__deprecated static inline int k_enable_sys_clock_always_on(void)
1798{
1799 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1800 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1801
1802 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1803}
1804/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001805
1806/**
1807 * @brief Disable clock always on in tickless kernel
1808 *
Andy Ross1db9f182019-06-25 10:09:45 -07001809 * Deprecated. This does nothing (it was always just a hint). This
1810 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1811 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001812 */
Andy Ross1db9f182019-06-25 10:09:45 -07001813/* LCOV_EXCL_START */
1814__deprecated static inline void k_disable_sys_clock_always_on(void)
1815{
1816 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1817 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1818}
1819/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001820
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001823 *
Peter Bigota6067a32019-08-28 08:19:26 -05001824 * This routine returns the lower 32 bits of the system uptime in
1825 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001826 *
Peter Bigota6067a32019-08-28 08:19:26 -05001827 * Because correct conversion requires full precision of the system
1828 * clock there is no benefit to using this over k_uptime_get() unless
1829 * you know the application will never run long enough for the system
1830 * clock to approach 2^32 ticks. Calls to this function may involve
1831 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001832 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001833 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001834 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001835 * While this function returns time in milliseconds, it does
1836 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001837 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001838 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001839 *
Peter Bigota6067a32019-08-28 08:19:26 -05001840 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001841 */
Peter Bigota6067a32019-08-28 08:19:26 -05001842static inline u32_t k_uptime_get_32(void)
1843{
1844 return (u32_t)k_uptime_get();
1845}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001846
1847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * This routine computes the elapsed time between the current system uptime
1851 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001853 * @param reftime Pointer to a reference time, which is updated to the current
1854 * uptime upon return.
1855 *
1856 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001857 */
Andy Ross987c0e52018-09-27 16:50:00 -07001858static inline s64_t k_uptime_delta(s64_t *reftime)
1859{
1860 s64_t uptime, delta;
1861
1862 uptime = k_uptime_get();
1863 delta = uptime - *reftime;
1864 *reftime = uptime;
1865
1866 return delta;
1867}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001868
1869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * This routine computes the elapsed time between the current system uptime
1873 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1876 * need for interrupt locking and 64-bit math. However, the 32-bit result
1877 * cannot hold an elapsed time larger than approximately 50 days, so the
1878 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * @param reftime Pointer to a reference time, which is updated to the current
1881 * uptime upon return.
1882 *
1883 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001884 */
Andy Ross987c0e52018-09-27 16:50:00 -07001885static inline u32_t k_uptime_delta_32(s64_t *reftime)
1886{
1887 return (u32_t)k_uptime_delta(reftime);
1888}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001889
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * This routine returns the current time, as measured by the system's hardware
1894 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001897 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001898static inline u32_t k_cycle_get_32(void)
1899{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001900 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001901}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903/**
Anas Nashif166f5192018-02-25 08:02:36 -06001904 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905 */
1906
Allan Stephensc98da842016-11-11 15:45:03 -05001907/**
1908 * @cond INTERNAL_HIDDEN
1909 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001910
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001911struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001912 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001913 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001914 union {
1915 _wait_q_t wait_q;
1916
1917 _POLL_EVENT;
1918 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001919
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001920 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001921 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001922};
1923
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001924#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001925 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001926 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001927 .lock = { }, \
1928 { \
1929 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1930 _POLL_EVENT_OBJ_INIT(obj) \
1931 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001932 _OBJECT_TRACING_INIT \
1933 }
1934
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001935#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001936
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001937extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1938
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001939/**
1940 * INTERNAL_HIDDEN @endcond
1941 */
1942
1943/**
1944 * @defgroup queue_apis Queue APIs
1945 * @ingroup kernel_apis
1946 * @{
1947 */
1948
1949/**
1950 * @brief Initialize a queue.
1951 *
1952 * This routine initializes a queue object, prior to its first use.
1953 *
1954 * @param queue Address of the queue.
1955 *
1956 * @return N/A
1957 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001958__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001959
1960/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001961 * @brief Cancel waiting on a queue.
1962 *
1963 * This routine causes first thread pending on @a queue, if any, to
1964 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001965 * If the queue is being waited on by k_poll(), it will return with
1966 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1967 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001968 *
1969 * @note Can be called by ISRs.
1970 *
1971 * @param queue Address of the queue.
1972 *
1973 * @return N/A
1974 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001975__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001976
1977/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978 * @brief Append an element to the end of a queue.
1979 *
1980 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001981 * aligned on a word boundary, and the first word of the item is reserved
1982 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001983 *
1984 * @note Can be called by ISRs.
1985 *
1986 * @param queue Address of the queue.
1987 * @param data Address of the data item.
1988 *
1989 * @return N/A
1990 */
1991extern void k_queue_append(struct k_queue *queue, void *data);
1992
1993/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001994 * @brief Append an element to a queue.
1995 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001996 * This routine appends a data item to @a queue. There is an implicit memory
1997 * allocation to create an additional temporary bookkeeping data structure from
1998 * the calling thread's resource pool, which is automatically freed when the
1999 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002000 *
2001 * @note Can be called by ISRs.
2002 *
2003 * @param queue Address of the queue.
2004 * @param data Address of the data item.
2005 *
2006 * @retval 0 on success
2007 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2008 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302009__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002010
2011/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002012 * @brief Prepend an element to a queue.
2013 *
2014 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002015 * aligned on a word boundary, and the first word of the item is reserved
2016 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002017 *
2018 * @note Can be called by ISRs.
2019 *
2020 * @param queue Address of the queue.
2021 * @param data Address of the data item.
2022 *
2023 * @return N/A
2024 */
2025extern void k_queue_prepend(struct k_queue *queue, void *data);
2026
2027/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002028 * @brief Prepend an element to a queue.
2029 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002030 * This routine prepends a data item to @a queue. There is an implicit memory
2031 * allocation to create an additional temporary bookkeeping data structure from
2032 * the calling thread's resource pool, which is automatically freed when the
2033 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002034 *
2035 * @note Can be called by ISRs.
2036 *
2037 * @param queue Address of the queue.
2038 * @param data Address of the data item.
2039 *
2040 * @retval 0 on success
2041 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2042 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302043__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002044
2045/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002046 * @brief Inserts an element to a queue.
2047 *
2048 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002049 * data item must be aligned on a word boundary, and the first word of
2050 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002051 *
2052 * @note Can be called by ISRs.
2053 *
2054 * @param queue Address of the queue.
2055 * @param prev Address of the previous data item.
2056 * @param data Address of the data item.
2057 *
2058 * @return N/A
2059 */
2060extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2061
2062/**
2063 * @brief Atomically append a list of elements to a queue.
2064 *
2065 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002066 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002067 * in each data item pointing to the next data item; the list must be
2068 * NULL-terminated.
2069 *
2070 * @note Can be called by ISRs.
2071 *
2072 * @param queue Address of the queue.
2073 * @param head Pointer to first node in singly-linked list.
2074 * @param tail Pointer to last node in singly-linked list.
2075 *
2076 * @return N/A
2077 */
2078extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
2079
2080/**
2081 * @brief Atomically add a list of elements to a queue.
2082 *
2083 * This routine adds a list of data items to @a queue in one operation.
2084 * The data items must be in a singly-linked list implemented using a
2085 * sys_slist_t object. Upon completion, the original list is empty.
2086 *
2087 * @note Can be called by ISRs.
2088 *
2089 * @param queue Address of the queue.
2090 * @param list Pointer to sys_slist_t object.
2091 *
2092 * @return N/A
2093 */
2094extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2095
2096/**
2097 * @brief Get an element from a queue.
2098 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002099 * This routine removes first data item from @a queue. The first word of the
2100 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002101 *
2102 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2103 *
2104 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002105 * @param timeout Non-negative waiting period to obtain a data item (in
2106 * milliseconds), or one of the special values K_NO_WAIT and
2107 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002108 *
2109 * @return Address of the data item if successful; NULL if returned
2110 * without waiting, or waiting period timed out.
2111 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002112__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002113
2114/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002115 * @brief Remove an element from a queue.
2116 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002117 * This routine removes data item from @a queue. The first word of the
2118 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002119 * rely on sys_slist_find_and_remove which is not a constant time operation.
2120 *
2121 * @note Can be called by ISRs
2122 *
2123 * @param queue Address of the queue.
2124 * @param data Address of the data item.
2125 *
2126 * @return true if data item was removed
2127 */
2128static inline bool k_queue_remove(struct k_queue *queue, void *data)
2129{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002130 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002131}
2132
2133/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002134 * @brief Append an element to a queue only if it's not present already.
2135 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002136 * This routine appends data item to @a queue. The first word of the data
2137 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002138 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2139 *
2140 * @note Can be called by ISRs
2141 *
2142 * @param queue Address of the queue.
2143 * @param data Address of the data item.
2144 *
2145 * @return true if data item was added, false if not
2146 */
2147static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2148{
2149 sys_sfnode_t *test;
2150
2151 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2152 if (test == (sys_sfnode_t *) data) {
2153 return false;
2154 }
2155 }
2156
2157 k_queue_append(queue, data);
2158 return true;
2159}
2160
2161/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002162 * @brief Query a queue to see if it has data available.
2163 *
2164 * Note that the data might be already gone by the time this function returns
2165 * if other threads are also trying to read from the queue.
2166 *
2167 * @note Can be called by ISRs.
2168 *
2169 * @param queue Address of the queue.
2170 *
2171 * @return Non-zero if the queue is empty.
2172 * @return 0 if data is available.
2173 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002174__syscall int k_queue_is_empty(struct k_queue *queue);
2175
Patrik Flykt4344e272019-03-08 14:19:05 -07002176static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002177{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002178 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002179}
2180
2181/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002182 * @brief Peek element at the head of queue.
2183 *
2184 * Return element from the head of queue without removing it.
2185 *
2186 * @param queue Address of the queue.
2187 *
2188 * @return Head element, or NULL if queue is empty.
2189 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002190__syscall void *k_queue_peek_head(struct k_queue *queue);
2191
Patrik Flykt4344e272019-03-08 14:19:05 -07002192static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002193{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002194 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002195}
2196
2197/**
2198 * @brief Peek element at the tail of queue.
2199 *
2200 * Return element from the tail of queue without removing it.
2201 *
2202 * @param queue Address of the queue.
2203 *
2204 * @return Tail element, or NULL if queue is empty.
2205 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002206__syscall void *k_queue_peek_tail(struct k_queue *queue);
2207
Patrik Flykt4344e272019-03-08 14:19:05 -07002208static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002209{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002210 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002211}
2212
2213/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002214 * @brief Statically define and initialize a queue.
2215 *
2216 * The queue can be accessed outside the module where it is defined using:
2217 *
2218 * @code extern struct k_queue <name>; @endcode
2219 *
2220 * @param name Name of the queue.
2221 */
2222#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002223 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002224 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002225
Anas Nashif166f5192018-02-25 08:02:36 -06002226/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002227
Wentong Wu5611e922019-06-20 23:51:27 +08002228#ifdef CONFIG_USERSPACE
2229/**
2230 * @brief futex structure
2231 *
2232 * A k_futex is a lightweight mutual exclusion primitive designed
2233 * to minimize kernel involvement. Uncontended operation relies
2234 * only on atomic access to shared memory. k_futex are tracked as
2235 * kernel objects and can live in user memory so any access bypass
2236 * the kernel object permission management mechanism.
2237 */
2238struct k_futex {
2239 atomic_t val;
2240};
2241
2242/**
2243 * @brief futex kernel data structure
2244 *
2245 * z_futex_data are the helper data structure for k_futex to complete
2246 * futex contended operation on kernel side, structure z_futex_data
2247 * of every futex object is invisible in user mode.
2248 */
2249struct z_futex_data {
2250 _wait_q_t wait_q;
2251 struct k_spinlock lock;
2252};
2253
2254#define Z_FUTEX_DATA_INITIALIZER(obj) \
2255 { \
2256 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2257 }
2258
2259/**
2260 * @defgroup futex_apis FUTEX APIs
2261 * @ingroup kernel_apis
2262 * @{
2263 */
2264
2265/**
Wentong Wu5611e922019-06-20 23:51:27 +08002266 * @brief Pend the current thread on a futex
2267 *
2268 * Tests that the supplied futex contains the expected value, and if so,
2269 * goes to sleep until some other thread calls k_futex_wake() on it.
2270 *
2271 * @param futex Address of the futex.
2272 * @param expected Expected value of the futex, if it is different the caller
2273 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002274 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2275 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002276 * @retval -EACCES Caller does not have read access to futex address.
2277 * @retval -EAGAIN If the futex value did not match the expected parameter.
2278 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2279 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2280 * @retval 0 if the caller went to sleep and was woken up. The caller
2281 * should check the futex's value on wakeup to determine if it needs
2282 * to block again.
2283 */
2284__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2285
2286/**
2287 * @brief Wake one/all threads pending on a futex
2288 *
2289 * Wake up the highest priority thread pending on the supplied futex, or
2290 * wakeup all the threads pending on the supplied futex, and the behavior
2291 * depends on wake_all.
2292 *
2293 * @param futex Futex to wake up pending threads.
2294 * @param wake_all If true, wake up all pending threads; If false,
2295 * wakeup the highest priority thread.
2296 * @retval -EACCES Caller does not have access to the futex address.
2297 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2298 * @retval Number of threads that were woken up.
2299 */
2300__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2301
2302/** @} */
2303#endif
2304
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002306 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307};
2308
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002309/**
2310 * @cond INTERNAL_HIDDEN
2311 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002312#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002313 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002314 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002315 }
2316
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002317#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002318
Allan Stephensc98da842016-11-11 15:45:03 -05002319/**
2320 * INTERNAL_HIDDEN @endcond
2321 */
2322
2323/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002324 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002325 * @ingroup kernel_apis
2326 * @{
2327 */
2328
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002330 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002332 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002334 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002335 *
2336 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002337 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002339#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002340 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341
2342/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002343 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002344 *
2345 * This routine causes first thread pending on @a fifo, if any, to
2346 * return from k_fifo_get() call with NULL value (as if timeout
2347 * expired).
2348 *
2349 * @note Can be called by ISRs.
2350 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002351 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002352 *
2353 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002354 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002355 */
2356#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002357 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002358
2359/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002360 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002362 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002363 * aligned on a word boundary, and the first word of the item is reserved
2364 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * @note Can be called by ISRs.
2367 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002368 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370 *
2371 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002372 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002374#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002375 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002376
2377/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002378 * @brief Add an element to a FIFO queue.
2379 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002380 * This routine adds a data item to @a fifo. There is an implicit memory
2381 * allocation to create an additional temporary bookkeeping data structure from
2382 * the calling thread's resource pool, which is automatically freed when the
2383 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002384 *
2385 * @note Can be called by ISRs.
2386 *
2387 * @param fifo Address of the FIFO.
2388 * @param data Address of the data item.
2389 *
2390 * @retval 0 on success
2391 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002392 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002393 */
2394#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002395 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002396
2397/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002398 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002400 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002401 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 * each data item pointing to the next data item; the list must be
2403 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002407 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * @param head Pointer to first node in singly-linked list.
2409 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002410 *
2411 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002412 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002414#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002415 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416
2417/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002418 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002420 * This routine adds a list of data items to @a fifo in one operation.
2421 * The data items must be in a singly-linked list implemented using a
2422 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 * and must be re-initialized via sys_slist_init().
2424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * @note Can be called by ISRs.
2426 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002427 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002429 *
2430 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002431 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002433#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002434 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435
2436/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002437 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002440 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2443 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002444 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446 * or one of the special values K_NO_WAIT and K_FOREVER.
2447 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002448 * @return Address of the data item if successful; NULL if returned
2449 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002450 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002451 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002452#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002453 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002456 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002457 *
2458 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002459 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002460 *
2461 * @note Can be called by ISRs.
2462 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002463 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002464 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002465 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002466 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002467 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002468 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002469#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002470 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002471
2472/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002473 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002474 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002475 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302476 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002477 * on each iteration of processing, a head container will be peeked,
2478 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002479 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002480 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002481 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002482 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002483 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002484 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002485 */
2486#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002487 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002488
2489/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002490 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002491 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002492 * Return element from the tail of FIFO queue (without removing it). A usecase
2493 * for this is if elements of the FIFO queue are themselves containers. Then
2494 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002495 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002496 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002497 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002498 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002499 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002500 */
2501#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002502 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002503
2504/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002505 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002507 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002509 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002511 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002512 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002515 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002516 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517
Anas Nashif166f5192018-02-25 08:02:36 -06002518/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002519
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002521 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522};
2523
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002524/**
2525 * @cond INTERNAL_HIDDEN
2526 */
2527
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002528#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002529 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002530 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002531 }
2532
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002533#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002534
Allan Stephensc98da842016-11-11 15:45:03 -05002535/**
2536 * INTERNAL_HIDDEN @endcond
2537 */
2538
2539/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002540 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002541 * @ingroup kernel_apis
2542 * @{
2543 */
2544
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002545/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002546 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002548 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002550 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002551 *
2552 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002553 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002554 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002555#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002556 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002557
2558/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002559 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002560 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002561 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002562 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002563 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002565 * @note Can be called by ISRs.
2566 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002567 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002569 *
2570 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002571 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002572 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002573#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002574 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575
2576/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002577 * @brief Add an element to a LIFO queue.
2578 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002579 * This routine adds a data item to @a lifo. There is an implicit memory
2580 * allocation to create an additional temporary bookkeeping data structure from
2581 * the calling thread's resource pool, which is automatically freed when the
2582 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002583 *
2584 * @note Can be called by ISRs.
2585 *
2586 * @param lifo Address of the LIFO.
2587 * @param data Address of the data item.
2588 *
2589 * @retval 0 on success
2590 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002591 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002592 */
2593#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002594 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002595
2596/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002597 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002600 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2603 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002604 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002606 * or one of the special values K_NO_WAIT and K_FOREVER.
2607 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002608 * @return Address of the data item if successful; NULL if returned
2609 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002610 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002611 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002612#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002613 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002615/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002616 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002617 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002618 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002619 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002620 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002622 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002623 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002625#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002626 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002627 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628
Anas Nashif166f5192018-02-25 08:02:36 -06002629/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002630
2631/**
2632 * @cond INTERNAL_HIDDEN
2633 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302634#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002636typedef uintptr_t stack_data_t;
2637
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638struct k_stack {
2639 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002640 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002641 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002643 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002644 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002645 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646};
2647
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002648#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002649 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002650 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002651 .base = stack_buffer, \
2652 .next = stack_buffer, \
2653 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002654 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002655 }
2656
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002657#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002658
Allan Stephensc98da842016-11-11 15:45:03 -05002659/**
2660 * INTERNAL_HIDDEN @endcond
2661 */
2662
2663/**
2664 * @defgroup stack_apis Stack APIs
2665 * @ingroup kernel_apis
2666 * @{
2667 */
2668
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669/**
2670 * @brief Initialize a stack.
2671 *
2672 * This routine initializes a stack object, prior to its first use.
2673 *
2674 * @param stack Address of the stack.
2675 * @param buffer Address of array used to hold stacked values.
2676 * @param num_entries Maximum number of values that can be stacked.
2677 *
2678 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002679 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 */
Andrew Boief3bee952018-05-02 17:44:39 -07002681void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002682 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002683
2684
2685/**
2686 * @brief Initialize a stack.
2687 *
2688 * This routine initializes a stack object, prior to its first use. Internal
2689 * buffers will be allocated from the calling thread's resource pool.
2690 * This memory will be released if k_stack_cleanup() is called, or
2691 * userspace is enabled and the stack object loses all references to it.
2692 *
2693 * @param stack Address of the stack.
2694 * @param num_entries Maximum number of values that can be stacked.
2695 *
2696 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002697 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002698 */
2699
Adithya Baglody28080d32018-10-15 11:48:51 +05302700__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2701 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002702
2703/**
2704 * @brief Release a stack's allocated buffer
2705 *
2706 * If a stack object was given a dynamically allocated buffer via
2707 * k_stack_alloc_init(), this will free it. This function does nothing
2708 * if the buffer wasn't dynamically allocated.
2709 *
2710 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002711 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002712 */
2713void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714
2715/**
2716 * @brief Push an element onto a stack.
2717 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002718 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719 *
2720 * @note Can be called by ISRs.
2721 *
2722 * @param stack Address of the stack.
2723 * @param data Value to push onto the stack.
2724 *
2725 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002726 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002728__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729
2730/**
2731 * @brief Pop an element from a stack.
2732 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002733 * This routine removes a stack_data_t value from @a stack in a "last in,
2734 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 *
2736 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2737 *
2738 * @param stack Address of the stack.
2739 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002740 * @param timeout Non-negative waiting period to obtain a value (in
2741 * milliseconds), or one of the special values K_NO_WAIT and
2742 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002743 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002744 * @retval 0 Element popped from stack.
2745 * @retval -EBUSY Returned without waiting.
2746 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002747 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002748 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002749__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2750 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002753 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002756 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002757 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * @param name Name of the stack.
2760 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002761 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002762 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002763#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002764 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002765 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002766 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002767 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002768 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769
Anas Nashif166f5192018-02-25 08:02:36 -06002770/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002771
Allan Stephens6bba9b02016-11-16 14:56:54 -05002772struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002773struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002774
Piotr Zięcik19d83492019-09-27 09:16:25 +02002775/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002776typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2777struct _poller {
2778 volatile bool is_polling;
2779 struct k_thread *thread;
2780 _poller_cb_t cb;
2781};
2782
Allan Stephensc98da842016-11-11 15:45:03 -05002783/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002784 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002785 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 */
2787
Allan Stephens6bba9b02016-11-16 14:56:54 -05002788/**
2789 * @typedef k_work_handler_t
2790 * @brief Work item handler function type.
2791 *
2792 * A work item's handler function is executed by a workqueue's thread
2793 * when the work item is processed by the workqueue.
2794 *
2795 * @param work Address of the work item.
2796 *
2797 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002798 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002799 */
2800typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801
2802/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002803 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002805
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002807 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002808 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809};
2810
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002812 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813};
2814
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002816 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817 k_work_handler_t handler;
2818 atomic_t flags[1];
2819};
2820
Allan Stephens6bba9b02016-11-16 14:56:54 -05002821struct k_delayed_work {
2822 struct k_work work;
2823 struct _timeout timeout;
2824 struct k_work_q *work_q;
2825};
2826
Piotr Zięcik19d83492019-09-27 09:16:25 +02002827struct k_work_poll {
2828 struct k_work work;
2829 struct _poller poller;
2830 struct k_poll_event *events;
2831 int num_events;
2832 k_work_handler_t real_handler;
2833 struct _timeout timeout;
2834 int poll_result;
2835};
2836
Allan Stephens6bba9b02016-11-16 14:56:54 -05002837extern struct k_work_q k_sys_work_q;
2838
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002840 * INTERNAL_HIDDEN @endcond
2841 */
2842
Patrik Flykt4344e272019-03-08 14:19:05 -07002843#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002844 { \
2845 ._reserved = NULL, \
2846 .handler = work_handler, \
2847 .flags = { 0 } \
2848 }
2849
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002850#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002851
Allan Stephens6bba9b02016-11-16 14:56:54 -05002852/**
2853 * @brief Initialize a statically-defined work item.
2854 *
2855 * This macro can be used to initialize a statically-defined workqueue work
2856 * item, prior to its first use. For example,
2857 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002858 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002859 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002860 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002861 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002862 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002864#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002865 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866
2867/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002868 * @brief Initialize a work item.
2869 *
2870 * This routine initializes a workqueue work item, prior to its first use.
2871 *
2872 * @param work Address of work item.
2873 * @param handler Function to invoke each time work item is processed.
2874 *
2875 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002876 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877 */
2878static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2879{
Patrik Flykt4344e272019-03-08 14:19:05 -07002880 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002881}
2882
2883/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002884 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002885 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002886 * This routine submits work item @a work to be processed by workqueue
2887 * @a work_q. If the work item is already pending in the workqueue's queue
2888 * as a result of an earlier submission, this routine has no effect on the
2889 * work item. If the work item has already been processed, or is currently
2890 * being processed, its work is considered complete and the work item can be
2891 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002892 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002893 * @warning
2894 * A submitted work item must not be modified until it has been processed
2895 * by the workqueue.
2896 *
2897 * @note Can be called by ISRs.
2898 *
2899 * @param work_q Address of workqueue.
2900 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002901 *
2902 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002903 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002904 */
2905static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2906 struct k_work *work)
2907{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002908 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002909 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910 }
2911}
2912
2913/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002914 * @brief Submit a work item to a user mode workqueue
2915 *
David B. Kinder06d78352018-12-17 14:32:40 -08002916 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002917 * memory allocation is made from the caller's resource pool which is freed
2918 * once the worker thread consumes the k_work item. The workqueue
2919 * thread must have memory access to the k_work item being submitted. The caller
2920 * must have permission granted on the work_q parameter's queue object.
2921 *
2922 * Otherwise this works the same as k_work_submit_to_queue().
2923 *
2924 * @note Can be called by ISRs.
2925 *
2926 * @param work_q Address of workqueue.
2927 * @param work Address of work item.
2928 *
2929 * @retval -EBUSY if the work item was already in some workqueue
2930 * @retval -ENOMEM if no memory for thread resource pool allocation
2931 * @retval 0 Success
2932 * @req K-WORK-001
2933 */
2934static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2935 struct k_work *work)
2936{
2937 int ret = -EBUSY;
2938
2939 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2940 ret = k_queue_alloc_append(&work_q->queue, work);
2941
2942 /* Couldn't insert into the queue. Clear the pending bit
2943 * so the work item can be submitted again
2944 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002945 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002946 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2947 }
2948 }
2949
2950 return ret;
2951}
2952
2953/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002954 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002955 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002956 * This routine indicates if work item @a work is pending in a workqueue's
2957 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002958 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002959 * @note Can be called by ISRs.
2960 *
2961 * @param work Address of work item.
2962 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002963 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002964 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002965 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002966static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002967{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002968 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002969}
2970
2971/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002972 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002973 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002974 * This routine starts workqueue @a work_q. The workqueue spawns its work
2975 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002977 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002978 * @param stack Pointer to work queue thread's stack space, as defined by
2979 * K_THREAD_STACK_DEFINE()
2980 * @param stack_size Size of the work queue thread's stack (in bytes), which
2981 * should either be the same constant passed to
2982 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002983 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002984 *
2985 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002986 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002987 */
Andrew Boie507852a2017-07-25 18:47:07 -07002988extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002989 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002990 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002992/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002993 * @brief Start a workqueue in user mode
2994 *
2995 * This works identically to k_work_q_start() except it is callable from user
2996 * mode, and the worker thread created will run in user mode.
2997 * The caller must have permissions granted on both the work_q parameter's
2998 * thread and queue objects, and the same restrictions on priority apply as
2999 * k_thread_create().
3000 *
3001 * @param work_q Address of workqueue.
3002 * @param stack Pointer to work queue thread's stack space, as defined by
3003 * K_THREAD_STACK_DEFINE()
3004 * @param stack_size Size of the work queue thread's stack (in bytes), which
3005 * should either be the same constant passed to
3006 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
3007 * @param prio Priority of the work queue's thread.
3008 *
3009 * @return N/A
3010 * @req K-WORK-001
3011 */
3012extern void k_work_q_user_start(struct k_work_q *work_q,
3013 k_thread_stack_t *stack,
3014 size_t stack_size, int prio);
3015
3016/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003017 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003018 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003019 * This routine initializes a workqueue delayed work item, prior to
3020 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003021 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003022 * @param work Address of delayed work item.
3023 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003024 *
3025 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003026 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003028extern void k_delayed_work_init(struct k_delayed_work *work,
3029 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003030
3031/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003032 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003034 * This routine schedules work item @a work to be processed by workqueue
3035 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003036 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003037 * Only when the countdown completes is the work item actually submitted to
3038 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003040 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08003041 * counting down cancels the existing submission and restarts the
3042 * countdown using the new delay. Note that this behavior is
3043 * inherently subject to race conditions with the pre-existing
3044 * timeouts and work queue, so care must be taken to synchronize such
3045 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003046 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003047 * @warning
3048 * A delayed work item must not be modified until it has been processed
3049 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003050 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003051 * @note Can be called by ISRs.
3052 *
3053 * @param work_q Address of workqueue.
3054 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003055 * @param delay Non-negative delay before submitting the work item (in
3056 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003057 *
3058 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003059 * @retval -EINVAL Work item is being processed or has completed its work.
3060 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003061 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003062 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003063extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3064 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003065 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066
3067/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003068 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003070 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003071 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003072 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003074 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003076 * @note The result of calling this on a k_delayed_work item that has
3077 * not been submitted (i.e. before the return of the
3078 * k_delayed_work_submit_to_queue() call) is undefined.
3079 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003080 * @param work Address of delayed work item.
3081 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003082 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003083 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003084 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003086extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003088/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089 * @brief Submit a work item to the system workqueue.
3090 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003091 * This routine submits work item @a work to be processed by the system
3092 * workqueue. If the work item is already pending in the workqueue's queue
3093 * as a result of an earlier submission, this routine has no effect on the
3094 * work item. If the work item has already been processed, or is currently
3095 * being processed, its work is considered complete and the work item can be
3096 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003098 * @warning
3099 * Work items submitted to the system workqueue should avoid using handlers
3100 * that block or yield since this may prevent the system workqueue from
3101 * processing other work items in a timely manner.
3102 *
3103 * @note Can be called by ISRs.
3104 *
3105 * @param work Address of work item.
3106 *
3107 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003108 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109 */
3110static inline void k_work_submit(struct k_work *work)
3111{
3112 k_work_submit_to_queue(&k_sys_work_q, work);
3113}
3114
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003115/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116 * @brief Submit a delayed work item to the system workqueue.
3117 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003118 * This routine schedules work item @a work to be processed by the system
3119 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003120 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003121 * Only when the countdown completes is the work item actually submitted to
3122 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003124 * Submitting a previously submitted delayed work item that is still
3125 * counting down cancels the existing submission and restarts the countdown
3126 * using the new delay. If the work item is currently pending on the
3127 * workqueue's queue because the countdown has completed it is too late to
3128 * resubmit the item, and resubmission fails without impacting the work item.
3129 * If the work item has already been processed, or is currently being processed,
3130 * its work is considered complete and the work item can be resubmitted.
3131 *
3132 * @warning
3133 * Work items submitted to the system workqueue should avoid using handlers
3134 * that block or yield since this may prevent the system workqueue from
3135 * processing other work items in a timely manner.
3136 *
3137 * @note Can be called by ISRs.
3138 *
3139 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003140 * @param delay Non-negative delay before submitting the work item (in
3141 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003142 *
3143 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003144 * @retval -EINVAL Work item is being processed or has completed its work.
3145 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003146 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003147 */
3148static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003149 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003151 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152}
3153
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003155 * @brief Get time remaining before a delayed work gets scheduled.
3156 *
3157 * This routine computes the (approximate) time remaining before a
3158 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003159 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003160 *
3161 * @param work Delayed work item.
3162 *
3163 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003164 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003165 */
Kumar Galacc334c72017-04-21 10:55:34 -05003166static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003167{
Andy Ross88924062019-10-03 11:43:10 -07003168 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003169}
3170
Piotr Zięcik19d83492019-09-27 09:16:25 +02003171/**
3172 * @brief Initialize a triggered work item.
3173 *
3174 * This routine initializes a workqueue triggered work item, prior to
3175 * its first use.
3176 *
3177 * @param work Address of triggered work item.
3178 * @param handler Function to invoke each time work item is processed.
3179 *
3180 * @return N/A
3181 */
3182extern void k_work_poll_init(struct k_work_poll *work,
3183 k_work_handler_t handler);
3184
3185/**
3186 * @brief Submit a triggered work item.
3187 *
3188 * This routine schedules work item @a work to be processed by workqueue
3189 * @a work_q when one of the given @a events is signaled. The routine
3190 * initiates internal poller for the work item and then returns to the caller.
3191 * Only when one of the watched events happen the work item is actually
3192 * submitted to the workqueue and becomes pending.
3193 *
3194 * Submitting a previously submitted triggered work item that is still
3195 * waiting for the event cancels the existing submission and reschedules it
3196 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003197 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003198 * so care must be taken to synchronize such resubmissions externally.
3199 *
3200 * @note Can be called by ISRs.
3201 *
3202 * @warning
3203 * Provided array of events as well as a triggered work item must be placed
3204 * in persistent memory (valid until work handler execution or work
3205 * cancellation) and cannot be modified after submission.
3206 *
3207 * @param work_q Address of workqueue.
3208 * @param work Address of delayed work item.
3209 * @param events An array of pointers to events which trigger the work.
3210 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003211 * @param timeout Non-negative timeout after which the work will be scheduled
3212 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003213 *
3214 *
3215 * @retval 0 Work item started watching for events.
3216 * @retval -EINVAL Work item is being processed or has completed its work.
3217 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3218 */
3219extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3220 struct k_work_poll *work,
3221 struct k_poll_event *events,
3222 int num_events,
3223 s32_t timeout);
3224
3225/**
3226 * @brief Submit a triggered work item to the system workqueue.
3227 *
3228 * This routine schedules work item @a work to be processed by system
3229 * workqueue when one of the given @a events is signaled. The routine
3230 * initiates internal poller for the work item and then returns to the caller.
3231 * Only when one of the watched events happen the work item is actually
3232 * submitted to the workqueue and becomes pending.
3233 *
3234 * Submitting a previously submitted triggered work item that is still
3235 * waiting for the event cancels the existing submission and reschedules it
3236 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003237 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003238 * so care must be taken to synchronize such resubmissions externally.
3239 *
3240 * @note Can be called by ISRs.
3241 *
3242 * @warning
3243 * Provided array of events as well as a triggered work item must not be
3244 * modified until the item has been processed by the workqueue.
3245 *
3246 * @param work Address of delayed work item.
3247 * @param events An array of pointers to events which trigger the work.
3248 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003249 * @param timeout Non-negative timeout after which the work will be scheduled
3250 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003251 *
3252 * @retval 0 Work item started watching for events.
3253 * @retval -EINVAL Work item is being processed or has completed its work.
3254 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3255 */
3256static inline int k_work_poll_submit(struct k_work_poll *work,
3257 struct k_poll_event *events,
3258 int num_events,
3259 s32_t timeout)
3260{
3261 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3262 events, num_events, timeout);
3263}
3264
3265/**
3266 * @brief Cancel a triggered work item.
3267 *
3268 * This routine cancels the submission of triggered work item @a work.
3269 * A triggered work item can only be canceled if no event triggered work
3270 * submission.
3271 *
3272 * @note Can be called by ISRs.
3273 *
3274 * @param work Address of delayed work item.
3275 *
David B. Kinder73896c02019-10-28 16:27:57 -07003276 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003277 * @retval -EINVAL Work item is being processed or has completed its work.
3278 */
3279extern int k_work_poll_cancel(struct k_work_poll *work);
3280
Anas Nashif166f5192018-02-25 08:02:36 -06003281/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003282/**
Anas Nashifce78d162018-05-24 12:43:11 -05003283 * @defgroup mutex_apis Mutex APIs
3284 * @ingroup kernel_apis
3285 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287
Anas Nashifce78d162018-05-24 12:43:11 -05003288/**
3289 * Mutex Structure
3290 * @ingroup mutex_apis
3291 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003292struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05003293 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003295 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003296 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05003297
3298 /** Current lock count */
Kumar Galacc334c72017-04-21 10:55:34 -05003299 u32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05003300
3301 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003304 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003305 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306};
3307
Anas Nashifce78d162018-05-24 12:43:11 -05003308/**
3309 * @cond INTERNAL_HIDDEN
3310 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003311#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003313 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314 .owner = NULL, \
3315 .lock_count = 0, \
3316 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003317 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318 }
3319
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003320#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003321
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003322/**
Allan Stephensc98da842016-11-11 15:45:03 -05003323 * INTERNAL_HIDDEN @endcond
3324 */
3325
3326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003330 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003331 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003332 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003333 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003334 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003335 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003337 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003338 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * Upon completion, the mutex is available and does not have an owner.
3346 *
3347 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003348 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003349 * @retval 0 Mutex object created
3350 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003351 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003352 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003353__syscall int k_mutex_init(struct k_mutex *mutex);
3354
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003355
3356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * This routine locks @a mutex. If the mutex is locked by another thread,
3360 * the calling thread waits until the mutex becomes available or until
3361 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * A thread is permitted to lock a mutex it has already locked. The operation
3364 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003367 * @param timeout Non-negative waiting period to lock the mutex (in
3368 * milliseconds), or one of the special values K_NO_WAIT and
3369 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003370 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003371 * @retval 0 Mutex locked.
3372 * @retval -EBUSY Returned without waiting.
3373 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003374 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003375 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003376__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003377
3378/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * This routine unlocks @a mutex. The mutex must already be locked by the
3382 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003383 *
3384 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003386 * thread.
3387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003389 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003390 * @retval 0 Mutex unlocked.
3391 * @retval -EPERM The current thread does not own the mutex
3392 * @retval -EINVAL The mutex is not locked
3393 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003394 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003395 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003396__syscall int k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397
Allan Stephensc98da842016-11-11 15:45:03 -05003398/**
Anas Nashif166f5192018-02-25 08:02:36 -06003399 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003400 */
3401
3402/**
3403 * @cond INTERNAL_HIDDEN
3404 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003405
3406struct k_sem {
3407 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303408 u32_t count;
3409 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003410 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003412 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003413 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414};
3415
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003416#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003417 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003418 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003419 .count = initial_count, \
3420 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003421 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003422 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003423 }
3424
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003425#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003426
Allan Stephensc98da842016-11-11 15:45:03 -05003427/**
3428 * INTERNAL_HIDDEN @endcond
3429 */
3430
3431/**
3432 * @defgroup semaphore_apis Semaphore APIs
3433 * @ingroup kernel_apis
3434 * @{
3435 */
3436
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * @param sem Address of the semaphore.
3443 * @param initial_count Initial semaphore count.
3444 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003445 *
Anas Nashif928af3c2019-05-04 10:36:14 -04003446 * @retval 0 Semaphore created successfully
3447 * @retval -EINVAL Invalid values
3448 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003449 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003450 */
Anas Nashif928af3c2019-05-04 10:36:14 -04003451__syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count,
Andrew Boie99280232017-09-29 14:17:47 -07003452 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003453
3454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3460 *
3461 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003462 * @param timeout Non-negative waiting period to take the semaphore (in
3463 * milliseconds), or one of the special values K_NO_WAIT and
3464 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003465 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003466 * @retval 0 Semaphore taken.
3467 * @retval -EBUSY Returned without waiting.
3468 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003469 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003470 */
Andrew Boie99280232017-09-29 14:17:47 -07003471__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003472
3473/**
3474 * @brief Give a semaphore.
3475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * This routine gives @a sem, unless the semaphore is already at its maximum
3477 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003482 *
3483 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003484 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003485 */
Andrew Boie99280232017-09-29 14:17:47 -07003486__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003487
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003488/**
3489 * @brief Reset a semaphore's count to zero.
3490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003493 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003494 *
3495 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003496 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003497 */
Andrew Boie990bf162017-10-03 12:36:49 -07003498__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003499
Anas Nashif954d5502018-02-25 08:37:28 -06003500/**
3501 * @internal
3502 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003503static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504{
Patrik Flykt24d71432019-03-26 19:57:45 -06003505 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506}
3507
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003508/**
3509 * @brief Get a semaphore's count.
3510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003511 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003513 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003516 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003517 */
Andrew Boie990bf162017-10-03 12:36:49 -07003518__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003519
Anas Nashif954d5502018-02-25 08:37:28 -06003520/**
3521 * @internal
3522 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003523static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003524{
3525 return sem->count;
3526}
3527
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003532 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003533 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * @param name Name of the semaphore.
3536 * @param initial_count Initial semaphore count.
3537 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003538 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003539 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003541 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003542 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303543 BUILD_ASSERT(((count_limit) != 0) && \
3544 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003545
Anas Nashif166f5192018-02-25 08:02:36 -06003546/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003547
3548/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003549 * @defgroup msgq_apis Message Queue APIs
3550 * @ingroup kernel_apis
3551 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003552 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003553
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003554/**
3555 * @brief Message Queue Structure
3556 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05003558 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003559 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05003560 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07003561 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05003562 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003563 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003564 /** Maximal number of messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003565 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003566 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05003568 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05003570 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003571 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003572 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003573 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003574 /** Number of used messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003575 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003576
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003577 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003578 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05003579
3580 /** Message queue */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003581 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003582};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003583/**
3584 * @cond INTERNAL_HIDDEN
3585 */
3586
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003587
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003588#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003589 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003590 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003591 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003592 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003594 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003595 .read_ptr = q_buffer, \
3596 .write_ptr = q_buffer, \
3597 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003598 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003599 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003600#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003601/**
3602 * INTERNAL_HIDDEN @endcond
3603 */
3604
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003605
Andrew Boie0fe789f2018-04-12 18:35:56 -07003606#define K_MSGQ_FLAG_ALLOC BIT(0)
3607
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003608/**
3609 * @brief Message Queue Attributes
3610 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303611struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05003612 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303613 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003614 /** Maximal number of messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303615 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003616 /** Used messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303617 u32_t used_msgs;
3618};
3619
Allan Stephensc98da842016-11-11 15:45:03 -05003620
3621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3625 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003626 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3627 * message is similarly aligned to this boundary, @a q_msg_size must also be
3628 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003630 * The message queue can be accessed outside the module where it is defined
3631 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003632 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003633 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * @param q_name Name of the message queue.
3636 * @param q_msg_size Message size (in bytes).
3637 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003638 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003639 *
3640 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003641 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003642#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3643 static char __noinit __aligned(q_align) \
3644 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3645 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3646 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003647 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003648
Peter Mitsisd7a37502016-10-13 11:37:40 -04003649/**
3650 * @brief Initialize a message queue.
3651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * This routine initializes a message queue object, prior to its first use.
3653 *
Allan Stephensda827222016-11-09 14:23:58 -06003654 * The message queue's ring buffer must contain space for @a max_msgs messages,
3655 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3656 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3657 * that each message is similarly aligned to this boundary, @a q_msg_size
3658 * must also be a multiple of N.
3659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * @param q Address of the message queue.
3661 * @param buffer Pointer to ring buffer that holds queued messages.
3662 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003663 * @param max_msgs Maximum number of messages that can be queued.
3664 *
3665 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003666 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003667 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003668void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3669 u32_t max_msgs);
3670
3671/**
3672 * @brief Initialize a message queue.
3673 *
3674 * This routine initializes a message queue object, prior to its first use,
3675 * allocating its internal ring buffer from the calling thread's resource
3676 * pool.
3677 *
3678 * Memory allocated for the ring buffer can be released by calling
3679 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3680 * all of its references.
3681 *
Anas Nashif4b386592019-11-25 09:30:47 -05003682 * @param msgq Address of the message queue.
Andrew Boie0fe789f2018-04-12 18:35:56 -07003683 * @param msg_size Message size (in bytes).
3684 * @param max_msgs Maximum number of messages that can be queued.
3685 *
3686 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3687 * thread's resource pool, or -EINVAL if the size parameters cause
3688 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003689 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003690 */
Anas Nashif4b386592019-11-25 09:30:47 -05003691__syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size,
Andrew Boie0fe789f2018-04-12 18:35:56 -07003692 u32_t max_msgs);
3693
Anas Nashife71293e2019-12-04 20:00:14 -05003694/**
Anas Nashif4b386592019-11-25 09:30:47 -05003695 * @brief Release allocated buffer for a queue
Anas Nashife71293e2019-12-04 20:00:14 -05003696 *
3697 * Releases memory allocated for the ring buffer.
Anas Nashif4b386592019-11-25 09:30:47 -05003698 *
3699 * @param msgq message queue to cleanup
3700 *
Anas Nashife71293e2019-12-04 20:00:14 -05003701 */
Anas Nashif4b386592019-11-25 09:30:47 -05003702void k_msgq_cleanup(struct k_msgq *msgq);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003703
3704/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003707 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003708 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003709 * @note Can be called by ISRs.
3710 *
Anas Nashif4b386592019-11-25 09:30:47 -05003711 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003712 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003713 * @param timeout Non-negative waiting period to add the message (in
3714 * milliseconds), or one of the special values K_NO_WAIT and
3715 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003716 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003717 * @retval 0 Message sent.
3718 * @retval -ENOMSG Returned without waiting or queue purged.
3719 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003720 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003721 */
Anas Nashif4b386592019-11-25 09:30:47 -05003722__syscall int k_msgq_put(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003723
3724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003725 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003727 * This routine receives a message from message queue @a q in a "first in,
3728 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003729 *
Allan Stephensc98da842016-11-11 15:45:03 -05003730 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003731 *
Anas Nashif4b386592019-11-25 09:30:47 -05003732 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003733 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003734 * @param timeout Non-negative waiting period to receive the message (in
3735 * milliseconds), or one of the special values K_NO_WAIT and
3736 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003737 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003738 * @retval 0 Message received.
3739 * @retval -ENOMSG Returned without waiting.
3740 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003741 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003742 */
Anas Nashif4b386592019-11-25 09:30:47 -05003743__syscall int k_msgq_get(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003744
3745/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003746 * @brief Peek/read a message from a message queue.
3747 *
3748 * This routine reads a message from message queue @a q in a "first in,
3749 * first out" manner and leaves the message in the queue.
3750 *
3751 * @note Can be called by ISRs.
3752 *
Anas Nashif4b386592019-11-25 09:30:47 -05003753 * @param msgq Address of the message queue.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003754 * @param data Address of area to hold the message read from the queue.
3755 *
3756 * @retval 0 Message read.
3757 * @retval -ENOMSG Returned when the queue has no message.
3758 * @req K-MSGQ-002
3759 */
Anas Nashif4b386592019-11-25 09:30:47 -05003760__syscall int k_msgq_peek(struct k_msgq *msgq, void *data);
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003761
3762/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * This routine discards all unreceived messages in a message queue's ring
3766 * buffer. Any threads that are blocked waiting to send a message to the
3767 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003768 *
Anas Nashif4b386592019-11-25 09:30:47 -05003769 * @param msgq Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003770 *
3771 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003772 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003773 */
Anas Nashif4b386592019-11-25 09:30:47 -05003774__syscall void k_msgq_purge(struct k_msgq *msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003775
Peter Mitsis67be2492016-10-07 11:44:34 -04003776/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003777 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003779 * This routine returns the number of unused entries in a message queue's
3780 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003781 *
Anas Nashif4b386592019-11-25 09:30:47 -05003782 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003783 *
3784 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003785 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003786 */
Anas Nashif4b386592019-11-25 09:30:47 -05003787__syscall u32_t k_msgq_num_free_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003788
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303789/**
3790 * @brief Get basic attributes of a message queue.
3791 *
3792 * This routine fetches basic attributes of message queue into attr argument.
3793 *
Anas Nashif4b386592019-11-25 09:30:47 -05003794 * @param msgq Address of the message queue.
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303795 * @param attrs pointer to message queue attribute structure.
3796 *
3797 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003798 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303799 */
Anas Nashif4b386592019-11-25 09:30:47 -05003800__syscall void k_msgq_get_attrs(struct k_msgq *msgq,
3801 struct k_msgq_attrs *attrs);
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303802
3803
Anas Nashif4b386592019-11-25 09:30:47 -05003804static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq)
Peter Mitsis67be2492016-10-07 11:44:34 -04003805{
Anas Nashif4b386592019-11-25 09:30:47 -05003806 return msgq->max_msgs - msgq->used_msgs;
Peter Mitsis67be2492016-10-07 11:44:34 -04003807}
3808
Peter Mitsisd7a37502016-10-13 11:37:40 -04003809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003810 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003812 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003813 *
Anas Nashif4b386592019-11-25 09:30:47 -05003814 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 *
3816 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003817 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003818 */
Anas Nashif4b386592019-11-25 09:30:47 -05003819__syscall u32_t k_msgq_num_used_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003820
Anas Nashif4b386592019-11-25 09:30:47 -05003821static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003822{
Anas Nashif4b386592019-11-25 09:30:47 -05003823 return msgq->used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824}
3825
Anas Nashif166f5192018-02-25 08:02:36 -06003826/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003827
3828/**
3829 * @defgroup mem_pool_apis Memory Pool APIs
3830 * @ingroup kernel_apis
3831 * @{
3832 */
3833
Andy Ross73cb9582017-05-09 10:42:39 -07003834/* Note on sizing: the use of a 20 bit field for block means that,
3835 * assuming a reasonable minimum block size of 16 bytes, we're limited
3836 * to 16M of memory managed by a single pool. Long term it would be
3837 * good to move to a variable bit size based on configuration.
3838 */
3839struct k_mem_block_id {
3840 u32_t pool : 8;
3841 u32_t level : 4;
3842 u32_t block : 20;
3843};
3844
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003846 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003847 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003848};
3849
Anas Nashif166f5192018-02-25 08:02:36 -06003850/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003851
3852/**
3853 * @defgroup mailbox_apis Mailbox APIs
3854 * @ingroup kernel_apis
3855 * @{
3856 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857
Anas Nashife71293e2019-12-04 20:00:14 -05003858/**
3859 * @brief Mailbox Message Structure
3860 *
3861 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003862struct k_mbox_msg {
3863 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003864 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003865 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003866 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003868 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003869 /** sender's message data buffer */
3870 void *tx_data;
3871 /** internal use only - needed for legacy API support */
3872 void *_rx_data;
3873 /** message data block descriptor */
3874 struct k_mem_block tx_block;
3875 /** source thread id */
3876 k_tid_t rx_source_thread;
3877 /** target thread id */
3878 k_tid_t tx_target_thread;
3879 /** internal use only - thread waiting on send (may be a dummy) */
3880 k_tid_t _syncing_thread;
3881#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3882 /** internal use only - semaphore used during asynchronous send */
3883 struct k_sem *_async_sem;
3884#endif
3885};
Anas Nashife71293e2019-12-04 20:00:14 -05003886/**
3887 * @brief Mailbox Structure
3888 *
3889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003890struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05003891 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003892 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05003893 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003894 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003895 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003896
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003897 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003898 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003899};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003900/**
3901 * @cond INTERNAL_HIDDEN
3902 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003903
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003904#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003905 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003906 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3907 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003908 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003909 }
3910
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003911#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003912
Peter Mitsis12092702016-10-14 12:57:23 -04003913/**
Allan Stephensc98da842016-11-11 15:45:03 -05003914 * INTERNAL_HIDDEN @endcond
3915 */
3916
3917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003918 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003920 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003921 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003922 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003925 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003926 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003927#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003928 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003929 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003930
Peter Mitsis12092702016-10-14 12:57:23 -04003931/**
3932 * @brief Initialize a mailbox.
3933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003934 * This routine initializes a mailbox object, prior to its first use.
3935 *
3936 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003937 *
3938 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003939 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003940 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003941extern void k_mbox_init(struct k_mbox *mbox);
3942
Peter Mitsis12092702016-10-14 12:57:23 -04003943/**
3944 * @brief Send a mailbox message in a synchronous manner.
3945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * This routine sends a message to @a mbox and waits for a receiver to both
3947 * receive and process it. The message data may be in a buffer, in a memory
3948 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003950 * @param mbox Address of the mailbox.
3951 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003952 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * milliseconds), or one of the special values K_NO_WAIT
3954 * and K_FOREVER. Once the message has been received,
3955 * this routine waits as long as necessary for the message
3956 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003957 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003958 * @retval 0 Message sent.
3959 * @retval -ENOMSG Returned without waiting.
3960 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003961 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003962 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003963extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003964 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003965
Peter Mitsis12092702016-10-14 12:57:23 -04003966/**
3967 * @brief Send a mailbox message in an asynchronous manner.
3968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * This routine sends a message to @a mbox without waiting for a receiver
3970 * to process it. The message data may be in a buffer, in a memory pool block,
3971 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3972 * will be given when the message has been both received and completely
3973 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003975 * @param mbox Address of the mailbox.
3976 * @param tx_msg Address of the transmit message descriptor.
3977 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003978 *
3979 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003980 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003981 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003982extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003983 struct k_sem *sem);
3984
Peter Mitsis12092702016-10-14 12:57:23 -04003985/**
3986 * @brief Receive a mailbox message.
3987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003988 * This routine receives a message from @a mbox, then optionally retrieves
3989 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003991 * @param mbox Address of the mailbox.
3992 * @param rx_msg Address of the receive message descriptor.
3993 * @param buffer Address of the buffer to receive data, or NULL to defer data
3994 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003995 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * milliseconds), or one of the special values K_NO_WAIT
3997 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003998 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003999 * @retval 0 Message received.
4000 * @retval -ENOMSG Returned without waiting.
4001 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004002 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004003 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004004extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05004005 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004006
4007/**
4008 * @brief Retrieve mailbox message data into a buffer.
4009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004010 * This routine completes the processing of a received message by retrieving
4011 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004012 *
4013 * Alternatively, this routine can be used to dispose of a received message
4014 * without retrieving its data.
4015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004016 * @param rx_msg Address of the receive message descriptor.
4017 * @param buffer Address of the buffer to receive data, or NULL to discard
4018 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04004019 *
4020 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004021 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004022 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004023extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04004024
4025/**
4026 * @brief Retrieve mailbox message data into a memory pool block.
4027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004028 * This routine completes the processing of a received message by retrieving
4029 * its data into a memory pool block, then disposing of the message.
4030 * The memory pool block that results from successful retrieval must be
4031 * returned to the pool once the data has been processed, even in cases
4032 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04004033 *
4034 * Alternatively, this routine can be used to dispose of a received message
4035 * without retrieving its data. In this case there is no need to return a
4036 * memory pool block to the pool.
4037 *
4038 * This routine allocates a new memory pool block for the data only if the
4039 * data is not already in one. If a new block cannot be allocated, the routine
4040 * returns a failure code and the received message is left unchanged. This
4041 * permits the caller to reattempt data retrieval at a later time or to dispose
4042 * of the received message without retrieving its data.
4043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004044 * @param rx_msg Address of a receive message descriptor.
4045 * @param pool Address of memory pool, or NULL to discard data.
4046 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004047 * @param timeout Non-negative waiting period to wait for a memory pool block
4048 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004049 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004050 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004051 * @retval 0 Data retrieved.
4052 * @retval -ENOMEM Returned without waiting.
4053 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004054 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004055 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004056extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04004057 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05004058 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004059
Anas Nashif166f5192018-02-25 08:02:36 -06004060/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004061
4062/**
Anas Nashifce78d162018-05-24 12:43:11 -05004063 * @defgroup pipe_apis Pipe APIs
4064 * @ingroup kernel_apis
4065 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004066 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004067
Anas Nashifce78d162018-05-24 12:43:11 -05004068/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004069struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004070 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4071 size_t size; /**< Buffer size */
4072 size_t bytes_used; /**< # bytes used in buffer */
4073 size_t read_index; /**< Where in buffer to read from */
4074 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004075 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004076
4077 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004078 _wait_q_t readers; /**< Reader wait queue */
4079 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004080 } wait_q;
4081
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004082 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004083 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05004084 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004085};
4086
Anas Nashifce78d162018-05-24 12:43:11 -05004087/**
4088 * @cond INTERNAL_HIDDEN
4089 */
4090#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4091
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004092#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
4093 { \
4094 .buffer = pipe_buffer, \
4095 .size = pipe_buffer_size, \
4096 .bytes_used = 0, \
4097 .read_index = 0, \
4098 .write_index = 0, \
4099 .lock = {}, \
4100 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004101 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4102 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004103 }, \
4104 _OBJECT_TRACING_INIT \
4105 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004106 }
4107
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004108#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004109
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004110/**
Allan Stephensc98da842016-11-11 15:45:03 -05004111 * INTERNAL_HIDDEN @endcond
4112 */
4113
4114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004115 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004117 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004118 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004119 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004121 * @param name Name of the pipe.
4122 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4123 * or zero if no ring buffer is used.
4124 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004125 *
4126 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004127 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004128#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004129 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004130 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004131 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004132 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004133
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004135 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004137 * This routine initializes a pipe object, prior to its first use.
4138 *
4139 * @param pipe Address of the pipe.
4140 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4141 * is used.
4142 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4143 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004144 *
4145 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004146 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004147 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004148void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4149
4150/**
4151 * @brief Release a pipe's allocated buffer
4152 *
4153 * If a pipe object was given a dynamically allocated buffer via
4154 * k_pipe_alloc_init(), this will free it. This function does nothing
4155 * if the buffer wasn't dynamically allocated.
4156 *
4157 * @param pipe Address of the pipe.
Anas Nashif361a84d2019-06-16 08:22:08 -04004158 * @retval 0 on success
4159 * @retval -EAGAIN nothing to cleanup
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004160 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004161 */
Anas Nashif361a84d2019-06-16 08:22:08 -04004162int k_pipe_cleanup(struct k_pipe *pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07004163
4164/**
4165 * @brief Initialize a pipe and allocate a buffer for it
4166 *
4167 * Storage for the buffer region will be allocated from the calling thread's
4168 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4169 * or userspace is enabled and the pipe object loses all references to it.
4170 *
4171 * This function should only be called on uninitialized pipe objects.
4172 *
4173 * @param pipe Address of the pipe.
4174 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4175 * buffer is used.
4176 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004177 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004178 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004179 */
4180__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004181
4182/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004183 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004185 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004187 * @param pipe Address of the pipe.
4188 * @param data Address of data to write.
4189 * @param bytes_to_write Size of data (in bytes).
4190 * @param bytes_written Address of area to hold the number of bytes written.
4191 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004192 * @param timeout Non-negative waiting period to wait for the data to be written
4193 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004194 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004195 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004196 * @retval 0 At least @a min_xfer bytes of data were written.
4197 * @retval -EIO Returned without waiting; zero data bytes were written.
4198 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004199 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004200 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004201 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004202__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4203 size_t bytes_to_write, size_t *bytes_written,
4204 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004205
4206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004207 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004209 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004211 * @param pipe Address of the pipe.
4212 * @param data Address to place the data read from pipe.
4213 * @param bytes_to_read Maximum number of data bytes to read.
4214 * @param bytes_read Address of area to hold the number of bytes read.
4215 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004216 * @param timeout Non-negative waiting period to wait for the data to be read
4217 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004218 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004219 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004220 * @retval 0 At least @a min_xfer bytes of data were read.
Anas Nashif361a84d2019-06-16 08:22:08 -04004221 * @retval -EINVAL invalid parameters supplied
Allan Stephens9ef50f42016-11-16 15:33:31 -05004222 * @retval -EIO Returned without waiting; zero data bytes were read.
4223 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004224 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004225 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004226 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004227__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4228 size_t bytes_to_read, size_t *bytes_read,
4229 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004230
4231/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004232 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004234 * This routine writes the data contained in a memory block to @a pipe.
4235 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004236 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004238 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004239 * @param block Memory block containing data to send
4240 * @param size Number of data bytes in memory block to send
4241 * @param sem Semaphore to signal upon completion (else NULL)
4242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004243 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004244 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004245 */
4246extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4247 size_t size, struct k_sem *sem);
4248
Anas Nashif166f5192018-02-25 08:02:36 -06004249/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004250
Allan Stephensc98da842016-11-11 15:45:03 -05004251/**
4252 * @cond INTERNAL_HIDDEN
4253 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004254
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004255struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004256 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004257 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004258 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004259 char *buffer;
4260 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004261 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004262
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004263 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004264 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004265};
4266
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004267#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004268 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004269 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004270 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004271 .num_blocks = slab_num_blocks, \
4272 .block_size = slab_block_size, \
4273 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004274 .free_list = NULL, \
4275 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004276 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004277 }
4278
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004279#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004280
4281
Peter Mitsis578f9112016-10-07 13:50:31 -04004282/**
Allan Stephensc98da842016-11-11 15:45:03 -05004283 * INTERNAL_HIDDEN @endcond
4284 */
4285
4286/**
4287 * @defgroup mem_slab_apis Memory Slab APIs
4288 * @ingroup kernel_apis
4289 * @{
4290 */
4291
4292/**
Allan Stephensda827222016-11-09 14:23:58 -06004293 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004294 *
Allan Stephensda827222016-11-09 14:23:58 -06004295 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004296 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004297 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4298 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004299 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004300 *
Allan Stephensda827222016-11-09 14:23:58 -06004301 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004302 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004303 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004304 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004306 * @param name Name of the memory slab.
4307 * @param slab_block_size Size of each memory block (in bytes).
4308 * @param slab_num_blocks Number memory blocks.
4309 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004310 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004311 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004312#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004313 char __noinit __aligned(WB_UP(slab_align)) \
4314 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004315 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004316 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004317 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004318
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004319/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004320 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004322 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004323 *
Allan Stephensda827222016-11-09 14:23:58 -06004324 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4325 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004326 * N-byte boundary matching a word boundary, where N is a power of 2
4327 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004328 * To ensure that each memory block is similarly aligned to this boundary,
4329 * @a slab_block_size must also be a multiple of N.
4330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004331 * @param slab Address of the memory slab.
4332 * @param buffer Pointer to buffer used for the memory blocks.
4333 * @param block_size Size of each memory block (in bytes).
4334 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004335 *
4336 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004337 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004338 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004339extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004340 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004341
4342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004343 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004345 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004347 * @param slab Address of the memory slab.
4348 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004349 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004350 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4351 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004352 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004353 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004354 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004355 * @retval -ENOMEM Returned without waiting.
4356 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004357 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004358 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004359extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004360 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004361
4362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004363 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004365 * This routine releases a previously allocated memory block back to its
4366 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004368 * @param slab Address of the memory slab.
4369 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004370 *
4371 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004372 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004373 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004374extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004375
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004377 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004379 * This routine gets the number of memory blocks that are currently
4380 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004382 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004384 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004385 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004386 */
Kumar Galacc334c72017-04-21 10:55:34 -05004387static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004388{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004389 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004390}
4391
Peter Mitsisc001aa82016-10-13 13:53:37 -04004392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004393 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004395 * This routine gets the number of memory blocks that are currently
4396 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004398 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004400 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004401 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004402 */
Kumar Galacc334c72017-04-21 10:55:34 -05004403static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004404{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004405 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004406}
4407
Anas Nashif166f5192018-02-25 08:02:36 -06004408/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004409
4410/**
4411 * @cond INTERNAL_HIDDEN
4412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004413
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004414struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004415 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004416 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004417};
4418
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004419/**
Allan Stephensc98da842016-11-11 15:45:03 -05004420 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004421 */
4422
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004423/**
Allan Stephensc98da842016-11-11 15:45:03 -05004424 * @addtogroup mem_pool_apis
4425 * @{
4426 */
4427
4428/**
4429 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004431 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4432 * long. The memory pool allows blocks to be repeatedly partitioned into
4433 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004434 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004435 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004436 * If the pool is to be accessed outside the module where it is defined, it
4437 * can be declared via
4438 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004439 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004441 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004442 * @param minsz Size of the smallest blocks in the pool (in bytes).
4443 * @param maxsz Size of the largest blocks in the pool (in bytes).
4444 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004445 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004446 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004447 */
Andy Ross73cb9582017-05-09 10:42:39 -07004448#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004449 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004450 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004451 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004452 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004453 .base = { \
4454 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004455 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004456 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004457 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004458 .levels = _mpool_lvls_##name, \
4459 .flags = SYS_MEM_POOL_KERNEL \
4460 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004461 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004462 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004463
Peter Mitsis937042c2016-10-13 13:18:26 -04004464/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004465 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004467 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004469 * @param pool Address of the memory pool.
4470 * @param block Pointer to block descriptor for the allocated memory.
4471 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004472 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004473 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4474 * or K_FOREVER to wait as long as necessary.
4475 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004476 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004477 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004478 * @retval -ENOMEM Returned without waiting.
4479 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004480 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004481 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004482extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004483 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004484
4485/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004486 * @brief Allocate memory from a memory pool with malloc() semantics
4487 *
4488 * Such memory must be released using k_free().
4489 *
4490 * @param pool Address of the memory pool.
4491 * @param size Amount of memory to allocate (in bytes).
4492 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004493 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004494 */
4495extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4496
4497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004498 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004500 * This routine releases a previously allocated memory block back to its
4501 * memory pool.
4502 *
4503 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004504 *
4505 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004506 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004507 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004508extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004509
4510/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004511 * @brief Free memory allocated from a memory pool.
4512 *
4513 * This routine releases a previously allocated memory block back to its
4514 * memory pool
4515 *
4516 * @param id Memory block identifier.
4517 *
4518 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004519 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004520 */
4521extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4522
4523/**
Anas Nashif166f5192018-02-25 08:02:36 -06004524 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004525 */
4526
4527/**
4528 * @defgroup heap_apis Heap Memory Pool APIs
4529 * @ingroup kernel_apis
4530 * @{
4531 */
4532
4533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004534 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004536 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004537 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004539 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004541 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004542 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004543 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004544extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004545
4546/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004547 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004548 *
4549 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004550 * returned must have been allocated from the heap memory pool or
4551 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004552 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004553 * If @a ptr is NULL, no operation is performed.
4554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004555 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004556 *
4557 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004558 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004559 */
4560extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004561
Allan Stephensc98da842016-11-11 15:45:03 -05004562/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004563 * @brief Allocate memory from heap, array style
4564 *
4565 * This routine provides traditional calloc() semantics. Memory is
4566 * allocated from the heap memory pool and zeroed.
4567 *
4568 * @param nmemb Number of elements in the requested array
4569 * @param size Size of each array element (in bytes).
4570 *
4571 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004572 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004573 */
4574extern void *k_calloc(size_t nmemb, size_t size);
4575
Anas Nashif166f5192018-02-25 08:02:36 -06004576/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004577
Benjamin Walshacc68c12017-01-29 18:57:45 -05004578/* polling API - PRIVATE */
4579
Benjamin Walshb0179862017-02-02 16:39:57 -05004580#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004581#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004582#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004583#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004584#endif
4585
Benjamin Walshacc68c12017-01-29 18:57:45 -05004586/* private - types bit positions */
4587enum _poll_types_bits {
4588 /* can be used to ignore an event */
4589 _POLL_TYPE_IGNORE,
4590
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004591 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004592 _POLL_TYPE_SIGNAL,
4593
4594 /* semaphore availability */
4595 _POLL_TYPE_SEM_AVAILABLE,
4596
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004597 /* queue/fifo/lifo data availability */
4598 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004599
4600 _POLL_NUM_TYPES
4601};
4602
Patrik Flykt4344e272019-03-08 14:19:05 -07004603#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004604
4605/* private - states bit positions */
4606enum _poll_states_bits {
4607 /* default state when creating event */
4608 _POLL_STATE_NOT_READY,
4609
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004610 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004611 _POLL_STATE_SIGNALED,
4612
4613 /* semaphore is available */
4614 _POLL_STATE_SEM_AVAILABLE,
4615
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004616 /* data is available to read on queue/fifo/lifo */
4617 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004618
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004619 /* queue/fifo/lifo wait was cancelled */
4620 _POLL_STATE_CANCELLED,
4621
Benjamin Walshacc68c12017-01-29 18:57:45 -05004622 _POLL_NUM_STATES
4623};
4624
Patrik Flykt4344e272019-03-08 14:19:05 -07004625#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004626
4627#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004628 (32 - (0 \
4629 + 8 /* tag */ \
4630 + _POLL_NUM_TYPES \
4631 + _POLL_NUM_STATES \
4632 + 1 /* modes */ \
4633 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004634
Benjamin Walshacc68c12017-01-29 18:57:45 -05004635/* end of polling API - PRIVATE */
4636
4637
4638/**
4639 * @defgroup poll_apis Async polling APIs
4640 * @ingroup kernel_apis
4641 * @{
4642 */
4643
4644/* Public polling API */
4645
4646/* public - values for k_poll_event.type bitfield */
4647#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004648#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4649#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4650#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004651#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004652
4653/* public - polling modes */
4654enum k_poll_modes {
4655 /* polling thread does not take ownership of objects when available */
4656 K_POLL_MODE_NOTIFY_ONLY = 0,
4657
4658 K_POLL_NUM_MODES
4659};
4660
4661/* public - values for k_poll_event.state bitfield */
4662#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004663#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4664#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4665#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004666#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004667#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004668
4669/* public - poll signal object */
4670struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05004671 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004672 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004673
Anas Nashife71293e2019-12-04 20:00:14 -05004674 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004675 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4676 * user resets it to 0.
4677 */
4678 unsigned int signaled;
4679
Anas Nashife71293e2019-12-04 20:00:14 -05004680 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004681 int result;
4682};
4683
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004684#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004685 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004686 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004687 .signaled = 0, \
4688 .result = 0, \
4689 }
Anas Nashife71293e2019-12-04 20:00:14 -05004690/**
4691 * @brief Poll Event
4692 *
4693 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004694struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05004695 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004696 sys_dnode_t _node;
4697
Anas Nashife71293e2019-12-04 20:00:14 -05004698 /** PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004699 struct _poller *poller;
4700
Anas Nashife71293e2019-12-04 20:00:14 -05004701 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004702 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004703
Anas Nashife71293e2019-12-04 20:00:14 -05004704 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004705 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004706
Anas Nashife71293e2019-12-04 20:00:14 -05004707 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004708 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004709
Anas Nashife71293e2019-12-04 20:00:14 -05004710 /** mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004711 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004712
Anas Nashife71293e2019-12-04 20:00:14 -05004713 /** unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004714 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004715
Anas Nashife71293e2019-12-04 20:00:14 -05004716 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004717 union {
4718 void *obj;
4719 struct k_poll_signal *signal;
4720 struct k_sem *sem;
4721 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004722 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004723 };
4724};
4725
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004726#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004727 { \
4728 .poller = NULL, \
4729 .type = event_type, \
4730 .state = K_POLL_STATE_NOT_READY, \
4731 .mode = event_mode, \
4732 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004733 { .obj = event_obj }, \
4734 }
4735
4736#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4737 event_tag) \
4738 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004739 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004740 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004741 .state = K_POLL_STATE_NOT_READY, \
4742 .mode = event_mode, \
4743 .unused = 0, \
4744 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004745 }
4746
4747/**
4748 * @brief Initialize one struct k_poll_event instance
4749 *
4750 * After this routine is called on a poll event, the event it ready to be
4751 * placed in an event array to be passed to k_poll().
4752 *
4753 * @param event The event to initialize.
4754 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4755 * values. Only values that apply to the same object being polled
4756 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4757 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004758 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004759 * @param obj Kernel object or poll signal.
4760 *
4761 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004762 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004763 */
4764
Kumar Galacc334c72017-04-21 10:55:34 -05004765extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004766 int mode, void *obj);
4767
4768/**
4769 * @brief Wait for one or many of multiple poll events to occur
4770 *
4771 * This routine allows a thread to wait concurrently for one or many of
4772 * multiple poll events to have occurred. Such events can be a kernel object
4773 * being available, like a semaphore, or a poll signal event.
4774 *
4775 * When an event notifies that a kernel object is available, the kernel object
4776 * is not "given" to the thread calling k_poll(): it merely signals the fact
4777 * that the object was available when the k_poll() call was in effect. Also,
4778 * all threads trying to acquire an object the regular way, i.e. by pending on
4779 * the object, have precedence over the thread polling on the object. This
4780 * means that the polling thread will never get the poll event on an object
4781 * until the object becomes available and its pend queue is empty. For this
4782 * reason, the k_poll() call is more effective when the objects being polled
4783 * only have one thread, the polling thread, trying to acquire them.
4784 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004785 * When k_poll() returns 0, the caller should loop on all the events that were
4786 * passed to k_poll() and check the state field for the values that were
4787 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004788 *
4789 * Before being reused for another call to k_poll(), the user has to reset the
4790 * state field to K_POLL_STATE_NOT_READY.
4791 *
Andrew Boie3772f772018-05-07 16:52:57 -07004792 * When called from user mode, a temporary memory allocation is required from
4793 * the caller's resource pool.
4794 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004795 * @param events An array of pointers to events to be polled for.
4796 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004797 * @param timeout Non-negative waiting period for an event to be ready (in
4798 * milliseconds), or one of the special values K_NO_WAIT and
4799 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004800 *
4801 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004802 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004803 * @retval -EINTR Polling has been interrupted, e.g. with
4804 * k_queue_cancel_wait(). All output events are still set and valid,
4805 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4806 * words, -EINTR status means that at least one of output events is
4807 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004808 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4809 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004810 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004811 */
4812
Andrew Boie3772f772018-05-07 16:52:57 -07004813__syscall int k_poll(struct k_poll_event *events, int num_events,
4814 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004815
4816/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004817 * @brief Initialize a poll signal object.
4818 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004819 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004820 *
4821 * @param signal A poll signal.
4822 *
4823 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004824 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004825 */
4826
Andrew Boie3772f772018-05-07 16:52:57 -07004827__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4828
4829/*
4830 * @brief Reset a poll signal object's state to unsignaled.
4831 *
4832 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004833 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004834 */
4835__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4836
Patrik Flykt4344e272019-03-08 14:19:05 -07004837static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004838{
Patrik Flykt24d71432019-03-26 19:57:45 -06004839 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004840}
4841
4842/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004843 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004844 *
4845 * @param signal A poll signal object
4846 * @param signaled An integer buffer which will be written nonzero if the
4847 * object was signaled
4848 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004849 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004850 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004851 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004852 */
4853__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4854 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004855
4856/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004857 * @brief Signal a poll signal object.
4858 *
4859 * This routine makes ready a poll signal, which is basically a poll event of
4860 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4861 * made ready to run. A @a result value can be specified.
4862 *
4863 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004864 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004865 * k_poll_signal_reset(). It thus has to be reset by the user before being
4866 * passed again to k_poll() or k_poll() will consider it being signaled, and
4867 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004868 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004869 * @note The result is stored and the 'signaled' field is set even if
4870 * this function returns an error indicating that an expiring poll was
4871 * not notified. The next k_poll() will detect the missed raise.
4872 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004873 * @param signal A poll signal.
4874 * @param result The value to store in the result field of the signal.
4875 *
4876 * @retval 0 The signal was delivered successfully.
4877 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004878 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004879 */
4880
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004881__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004882
Anas Nashif954d5502018-02-25 08:37:28 -06004883/**
4884 * @internal
4885 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004886extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004887
Anas Nashif166f5192018-02-25 08:02:36 -06004888/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004889
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004890/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004891 * @defgroup cpu_idle_apis CPU Idling APIs
4892 * @ingroup kernel_apis
4893 * @{
4894 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004895/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004896 * @brief Make the CPU idle.
4897 *
4898 * This function makes the CPU idle until an event wakes it up.
4899 *
4900 * In a regular system, the idle thread should be the only thread responsible
4901 * for making the CPU idle and triggering any type of power management.
4902 * However, in some more constrained systems, such as a single-threaded system,
4903 * the only thread would be responsible for this if needed.
4904 *
4905 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004906 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004907 */
Andrew Boie07525a32019-09-21 16:17:23 -07004908static inline void k_cpu_idle(void)
4909{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004910 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004911}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004912
4913/**
4914 * @brief Make the CPU idle in an atomic fashion.
4915 *
4916 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4917 * must be done atomically before making the CPU idle.
4918 *
4919 * @param key Interrupt locking key obtained from irq_lock().
4920 *
4921 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004922 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004923 */
Andrew Boie07525a32019-09-21 16:17:23 -07004924static inline void k_cpu_atomic_idle(unsigned int key)
4925{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004926 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004927}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004928
Anas Nashif30c3cff2019-01-22 08:18:13 -05004929/**
4930 * @}
4931 */
Anas Nashif954d5502018-02-25 08:37:28 -06004932
4933/**
4934 * @internal
4935 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004936extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004937
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004938#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004939/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004940#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004941#else
4942
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004943#if !defined(CONFIG_ASSERT_NO_FILE_INFO)
4944#define __EXCEPT_LOC() __ASSERT_PRINT("@ %s:%d\n", __FILE__, __LINE__)
4945#else
4946#define __EXCEPT_LOC()
4947#endif
4948
Andrew Boiecdb94d62017-04-18 15:22:05 -07004949/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004950 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004951 *
4952 * We won't have a real exception frame to determine the PC value when
4953 * the oops occurred, so print file and line number before we jump into
4954 * the fatal error handler.
4955 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004956#define z_except_reason(reason) do { \
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004957 __EXCEPT_LOC(); \
Andrew Boie56236372019-07-15 15:22:29 -07004958 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004959 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004960
4961#endif /* _ARCH__EXCEPT */
4962
4963/**
4964 * @brief Fatally terminate a thread
4965 *
4966 * This should be called when a thread has encountered an unrecoverable
4967 * runtime condition and needs to terminate. What this ultimately
4968 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004969 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004970 *
4971 * If this is called from ISR context, the default system fatal error handler
4972 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004973 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004974 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004975#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004976
4977/**
4978 * @brief Fatally terminate the system
4979 *
4980 * This should be called when the Zephyr kernel has encountered an
4981 * unrecoverable runtime condition and needs to terminate. What this ultimately
4982 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004983 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004984 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004985 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004986#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004987
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004988/*
4989 * private APIs that are utilized by one or more public APIs
4990 */
4991
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09004992/**
4993 * @internal
4994 */
4995extern void z_init_thread_base(struct _thread_base *thread_base,
4996 int priority, u32_t initial_state,
4997 unsigned int options);
4998
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004999#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06005000/**
5001 * @internal
5002 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005003extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005004#else
Anas Nashif954d5502018-02-25 08:37:28 -06005005/**
5006 * @internal
5007 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005008#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005009#endif
5010
Anas Nashif954d5502018-02-25 08:37:28 -06005011/**
5012 * @internal
5013 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005014extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06005015/**
5016 * @internal
5017 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005018extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005019
Andrew Boiedc5d9352017-06-02 12:56:47 -07005020/* arch/cpu.h may declare an architecture or platform-specific macro
5021 * for properly declaring stacks, compatible with MMU/MPU constraints if
5022 * enabled
5023 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07005024
5025/**
5026 * @brief Obtain an extern reference to a stack
5027 *
5028 * This macro properly brings the symbol of a thread stack declared
5029 * elsewhere into scope.
5030 *
5031 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005032 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07005033 */
5034#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
5035
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005036#ifdef ARCH_THREAD_STACK_DEFINE
5037#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07005038#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005039 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
5040#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
5041#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
5042#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boieb5c68102019-11-21 17:38:17 -08005043#define K_THREAD_STACK_RESERVED ((size_t)ARCH_THREAD_STACK_RESERVED)
Andrew Boie4e5c0932019-04-04 12:05:28 -07005044static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005045{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005046 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07005047}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005048#else
5049/**
5050 * @brief Declare a toplevel thread stack memory region
5051 *
5052 * This declares a region of memory suitable for use as a thread's stack.
5053 *
5054 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5055 * 'noinit' section so that it isn't zeroed at boot
5056 *
Andrew Boie507852a2017-07-25 18:47:07 -07005057 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04005058 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07005059 * inside needs to be examined, examine thread->stack_info for the associated
5060 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005061 *
5062 * It is legal to precede this definition with the 'static' keyword.
5063 *
5064 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
5065 * parameter of k_thread_create(), it may not be the same as the
5066 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
5067 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005068 * Some arches may round the size of the usable stack region up to satisfy
5069 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
5070 * size.
5071 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07005072 * @param sym Thread stack symbol name
5073 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005074 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005075 */
5076#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005077 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005078
5079/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305080 * @brief Calculate size of stacks to be allocated in a stack array
5081 *
5082 * This macro calculates the size to be allocated for the stacks
5083 * inside a stack array. It accepts the indicated "size" as a parameter
5084 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
5085 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
5086 *
5087 * @param size Size of the stack memory region
5088 * @req K-TSTACK-001
5089 */
5090#define K_THREAD_STACK_LEN(size) (size)
5091
5092/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07005093 * @brief Declare a toplevel array of thread stack memory regions
5094 *
5095 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
5096 * definition for additional details and constraints.
5097 *
5098 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5099 * 'noinit' section so that it isn't zeroed at boot
5100 *
5101 * @param sym Thread stack symbol name
5102 * @param nmemb Number of stacks to declare
5103 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005104 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005105 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07005106#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005107 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305108 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005109
5110/**
5111 * @brief Declare an embedded stack memory region
5112 *
5113 * Used for stacks embedded within other data structures. Use is highly
5114 * discouraged but in some cases necessary. For memory protection scenarios,
5115 * it is very important that any RAM preceding this member not be writable
5116 * by threads else a stack overflow will lead to silent corruption. In other
5117 * words, the containing data structure should live in RAM owned by the kernel.
5118 *
5119 * @param sym Thread stack symbol name
5120 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005121 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005122 */
5123#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005124 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005125
5126/**
5127 * @brief Return the size in bytes of a stack memory region
5128 *
5129 * Convenience macro for passing the desired stack size to k_thread_create()
5130 * since the underlying implementation may actually create something larger
5131 * (for instance a guard area).
5132 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005133 * The value returned here is not guaranteed to match the 'size' parameter
5134 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005135 *
5136 * @param sym Stack memory symbol
5137 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005138 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005139 */
5140#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5141
Andrew Boie575abc02019-03-19 10:42:24 -07005142
5143/**
5144 * @brief Indicate how much additional memory is reserved for stack objects
5145 *
5146 * Any given stack declaration may have additional memory in it for guard
5147 * areas or supervisor mode stacks. This macro indicates how much space
5148 * is reserved for this. The memory reserved may not be contiguous within
5149 * the stack object, and does not account for additional space used due to
5150 * enforce alignment.
5151 */
Andrew Boieb5c68102019-11-21 17:38:17 -08005152#define K_THREAD_STACK_RESERVED ((size_t)0U)
Andrew Boie575abc02019-03-19 10:42:24 -07005153
Andrew Boiedc5d9352017-06-02 12:56:47 -07005154/**
5155 * @brief Get a pointer to the physical stack buffer
5156 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005157 * This macro is deprecated. If a stack buffer needs to be examined, the
5158 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005159 *
5160 * @param sym Declared stack symbol name
5161 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005162 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005163 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005164static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005165{
5166 return (char *)sym;
5167}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005168
5169#endif /* _ARCH_DECLARE_STACK */
5170
Chunlin Hane9c97022017-07-07 20:29:30 +08005171/**
5172 * @defgroup mem_domain_apis Memory domain APIs
5173 * @ingroup kernel_apis
5174 * @{
5175 */
5176
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005177/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005178 * @def K_MEM_PARTITION_DEFINE
5179 * @brief Used to declare a memory partition
5180 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005181 */
5182#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5183#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5184 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005185 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005186 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005187#else
5188#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005189 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005190 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005191#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5192
Chunlin Hane9c97022017-07-07 20:29:30 +08005193/* memory partition */
5194struct k_mem_partition {
Anas Nashife71293e2019-12-04 20:00:14 -05005195 /** start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005196 uintptr_t start;
Anas Nashife71293e2019-12-04 20:00:14 -05005197 /** size of memory partition */
Andrew Boiea8248212019-11-13 12:10:56 -08005198 size_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005199#if defined(CONFIG_MEMORY_PROTECTION)
Anas Nashife71293e2019-12-04 20:00:14 -05005200 /** attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305201 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005202#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005203};
5204
Anas Nashife71293e2019-12-04 20:00:14 -05005205/**
5206 * @brief Memory Domain
5207 *
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305208 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005209struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305210#ifdef CONFIG_USERSPACE
Anas Nashife71293e2019-12-04 20:00:14 -05005211 /** partitions in the domain */
Chunlin Hane9c97022017-07-07 20:29:30 +08005212 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305213#endif /* CONFIG_USERSPACE */
Anas Nashife71293e2019-12-04 20:00:14 -05005214 /** domain q */
Chunlin Hane9c97022017-07-07 20:29:30 +08005215 sys_dlist_t mem_domain_q;
Anas Nashife71293e2019-12-04 20:00:14 -05005216 /** number of partitions in the domain */
Leandro Pereira08de6582018-02-28 14:22:57 -08005217 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005218};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305219
Chunlin Hane9c97022017-07-07 20:29:30 +08005220
5221/**
5222 * @brief Initialize a memory domain.
5223 *
5224 * Initialize a memory domain with given name and memory partitions.
5225 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005226 * See documentation for k_mem_domain_add_partition() for details about
5227 * partition constraints.
5228 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005229 * @param domain The memory domain to be initialized.
5230 * @param num_parts The number of array items of "parts" parameter.
5231 * @param parts An array of pointers to the memory partitions. Can be NULL
5232 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005233 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005234 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005235extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005236 struct k_mem_partition *parts[]);
5237/**
5238 * @brief Destroy a memory domain.
5239 *
5240 * Destroy a memory domain.
5241 *
5242 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005243 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005244 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005245extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5246
5247/**
5248 * @brief Add a memory partition into a memory domain.
5249 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005250 * Add a memory partition into a memory domain. Partitions must conform to
5251 * the following constraints:
5252 *
5253 * - Partition bounds must be within system RAM boundaries on MMU-based
5254 * systems.
5255 * - Partitions in the same memory domain may not overlap each other.
5256 * - Partitions must not be defined which expose private kernel
5257 * data structures or kernel objects.
5258 * - The starting address alignment, and the partition size must conform to
5259 * the constraints of the underlying memory management hardware, which
5260 * varies per architecture.
5261 * - Memory domain partitions are only intended to control access to memory
5262 * from user mode threads.
5263 *
5264 * Violating these constraints may lead to CPU exceptions or undefined
5265 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005266 *
5267 * @param domain The memory domain to be added a memory partition.
5268 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005269 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005270 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005271extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5272 struct k_mem_partition *part);
5273
5274/**
5275 * @brief Remove a memory partition from a memory domain.
5276 *
5277 * Remove a memory partition from a memory domain.
5278 *
5279 * @param domain The memory domain to be removed a memory partition.
5280 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005281 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005282 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005283extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5284 struct k_mem_partition *part);
5285
5286/**
5287 * @brief Add a thread into a memory domain.
5288 *
5289 * Add a thread into a memory domain.
5290 *
5291 * @param domain The memory domain that the thread is going to be added into.
5292 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005293 *
5294 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005295 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005296extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5297 k_tid_t thread);
5298
5299/**
5300 * @brief Remove a thread from its memory domain.
5301 *
5302 * Remove a thread from its memory domain.
5303 *
5304 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005305 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005306 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005307extern void k_mem_domain_remove_thread(k_tid_t thread);
5308
Anas Nashif166f5192018-02-25 08:02:36 -06005309/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005310
Andrew Boied76ae462020-01-02 11:57:43 -08005311#ifdef CONFIG_PRINTK
Andrew Boie756f9072017-10-10 16:01:49 -07005312/**
5313 * @brief Emit a character buffer to the console device
5314 *
5315 * @param c String of characters to print
5316 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005317 *
5318 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005319 */
5320__syscall void k_str_out(char *c, size_t n);
Andrew Boied76ae462020-01-02 11:57:43 -08005321#endif
Andrew Boie756f9072017-10-10 16:01:49 -07005322
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005323/**
5324 * @brief Disable preservation of floating point context information.
5325 *
5326 * This routine informs the kernel that the specified thread
5327 * will no longer be using the floating point registers.
5328 *
5329 * @warning
5330 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005331 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005332 *
5333 * @warning
5334 * This routine should only be used to disable floating point support for
5335 * a thread that currently has such support enabled.
5336 *
5337 * @param thread ID of thread.
5338 *
5339 * @retval 0 On success.
5340 * @retval -ENOSYS If the floating point disabling is not implemented.
5341 * -EINVAL If the floating point disabling could not be performed.
5342 */
5343__syscall int k_float_disable(struct k_thread *thread);
5344
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005345#ifdef __cplusplus
5346}
5347#endif
5348
Anas Nashif10291a02019-06-25 12:25:12 -04005349#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005350#include <syscalls/kernel.h>
5351
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005352#endif /* !_ASMLANGUAGE */
5353
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005354#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */