blob: 8faf04c5a1ceba0c6a943ea067e5c1ce25fb6185 [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
Andrew Boieefc5fe02020-02-05 10:41:58 -0800837#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
838/**
839 * @brief Obtain stack usage information for the specified thread
840 *
841 * User threads will need to have permission on the target thread object.
842 *
843 * Some hardware may prevent inspection of a stack buffer currently in use.
844 * If this API is called from supervisor mode, on the currently running thread,
845 * on a platform which selects CONFIG_NO_UNUSED_STACK_INSPECTION, an error
846 * will be generated.
847 *
848 * @param thread Thread to inspect stack information
849 * @param unused_ptr Output parameter, filled in with the unused stack space
850 * of the target thread in bytes.
851 * @return 0 on success
852 * @return -EBADF Bad thread object (user mode only)
853 * @return -EPERM No permissions on thread object (user mode only)
854 * #return -ENOTSUP Forbidden by hardware policy
855 * @return -EINVAL Thread is uninitialized or exited (user mode only)
856 * @return -EFAULT Bad memory address for unused_ptr (user mode only)
857 */
858__syscall int k_thread_stack_space_get(const struct k_thread *thread,
859 size_t *unused_ptr);
860#endif
861
Andrew Boie92e5bd72018-04-12 17:12:15 -0700862#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
863/**
864 * @brief Assign the system heap as a thread's resource pool
865 *
866 * Similar to k_thread_resource_pool_assign(), but the thread will use
867 * the kernel heap to draw memory.
868 *
869 * Use with caution, as a malicious thread could perform DoS attacks on the
870 * kernel heap.
871 *
872 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400873 *
874 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700875 */
876void k_thread_system_pool_assign(struct k_thread *thread);
877#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
878
879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700882 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700884 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200886 * @return Zero if the requested time has elapsed or the number of milliseconds
887 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700889__syscall s32_t k_sleep(s32_t ms);
890
891/**
892 * @brief Put the current thread to sleep with microsecond resolution.
893 *
894 * This function is unlikely to work as expected without kernel tuning.
895 * In particular, because the lower bound on the duration of a sleep is
896 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
897 * to achieve the resolution desired. The implications of doing this must
898 * be understood before attempting to use k_usleep(). Use with caution.
899 *
900 * @param us Number of microseconds to sleep.
901 *
902 * @return Zero if the requested time has elapsed or the number of microseconds
903 * left to sleep, if thread was woken up by \ref k_wakeup call.
904 */
905__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906
907/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500908 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400909 *
910 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500911 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400912 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400913 * @return N/A
914 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800915__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400916
917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500918 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400921 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500922 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400923 *
924 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400925 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 */
Andrew Boie468190a2017-09-29 14:00:48 -0700927__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400928
929/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500932 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * If @a thread is not currently sleeping, the routine has no effect.
935 *
936 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400937 *
938 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400939 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 */
Andrew Boie468190a2017-09-29 14:00:48 -0700941__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942
943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500944 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500946 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400947 *
948 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400949 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700950__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400951
952/**
Allan Stephensc98da842016-11-11 15:45:03 -0500953 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500955 * This routine permanently stops execution of @a thread. The thread is taken
956 * off all kernel queues it is part of (i.e. the ready queue, the timeout
957 * queue, or a kernel object wait queue). However, any kernel resources the
958 * thread might currently own (such as mutexes or memory blocks) are not
959 * released. It is the responsibility of the caller of this routine to ensure
960 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963 *
964 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400965 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400966 */
Andrew Boie468190a2017-09-29 14:00:48 -0700967__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400968
Andrew Boie7d627c52017-08-30 11:01:56 -0700969
970/**
971 * @brief Start an inactive thread
972 *
973 * If a thread was created with K_FOREVER in the delay parameter, it will
974 * not be added to the scheduling queue until this function is called
975 * on it.
976 *
977 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400978 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700979 */
Andrew Boie468190a2017-09-29 14:00:48 -0700980__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700981
Allan Stephensc98da842016-11-11 15:45:03 -0500982/**
983 * @cond INTERNAL_HIDDEN
984 */
985
Benjamin Walshd211a522016-12-06 11:44:01 -0500986/* timeout has timed out and is not on _timeout_q anymore */
987#define _EXPIRED (-2)
988
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400989struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700990 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700991 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400992 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700993 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500994 void *init_p1;
995 void *init_p2;
996 void *init_p3;
997 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500998 u32_t init_options;
999 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -05001000 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -06001001 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001002};
1003
Andrew Boied26cf2d2017-03-30 13:07:02 -07001004#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001005 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -06001006 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001007 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001008 .init_thread = (thread), \
1009 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001010 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -07001011 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001012 .init_p1 = (void *)p1, \
1013 .init_p2 = (void *)p2, \
1014 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001015 .init_prio = (prio), \
1016 .init_options = (options), \
1017 .init_delay = (delay), \
1018 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -06001019 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001020 }
1021
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001022/**
Allan Stephensc98da842016-11-11 15:45:03 -05001023 * INTERNAL_HIDDEN @endcond
1024 */
1025
1026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001027 * @brief Statically define and initialize a thread.
1028 *
1029 * The thread may be scheduled for immediate execution or a delayed start.
1030 *
1031 * Thread options are architecture-specific, and can include K_ESSENTIAL,
1032 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
1033 * them using "|" (the logical OR operator).
1034 *
1035 * The ID of the thread can be accessed using:
1036 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001037 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 *
1039 * @param name Name of the thread.
1040 * @param stack_size Stack size in bytes.
1041 * @param entry Thread entry function.
1042 * @param p1 1st entry point parameter.
1043 * @param p2 2nd entry point parameter.
1044 * @param p3 3rd entry point parameter.
1045 * @param prio Thread priority.
1046 * @param options Thread options.
1047 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001048 *
Anas Nashif47420d02018-05-24 14:20:56 -04001049 * @req K-THREAD-010
1050 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001051 * @internal It has been observed that the x86 compiler by default aligns
1052 * these _static_thread_data structures to 32-byte boundaries, thereby
1053 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -04001054 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001055 */
Allan Stephens6cfe1322016-10-26 10:16:51 -05001056#define K_THREAD_DEFINE(name, stack_size, \
1057 entry, p1, p2, p3, \
1058 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -07001059 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001060 struct k_thread _k_thread_obj_##name; \
1061 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -07001062 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
1063 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001064 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -06001065 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001066 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001067
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001071 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * @param thread ID of thread whose priority is needed.
1074 *
1075 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001076 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001078__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001079
1080/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001084 *
1085 * Rescheduling can occur immediately depending on the priority @a thread is
1086 * set to:
1087 *
1088 * - If its priority is raised above the priority of the caller of this
1089 * function, and the caller is preemptible, @a thread will be scheduled in.
1090 *
1091 * - If the caller operates on itself, it lowers its priority below that of
1092 * other threads in the system, and the caller is preemptible, the thread of
1093 * highest priority will be scheduled in.
1094 *
1095 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1096 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1097 * highest priority.
1098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 * @param prio New priority.
1101 *
1102 * @warning Changing the priority of a thread currently involved in mutex
1103 * priority inheritance may result in undefined behavior.
1104 *
1105 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001106 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107 */
Andrew Boie468190a2017-09-29 14:00:48 -07001108__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001109
Andy Ross4a2e50f2018-05-15 11:06:25 -07001110
1111#ifdef CONFIG_SCHED_DEADLINE
1112/**
1113 * @brief Set deadline expiration time for scheduler
1114 *
1115 * This sets the "deadline" expiration as a time delta from the
1116 * current time, in the same units used by k_cycle_get_32(). The
1117 * scheduler (when deadline scheduling is enabled) will choose the
1118 * next expiring thread when selecting between threads at the same
1119 * static priority. Threads at different priorities will be scheduled
1120 * according to their static priority.
1121 *
1122 * @note Deadlines that are negative (i.e. in the past) are still seen
1123 * as higher priority than others, even if the thread has "finished"
1124 * its work. If you don't want it scheduled anymore, you have to
1125 * reset the deadline into the future, block/pend the thread, or
1126 * modify its priority with k_thread_priority_set().
1127 *
1128 * @note Despite the API naming, the scheduler makes no guarantees the
1129 * the thread WILL be scheduled within that deadline, nor does it take
1130 * extra metadata (like e.g. the "runtime" and "period" parameters in
1131 * Linux sched_setattr()) that allows the kernel to validate the
1132 * scheduling for achievability. Such features could be implemented
1133 * above this call, which is simply input to the priority selection
1134 * logic.
1135 *
Anas Nashif240c5162019-06-10 12:25:50 -04001136 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001137 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001138 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1139 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001140 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001141 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001142 * @param thread A thread on which to set the deadline
1143 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001144 *
1145 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001146 */
1147__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1148#endif
1149
Andy Rossab46b1b2019-01-30 15:00:42 -08001150#ifdef CONFIG_SCHED_CPU_MASK
1151/**
1152 * @brief Sets all CPU enable masks to zero
1153 *
1154 * After this returns, the thread will no longer be schedulable on any
1155 * CPUs. The thread must not be currently runnable.
1156 *
Anas Nashif240c5162019-06-10 12:25:50 -04001157 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001158 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001159 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1160 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001161 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001162 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001163 * @param thread Thread to operate upon
1164 * @return Zero on success, otherwise error code
1165 */
1166int k_thread_cpu_mask_clear(k_tid_t thread);
1167
1168/**
1169 * @brief Sets all CPU enable masks to one
1170 *
1171 * After this returns, the thread will be schedulable on any CPU. The
1172 * thread must not be currently runnable.
1173 *
Anas Nashif240c5162019-06-10 12:25:50 -04001174 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001175 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001176 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1177 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001178 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001179 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001180 * @param thread Thread to operate upon
1181 * @return Zero on success, otherwise error code
1182 */
1183int k_thread_cpu_mask_enable_all(k_tid_t thread);
1184
1185/**
1186 * @brief Enable thread to run on specified CPU
1187 *
1188 * The thread must not be currently runnable.
1189 *
Anas Nashif240c5162019-06-10 12:25:50 -04001190 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001191 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001192 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1193 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001194 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001195 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001196 * @param thread Thread to operate upon
1197 * @param cpu CPU index
1198 * @return Zero on success, otherwise error code
1199 */
1200int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1201
1202/**
1203 * @brief Prevent thread to run on specified CPU
1204 *
1205 * The thread must not be currently runnable.
1206 *
Anas Nashif240c5162019-06-10 12:25:50 -04001207 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001208 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001209 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1210 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001211 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001212 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001213 * @param thread Thread to operate upon
1214 * @param cpu CPU index
1215 * @return Zero on success, otherwise error code
1216 */
1217int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1218#endif
1219
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 *
Andy Ross50d09422019-11-19 11:20:07 -08001223 * This routine prevents the kernel scheduler from making @a thread
1224 * the current thread. All other internal operations on @a thread are
1225 * still performed; for example, kernel objects it is waiting on are
1226 * still handed to it. Note that any existing timeouts
1227 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1228 * will be canceled. On resume, the thread will begin running
1229 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * If @a thread is already suspended, the routine has no effect.
1232 *
1233 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
1235 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001236 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001237 */
Andrew Boie468190a2017-09-29 14:00:48 -07001238__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239
1240/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001243 * This routine allows the kernel scheduler to make @a thread the current
1244 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001246 * If @a thread is not currently suspended, the routine has no effect.
1247 *
1248 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001249 *
1250 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001251 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001252 */
Andrew Boie468190a2017-09-29 14:00:48 -07001253__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001254
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001255/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001256 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001258 * This routine specifies how the scheduler will perform time slicing of
1259 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001261 * To enable time slicing, @a slice must be non-zero. The scheduler
1262 * ensures that no thread runs for more than the specified time limit
1263 * before other threads of that priority are given a chance to execute.
1264 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001265 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001267 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001268 * execute. Once the scheduler selects a thread for execution, there is no
1269 * minimum guaranteed time the thread will execute before threads of greater or
1270 * equal priority are scheduled.
1271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001272 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001273 * for execution, this routine has no effect; the thread is immediately
1274 * rescheduled after the slice period expires.
1275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001276 * To disable timeslicing, set both @a slice and @a prio to zero.
1277 *
1278 * @param slice Maximum time slice length (in milliseconds).
1279 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
1281 * @return N/A
1282 */
Kumar Galacc334c72017-04-21 10:55:34 -05001283extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001284
Anas Nashif166f5192018-02-25 08:02:36 -06001285/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001286
1287/**
1288 * @addtogroup isr_apis
1289 * @{
1290 */
1291
1292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001294 *
Allan Stephensc98da842016-11-11 15:45:03 -05001295 * This routine allows the caller to customize its actions, depending on
1296 * whether it is a thread or an ISR.
1297 *
1298 * @note Can be called by ISRs.
1299 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001300 * @return false if invoked by a thread.
1301 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001303extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001304
Benjamin Walsh445830d2016-11-10 15:54:27 -05001305/**
1306 * @brief Determine if code is running in a preemptible thread.
1307 *
Allan Stephensc98da842016-11-11 15:45:03 -05001308 * This routine allows the caller to customize its actions, depending on
1309 * whether it can be preempted by another thread. The routine returns a 'true'
1310 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001311 *
Allan Stephensc98da842016-11-11 15:45:03 -05001312 * - The code is running in a thread, not at ISR.
1313 * - The thread's priority is in the preemptible range.
1314 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001315 *
Allan Stephensc98da842016-11-11 15:45:03 -05001316 * @note Can be called by ISRs.
1317 *
1318 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001319 * @return Non-zero if invoked by a preemptible thread.
1320 */
Andrew Boie468190a2017-09-29 14:00:48 -07001321__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001322
Allan Stephensc98da842016-11-11 15:45:03 -05001323/**
Peter Bigot74ef3952019-12-23 11:48:43 -06001324 * @brief Test whether startup is in the before-main-task phase.
1325 *
1326 * This routine allows the caller to customize its actions, depending on
1327 * whether it being invoked before the kernel is fully active.
1328 *
1329 * @note Can be called by ISRs.
1330 *
1331 * @return true if invoked before post-kernel initialization
1332 * @return false if invoked during/after post-kernel initialization
1333 */
1334static inline bool k_is_pre_kernel(void)
1335{
1336 extern bool z_sys_post_kernel; /* in init.c */
1337
1338 return !z_sys_post_kernel;
1339}
1340
1341/**
Anas Nashif166f5192018-02-25 08:02:36 -06001342 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001343 */
1344
1345/**
1346 * @addtogroup thread_apis
1347 * @{
1348 */
1349
1350/**
1351 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001352 *
Allan Stephensc98da842016-11-11 15:45:03 -05001353 * This routine prevents the current thread from being preempted by another
1354 * thread by instructing the scheduler to treat it as a cooperative thread.
1355 * If the thread subsequently performs an operation that makes it unready,
1356 * it will be context switched out in the normal manner. When the thread
1357 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001358 *
Allan Stephensc98da842016-11-11 15:45:03 -05001359 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001360 *
Allan Stephensc98da842016-11-11 15:45:03 -05001361 * @note k_sched_lock() and k_sched_unlock() should normally be used
1362 * when the operation being performed can be safely interrupted by ISRs.
1363 * However, if the amount of processing involved is very small, better
1364 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001365 *
1366 * @return N/A
1367 */
1368extern void k_sched_lock(void);
1369
Allan Stephensc98da842016-11-11 15:45:03 -05001370/**
1371 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001372 *
Allan Stephensc98da842016-11-11 15:45:03 -05001373 * This routine reverses the effect of a previous call to k_sched_lock().
1374 * A thread must call the routine once for each time it called k_sched_lock()
1375 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001376 *
1377 * @return N/A
1378 */
1379extern void k_sched_unlock(void);
1380
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001382 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001384 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * Custom data is not used by the kernel itself, and is freely available
1387 * for a thread to use as it sees fit. It can be used as a framework
1388 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001390 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001391 *
1392 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001393 *
1394 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 */
Andrew Boie468190a2017-09-29 14:00:48 -07001396__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001397
1398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001401 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001403 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001404 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001405 */
Andrew Boie468190a2017-09-29 14:00:48 -07001406__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407
1408/**
Anas Nashif57554052018-03-03 02:31:05 -06001409 * @brief Set current thread name
1410 *
1411 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1412 * tracing and debugging.
1413 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001414 * @param thread_id Thread to set name, or NULL to set the current thread
1415 * @param value Name string
1416 * @retval 0 on success
1417 * @retval -EFAULT Memory access error with supplied string
1418 * @retval -ENOSYS Thread name configuration option not enabled
1419 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001420 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001421__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001422
1423/**
1424 * @brief Get thread name
1425 *
1426 * Get the name of a thread
1427 *
1428 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001429 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001430 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001431const char *k_thread_name_get(k_tid_t thread_id);
1432
1433/**
1434 * @brief Copy the thread name into a supplied buffer
1435 *
1436 * @param thread_id Thread to obtain name information
1437 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001438 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001439 * @retval -ENOSPC Destination buffer too small
1440 * @retval -EFAULT Memory access error
1441 * @retval -ENOSYS Thread name feature not enabled
1442 * @retval 0 Success
1443 */
1444__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1445 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001446
1447/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001448 * @brief Get thread state string
1449 *
1450 * Get the human friendly thread state string
1451 *
1452 * @param thread_id Thread ID
1453 * @retval Thread state string, empty if no state flag is set
1454 */
1455const char *k_thread_state_str(k_tid_t thread_id);
1456
1457/**
Andy Rosscfe62032018-09-29 07:34:55 -07001458 * @}
1459 */
1460
1461/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001462 * @addtogroup clock_apis
1463 * @{
1464 */
1465
1466/**
1467 * @brief Generate null timeout delay.
1468 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001469 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001470 * not to wait if the requested operation cannot be performed immediately.
1471 *
1472 * @return Timeout delay value.
1473 */
1474#define K_NO_WAIT 0
1475
1476/**
1477 * @brief Generate timeout delay from milliseconds.
1478 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001479 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001480 * to wait up to @a ms milliseconds to perform the requested operation.
1481 *
1482 * @param ms Duration in milliseconds.
1483 *
1484 * @return Timeout delay value.
1485 */
Johan Hedberg14471692016-11-13 10:52:15 +02001486#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001487
1488/**
1489 * @brief Generate timeout delay from seconds.
1490 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001491 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001492 * to wait up to @a s seconds to perform the requested operation.
1493 *
1494 * @param s Duration in seconds.
1495 *
1496 * @return Timeout delay value.
1497 */
Johan Hedberg14471692016-11-13 10:52:15 +02001498#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001499
1500/**
1501 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001502
1503 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001504 * to wait up to @a m minutes to perform the requested operation.
1505 *
1506 * @param m Duration in minutes.
1507 *
1508 * @return Timeout delay value.
1509 */
Johan Hedberg14471692016-11-13 10:52:15 +02001510#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001511
1512/**
1513 * @brief Generate timeout delay from hours.
1514 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001515 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001516 * to wait up to @a h hours to perform the requested operation.
1517 *
1518 * @param h Duration in hours.
1519 *
1520 * @return Timeout delay value.
1521 */
Johan Hedberg14471692016-11-13 10:52:15 +02001522#define K_HOURS(h) K_MINUTES((h) * 60)
1523
Allan Stephensc98da842016-11-11 15:45:03 -05001524/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001525 * @brief Generate infinite timeout delay.
1526 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001527 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001528 * to wait as long as necessary to perform the requested operation.
1529 *
1530 * @return Timeout delay value.
1531 */
1532#define K_FOREVER (-1)
1533
1534/**
Anas Nashif166f5192018-02-25 08:02:36 -06001535 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001536 */
1537
1538/**
Allan Stephensc98da842016-11-11 15:45:03 -05001539 * @cond INTERNAL_HIDDEN
1540 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001541
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542struct k_timer {
1543 /*
1544 * _timeout structure must be first here if we want to use
1545 * dynamic timer allocation. timeout.node is used in the double-linked
1546 * list of free timers
1547 */
1548 struct _timeout timeout;
1549
Allan Stephens45bfa372016-10-12 12:39:42 -05001550 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001551 _wait_q_t wait_q;
1552
1553 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001554 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001555
1556 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001557 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001558
1559 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001560 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561
Allan Stephens45bfa372016-10-12 12:39:42 -05001562 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001563 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001564
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001565 /* user-specific data, also used to support legacy features */
1566 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001567
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001568 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001569 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570};
1571
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001572#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001574 .timeout = { \
1575 .node = {},\
1576 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001577 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001578 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001579 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001580 .expiry_fn = expiry, \
1581 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001582 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001583 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001584 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001585 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001586 }
1587
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001588#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001589
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001590/**
Allan Stephensc98da842016-11-11 15:45:03 -05001591 * INTERNAL_HIDDEN @endcond
1592 */
1593
1594/**
1595 * @defgroup timer_apis Timer APIs
1596 * @ingroup kernel_apis
1597 * @{
1598 */
1599
1600/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001601 * @typedef k_timer_expiry_t
1602 * @brief Timer expiry function type.
1603 *
1604 * A timer's expiry function is executed by the system clock interrupt handler
1605 * each time the timer expires. The expiry function is optional, and is only
1606 * invoked if the timer has been initialized with one.
1607 *
1608 * @param timer Address of timer.
1609 *
1610 * @return N/A
1611 */
1612typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1613
1614/**
1615 * @typedef k_timer_stop_t
1616 * @brief Timer stop function type.
1617 *
1618 * A timer's stop function is executed if the timer is stopped prematurely.
1619 * The function runs in the context of the thread that stops the timer.
1620 * The stop function is optional, and is only invoked if the timer has been
1621 * initialized with one.
1622 *
1623 * @param timer Address of timer.
1624 *
1625 * @return N/A
1626 */
1627typedef void (*k_timer_stop_t)(struct k_timer *timer);
1628
1629/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001630 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001632 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001633 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001634 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001635 *
1636 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001637 * @param expiry_fn Function to invoke each time the timer expires.
1638 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001639 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001640#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001641 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001642 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001643
Allan Stephens45bfa372016-10-12 12:39:42 -05001644/**
1645 * @brief Initialize a timer.
1646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001647 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001648 *
1649 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * @param expiry_fn Function to invoke each time the timer expires.
1651 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001652 *
1653 * @return N/A
1654 */
1655extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001656 k_timer_expiry_t expiry_fn,
1657 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001658
Allan Stephens45bfa372016-10-12 12:39:42 -05001659/**
1660 * @brief Start a timer.
1661 *
1662 * This routine starts a timer, and resets its status to zero. The timer
1663 * begins counting down using the specified duration and period values.
1664 *
1665 * Attempting to start a timer that is already running is permitted.
1666 * The timer's status is reset to zero and the timer begins counting down
1667 * using the new duration and period values.
1668 *
1669 * @param timer Address of timer.
1670 * @param duration Initial timer duration (in milliseconds).
1671 * @param period Timer period (in milliseconds).
1672 *
1673 * @return N/A
1674 */
Andrew Boiea354d492017-09-29 16:22:28 -07001675__syscall void k_timer_start(struct k_timer *timer,
1676 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001677
1678/**
1679 * @brief Stop a timer.
1680 *
1681 * This routine stops a running timer prematurely. The timer's stop function,
1682 * if one exists, is invoked by the caller.
1683 *
1684 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001686 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001687 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1688 * if @a k_timer_stop is to be called from ISRs.
1689 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001690 * @param timer Address of timer.
1691 *
1692 * @return N/A
1693 */
Andrew Boiea354d492017-09-29 16:22:28 -07001694__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001695
1696/**
1697 * @brief Read timer status.
1698 *
1699 * This routine reads the timer's status, which indicates the number of times
1700 * it has expired since its status was last read.
1701 *
1702 * Calling this routine resets the timer's status to zero.
1703 *
1704 * @param timer Address of timer.
1705 *
1706 * @return Timer status.
1707 */
Andrew Boiea354d492017-09-29 16:22:28 -07001708__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001709
1710/**
1711 * @brief Synchronize thread to timer expiration.
1712 *
1713 * This routine blocks the calling thread until the timer's status is non-zero
1714 * (indicating that it has expired at least once since it was last examined)
1715 * or the timer is stopped. If the timer status is already non-zero,
1716 * or the timer is already stopped, the caller continues without waiting.
1717 *
1718 * Calling this routine resets the timer's status to zero.
1719 *
1720 * This routine must not be used by interrupt handlers, since they are not
1721 * allowed to block.
1722 *
1723 * @param timer Address of timer.
1724 *
1725 * @return Timer status.
1726 */
Andrew Boiea354d492017-09-29 16:22:28 -07001727__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001728
Andy Ross52e444b2018-09-28 09:06:37 -07001729extern s32_t z_timeout_remaining(struct _timeout *timeout);
1730
Allan Stephens45bfa372016-10-12 12:39:42 -05001731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001733 *
1734 * This routine computes the (approximate) time remaining before a running
1735 * timer next expires. If the timer is not running, it returns zero.
1736 *
1737 * @param timer Address of timer.
1738 *
1739 * @return Remaining time (in milliseconds).
1740 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001741__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001742
Patrik Flykt4344e272019-03-08 14:19:05 -07001743static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001744{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001745 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001746 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001747}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001748
Allan Stephensc98da842016-11-11 15:45:03 -05001749/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001750 * @brief Associate user-specific data with a timer.
1751 *
1752 * This routine records the @a user_data with the @a timer, to be retrieved
1753 * later.
1754 *
1755 * It can be used e.g. in a timer handler shared across multiple subsystems to
1756 * retrieve data specific to the subsystem this timer is associated with.
1757 *
1758 * @param timer Address of timer.
1759 * @param user_data User data to associate with the timer.
1760 *
1761 * @return N/A
1762 */
Andrew Boiea354d492017-09-29 16:22:28 -07001763__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1764
Anas Nashif954d5502018-02-25 08:37:28 -06001765/**
1766 * @internal
1767 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001768static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001769 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001770{
1771 timer->user_data = user_data;
1772}
1773
1774/**
1775 * @brief Retrieve the user-specific data from a timer.
1776 *
1777 * @param timer Address of timer.
1778 *
1779 * @return The user data.
1780 */
Andrew Boiea354d492017-09-29 16:22:28 -07001781__syscall void *k_timer_user_data_get(struct k_timer *timer);
1782
Patrik Flykt4344e272019-03-08 14:19:05 -07001783static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001784{
1785 return timer->user_data;
1786}
1787
Anas Nashif166f5192018-02-25 08:02:36 -06001788/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001789
Allan Stephensc98da842016-11-11 15:45:03 -05001790/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001791 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001792 * @{
1793 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001794
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * This routine returns the elapsed time since the system booted,
1799 * in milliseconds.
1800 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001801 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001802 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001803 * While this function returns time in milliseconds, it does
1804 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001805 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001806 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001807 *
1808 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001809 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001810__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001811
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001812/**
1813 * @brief Enable clock always on in tickless kernel
1814 *
Andy Ross1db9f182019-06-25 10:09:45 -07001815 * Deprecated. This does nothing (it was always just a hint). This
1816 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1817 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001818 *
1819 * @retval prev_status Previous status of always on flag
1820 */
Andy Ross1db9f182019-06-25 10:09:45 -07001821/* LCOV_EXCL_START */
1822__deprecated static inline int k_enable_sys_clock_always_on(void)
1823{
1824 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1825 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1826
1827 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1828}
1829/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001830
1831/**
1832 * @brief Disable clock always on in tickless kernel
1833 *
Andy Ross1db9f182019-06-25 10:09:45 -07001834 * Deprecated. This does nothing (it was always just a hint). This
1835 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1836 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001837 */
Andy Ross1db9f182019-06-25 10:09:45 -07001838/* LCOV_EXCL_START */
1839__deprecated static inline void k_disable_sys_clock_always_on(void)
1840{
1841 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1842 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1843}
1844/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001845
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001848 *
Peter Bigota6067a32019-08-28 08:19:26 -05001849 * This routine returns the lower 32 bits of the system uptime in
1850 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001851 *
Peter Bigota6067a32019-08-28 08:19:26 -05001852 * Because correct conversion requires full precision of the system
1853 * clock there is no benefit to using this over k_uptime_get() unless
1854 * you know the application will never run long enough for the system
1855 * clock to approach 2^32 ticks. Calls to this function may involve
1856 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001857 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001858 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001859 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001860 * While this function returns time in milliseconds, it does
1861 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001862 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001863 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001864 *
Peter Bigota6067a32019-08-28 08:19:26 -05001865 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001866 */
Peter Bigota6067a32019-08-28 08:19:26 -05001867static inline u32_t k_uptime_get_32(void)
1868{
1869 return (u32_t)k_uptime_get();
1870}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001871
1872/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * This routine computes the elapsed time between the current system uptime
1876 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @param reftime Pointer to a reference time, which is updated to the current
1879 * uptime upon return.
1880 *
1881 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001882 */
Andy Ross987c0e52018-09-27 16:50:00 -07001883static inline s64_t k_uptime_delta(s64_t *reftime)
1884{
1885 s64_t uptime, delta;
1886
1887 uptime = k_uptime_get();
1888 delta = uptime - *reftime;
1889 *reftime = uptime;
1890
1891 return delta;
1892}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001893
1894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001895 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001897 * This routine computes the elapsed time between the current system uptime
1898 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1901 * need for interrupt locking and 64-bit math. However, the 32-bit result
1902 * cannot hold an elapsed time larger than approximately 50 days, so the
1903 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001905 * @param reftime Pointer to a reference time, which is updated to the current
1906 * uptime upon return.
1907 *
1908 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001909 */
Andy Ross987c0e52018-09-27 16:50:00 -07001910static inline u32_t k_uptime_delta_32(s64_t *reftime)
1911{
1912 return (u32_t)k_uptime_delta(reftime);
1913}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001914
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001915/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001916 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001918 * This routine returns the current time, as measured by the system's hardware
1919 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001923static inline u32_t k_cycle_get_32(void)
1924{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001925 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001926}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001927
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001928/**
Anas Nashif166f5192018-02-25 08:02:36 -06001929 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930 */
1931
Allan Stephensc98da842016-11-11 15:45:03 -05001932/**
1933 * @cond INTERNAL_HIDDEN
1934 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001935
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001936struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001937 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001938 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001939 union {
1940 _wait_q_t wait_q;
1941
1942 _POLL_EVENT;
1943 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001944
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001945 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001946 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001947};
1948
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001949#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001950 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001951 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001952 .lock = { }, \
1953 { \
1954 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1955 _POLL_EVENT_OBJ_INIT(obj) \
1956 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001957 _OBJECT_TRACING_INIT \
1958 }
1959
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001960#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001961
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1963
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001964/**
1965 * INTERNAL_HIDDEN @endcond
1966 */
1967
1968/**
1969 * @defgroup queue_apis Queue APIs
1970 * @ingroup kernel_apis
1971 * @{
1972 */
1973
1974/**
1975 * @brief Initialize a queue.
1976 *
1977 * This routine initializes a queue object, prior to its first use.
1978 *
1979 * @param queue Address of the queue.
1980 *
1981 * @return N/A
1982 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001983__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001984
1985/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001986 * @brief Cancel waiting on a queue.
1987 *
1988 * This routine causes first thread pending on @a queue, if any, to
1989 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001990 * If the queue is being waited on by k_poll(), it will return with
1991 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1992 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001993 *
1994 * @note Can be called by ISRs.
1995 *
1996 * @param queue Address of the queue.
1997 *
1998 * @return N/A
1999 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002000__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002001
2002/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002003 * @brief Append an element to the end of a queue.
2004 *
2005 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002006 * aligned on a word boundary, and the first word of the item is reserved
2007 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002008 *
2009 * @note Can be called by ISRs.
2010 *
2011 * @param queue Address of the queue.
2012 * @param data Address of the data item.
2013 *
2014 * @return N/A
2015 */
2016extern void k_queue_append(struct k_queue *queue, void *data);
2017
2018/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002019 * @brief Append an element to a queue.
2020 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002021 * This routine appends a data item to @a queue. There is an implicit memory
2022 * allocation to create an additional temporary bookkeeping data structure from
2023 * the calling thread's resource pool, which is automatically freed when the
2024 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002025 *
2026 * @note Can be called by ISRs.
2027 *
2028 * @param queue Address of the queue.
2029 * @param data Address of the data item.
2030 *
2031 * @retval 0 on success
2032 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2033 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302034__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002035
2036/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002037 * @brief Prepend an element to a queue.
2038 *
2039 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002040 * aligned on a word boundary, and the first word of the item is reserved
2041 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002042 *
2043 * @note Can be called by ISRs.
2044 *
2045 * @param queue Address of the queue.
2046 * @param data Address of the data item.
2047 *
2048 * @return N/A
2049 */
2050extern void k_queue_prepend(struct k_queue *queue, void *data);
2051
2052/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002053 * @brief Prepend an element to a queue.
2054 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002055 * This routine prepends a data item to @a queue. There is an implicit memory
2056 * allocation to create an additional temporary bookkeeping data structure from
2057 * the calling thread's resource pool, which is automatically freed when the
2058 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002059 *
2060 * @note Can be called by ISRs.
2061 *
2062 * @param queue Address of the queue.
2063 * @param data Address of the data item.
2064 *
2065 * @retval 0 on success
2066 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2067 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302068__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002069
2070/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002071 * @brief Inserts an element to a queue.
2072 *
2073 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002074 * data item must be aligned on a word boundary, and the first word of
2075 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002076 *
2077 * @note Can be called by ISRs.
2078 *
2079 * @param queue Address of the queue.
2080 * @param prev Address of the previous data item.
2081 * @param data Address of the data item.
2082 *
2083 * @return N/A
2084 */
2085extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2086
2087/**
2088 * @brief Atomically append a list of elements to a queue.
2089 *
2090 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002091 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002092 * in each data item pointing to the next data item; the list must be
2093 * NULL-terminated.
2094 *
2095 * @note Can be called by ISRs.
2096 *
2097 * @param queue Address of the queue.
2098 * @param head Pointer to first node in singly-linked list.
2099 * @param tail Pointer to last node in singly-linked list.
2100 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002101 * @retval 0 on success
2102 * @retval -EINVAL on invalid supplied data
2103 *
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002104 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002105extern int k_queue_append_list(struct k_queue *queue, void *head, void *tail);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002106
2107/**
2108 * @brief Atomically add a list of elements to a queue.
2109 *
2110 * This routine adds a list of data items to @a queue in one operation.
2111 * The data items must be in a singly-linked list implemented using a
2112 * sys_slist_t object. Upon completion, the original list is empty.
2113 *
2114 * @note Can be called by ISRs.
2115 *
2116 * @param queue Address of the queue.
2117 * @param list Pointer to sys_slist_t object.
2118 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002119 * @retval 0 on success
2120 * @retval -EINVAL on invalid data
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002121 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002122extern int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002123
2124/**
2125 * @brief Get an element from a queue.
2126 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002127 * This routine removes first data item from @a queue. The first word of the
2128 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002129 *
2130 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2131 *
2132 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002133 * @param timeout Non-negative waiting period to obtain a data item (in
2134 * milliseconds), or one of the special values K_NO_WAIT and
2135 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002136 *
2137 * @return Address of the data item if successful; NULL if returned
2138 * without waiting, or waiting period timed out.
2139 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002140__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002141
2142/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002143 * @brief Remove an element from a queue.
2144 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002145 * This routine removes data item from @a queue. The first word of the
2146 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002147 * rely on sys_slist_find_and_remove which is not a constant time operation.
2148 *
2149 * @note Can be called by ISRs
2150 *
2151 * @param queue Address of the queue.
2152 * @param data Address of the data item.
2153 *
2154 * @return true if data item was removed
2155 */
2156static inline bool k_queue_remove(struct k_queue *queue, void *data)
2157{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002158 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002159}
2160
2161/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002162 * @brief Append an element to a queue only if it's not present already.
2163 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002164 * This routine appends data item to @a queue. The first word of the data
2165 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002166 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2167 *
2168 * @note Can be called by ISRs
2169 *
2170 * @param queue Address of the queue.
2171 * @param data Address of the data item.
2172 *
2173 * @return true if data item was added, false if not
2174 */
2175static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2176{
2177 sys_sfnode_t *test;
2178
2179 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2180 if (test == (sys_sfnode_t *) data) {
2181 return false;
2182 }
2183 }
2184
2185 k_queue_append(queue, data);
2186 return true;
2187}
2188
2189/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002190 * @brief Query a queue to see if it has data available.
2191 *
2192 * Note that the data might be already gone by the time this function returns
2193 * if other threads are also trying to read from the queue.
2194 *
2195 * @note Can be called by ISRs.
2196 *
2197 * @param queue Address of the queue.
2198 *
2199 * @return Non-zero if the queue is empty.
2200 * @return 0 if data is available.
2201 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002202__syscall int k_queue_is_empty(struct k_queue *queue);
2203
Patrik Flykt4344e272019-03-08 14:19:05 -07002204static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002205{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002206 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002207}
2208
2209/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002210 * @brief Peek element at the head of queue.
2211 *
2212 * Return element from the head of queue without removing it.
2213 *
2214 * @param queue Address of the queue.
2215 *
2216 * @return Head element, or NULL if queue is empty.
2217 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002218__syscall void *k_queue_peek_head(struct k_queue *queue);
2219
Patrik Flykt4344e272019-03-08 14:19:05 -07002220static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002221{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002222 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002223}
2224
2225/**
2226 * @brief Peek element at the tail of queue.
2227 *
2228 * Return element from the tail of queue without removing it.
2229 *
2230 * @param queue Address of the queue.
2231 *
2232 * @return Tail element, or NULL if queue is empty.
2233 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002234__syscall void *k_queue_peek_tail(struct k_queue *queue);
2235
Patrik Flykt4344e272019-03-08 14:19:05 -07002236static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002237{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002238 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002239}
2240
2241/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002242 * @brief Statically define and initialize a queue.
2243 *
2244 * The queue can be accessed outside the module where it is defined using:
2245 *
2246 * @code extern struct k_queue <name>; @endcode
2247 *
2248 * @param name Name of the queue.
2249 */
2250#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002251 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002252 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002253
Anas Nashif166f5192018-02-25 08:02:36 -06002254/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002255
Wentong Wu5611e922019-06-20 23:51:27 +08002256#ifdef CONFIG_USERSPACE
2257/**
2258 * @brief futex structure
2259 *
2260 * A k_futex is a lightweight mutual exclusion primitive designed
2261 * to minimize kernel involvement. Uncontended operation relies
2262 * only on atomic access to shared memory. k_futex are tracked as
2263 * kernel objects and can live in user memory so any access bypass
2264 * the kernel object permission management mechanism.
2265 */
2266struct k_futex {
2267 atomic_t val;
2268};
2269
2270/**
2271 * @brief futex kernel data structure
2272 *
2273 * z_futex_data are the helper data structure for k_futex to complete
2274 * futex contended operation on kernel side, structure z_futex_data
2275 * of every futex object is invisible in user mode.
2276 */
2277struct z_futex_data {
2278 _wait_q_t wait_q;
2279 struct k_spinlock lock;
2280};
2281
2282#define Z_FUTEX_DATA_INITIALIZER(obj) \
2283 { \
2284 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2285 }
2286
2287/**
2288 * @defgroup futex_apis FUTEX APIs
2289 * @ingroup kernel_apis
2290 * @{
2291 */
2292
2293/**
Wentong Wu5611e922019-06-20 23:51:27 +08002294 * @brief Pend the current thread on a futex
2295 *
2296 * Tests that the supplied futex contains the expected value, and if so,
2297 * goes to sleep until some other thread calls k_futex_wake() on it.
2298 *
2299 * @param futex Address of the futex.
2300 * @param expected Expected value of the futex, if it is different the caller
2301 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002302 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2303 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002304 * @retval -EACCES Caller does not have read access to futex address.
2305 * @retval -EAGAIN If the futex value did not match the expected parameter.
2306 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2307 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2308 * @retval 0 if the caller went to sleep and was woken up. The caller
2309 * should check the futex's value on wakeup to determine if it needs
2310 * to block again.
2311 */
2312__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2313
2314/**
2315 * @brief Wake one/all threads pending on a futex
2316 *
2317 * Wake up the highest priority thread pending on the supplied futex, or
2318 * wakeup all the threads pending on the supplied futex, and the behavior
2319 * depends on wake_all.
2320 *
2321 * @param futex Futex to wake up pending threads.
2322 * @param wake_all If true, wake up all pending threads; If false,
2323 * wakeup the highest priority thread.
2324 * @retval -EACCES Caller does not have access to the futex address.
2325 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2326 * @retval Number of threads that were woken up.
2327 */
2328__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2329
2330/** @} */
2331#endif
2332
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002333struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002334 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335};
2336
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002337/**
2338 * @cond INTERNAL_HIDDEN
2339 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002340#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002341 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002342 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002343 }
2344
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002345#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002346
Allan Stephensc98da842016-11-11 15:45:03 -05002347/**
2348 * INTERNAL_HIDDEN @endcond
2349 */
2350
2351/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002352 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002353 * @ingroup kernel_apis
2354 * @{
2355 */
2356
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002358 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002360 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002362 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002363 *
2364 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002365 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002367#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002368 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369
2370/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002371 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002372 *
2373 * This routine causes first thread pending on @a fifo, if any, to
2374 * return from k_fifo_get() call with NULL value (as if timeout
2375 * expired).
2376 *
2377 * @note Can be called by ISRs.
2378 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002379 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002380 *
2381 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002382 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002383 */
2384#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002385 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002386
2387/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002388 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002389 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002390 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002391 * aligned on a word boundary, and the first word of the item is reserved
2392 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * @note Can be called by ISRs.
2395 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002396 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 *
2399 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002400 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002402#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002403 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404
2405/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002406 * @brief Add an element to a FIFO queue.
2407 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002408 * This routine adds a data item to @a fifo. There is an implicit memory
2409 * allocation to create an additional temporary bookkeeping data structure from
2410 * the calling thread's resource pool, which is automatically freed when the
2411 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002412 *
2413 * @note Can be called by ISRs.
2414 *
2415 * @param fifo Address of the FIFO.
2416 * @param data Address of the data item.
2417 *
2418 * @retval 0 on success
2419 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002420 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002421 */
2422#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002423 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002424
2425/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002426 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002429 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002430 * each data item pointing to the next data item; the list must be
2431 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002434 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002435 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * @param head Pointer to first node in singly-linked list.
2437 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438 *
2439 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002440 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002442#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002443 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444
2445/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002446 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * This routine adds a list of data items to @a fifo in one operation.
2449 * The data items must be in a singly-linked list implemented using a
2450 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002451 * and must be re-initialized via sys_slist_init().
2452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453 * @note Can be called by ISRs.
2454 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002455 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 *
2458 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002459 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002461#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002462 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463
2464/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002465 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002468 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2471 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002472 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 * or one of the special values K_NO_WAIT and K_FOREVER.
2475 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002476 * @return Address of the data item if successful; NULL if returned
2477 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002478 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002480#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002481 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002482
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002484 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002485 *
2486 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002487 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002488 *
2489 * @note Can be called by ISRs.
2490 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002491 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002492 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002493 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002494 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002495 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002496 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002497#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002498 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002499
2500/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002501 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002502 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002503 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302504 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002505 * on each iteration of processing, a head container will be peeked,
2506 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002507 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002508 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002509 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002510 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002511 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002512 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002513 */
2514#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002515 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002516
2517/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002518 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002519 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002520 * Return element from the tail of FIFO queue (without removing it). A usecase
2521 * for this is if elements of the FIFO queue are themselves containers. Then
2522 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002523 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002524 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002525 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002526 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002527 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002528 */
2529#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002530 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002531
2532/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002533 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002534 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002535 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002537 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002539 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002540 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002543 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002544 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
Anas Nashif166f5192018-02-25 08:02:36 -06002546/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002547
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002549 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550};
2551
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002552/**
2553 * @cond INTERNAL_HIDDEN
2554 */
2555
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002556#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002557 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002558 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002559 }
2560
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002561#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002562
Allan Stephensc98da842016-11-11 15:45:03 -05002563/**
2564 * INTERNAL_HIDDEN @endcond
2565 */
2566
2567/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002568 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002569 * @ingroup kernel_apis
2570 * @{
2571 */
2572
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002573/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002574 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002576 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002577 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002578 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002579 *
2580 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002581 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002582 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002583#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002584 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002585
2586/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002587 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002588 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002589 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002590 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002591 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 * @note Can be called by ISRs.
2594 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002595 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002597 *
2598 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002599 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002600 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002601#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002602 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603
2604/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002605 * @brief Add an element to a LIFO queue.
2606 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002607 * This routine adds a data item to @a lifo. There is an implicit memory
2608 * allocation to create an additional temporary bookkeeping data structure from
2609 * the calling thread's resource pool, which is automatically freed when the
2610 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002611 *
2612 * @note Can be called by ISRs.
2613 *
2614 * @param lifo Address of the LIFO.
2615 * @param data Address of the data item.
2616 *
2617 * @retval 0 on success
2618 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002619 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002620 */
2621#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002622 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002623
2624/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002625 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002628 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2631 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002632 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634 * or one of the special values K_NO_WAIT and K_FOREVER.
2635 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002636 * @return Address of the data item if successful; NULL if returned
2637 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002638 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002640#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002641 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002643/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002644 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002645 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002646 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002647 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002648 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002651 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002652 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002653#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002654 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002655 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656
Anas Nashif166f5192018-02-25 08:02:36 -06002657/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002658
2659/**
2660 * @cond INTERNAL_HIDDEN
2661 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302662#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002664typedef uintptr_t stack_data_t;
2665
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666struct k_stack {
2667 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002668 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002669 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002671 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002672 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002673 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674};
2675
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002676#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002677 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002678 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002679 .base = stack_buffer, \
2680 .next = stack_buffer, \
2681 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002682 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002683 }
2684
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002685#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002686
Allan Stephensc98da842016-11-11 15:45:03 -05002687/**
2688 * INTERNAL_HIDDEN @endcond
2689 */
2690
2691/**
2692 * @defgroup stack_apis Stack APIs
2693 * @ingroup kernel_apis
2694 * @{
2695 */
2696
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002697/**
2698 * @brief Initialize a stack.
2699 *
2700 * This routine initializes a stack object, prior to its first use.
2701 *
2702 * @param stack Address of the stack.
2703 * @param buffer Address of array used to hold stacked values.
2704 * @param num_entries Maximum number of values that can be stacked.
2705 *
2706 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002707 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002708 */
Andrew Boief3bee952018-05-02 17:44:39 -07002709void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002710 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002711
2712
2713/**
2714 * @brief Initialize a stack.
2715 *
2716 * This routine initializes a stack object, prior to its first use. Internal
2717 * buffers will be allocated from the calling thread's resource pool.
2718 * This memory will be released if k_stack_cleanup() is called, or
2719 * userspace is enabled and the stack object loses all references to it.
2720 *
2721 * @param stack Address of the stack.
2722 * @param num_entries Maximum number of values that can be stacked.
2723 *
2724 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002725 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002726 */
2727
Adithya Baglody28080d32018-10-15 11:48:51 +05302728__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2729 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002730
2731/**
2732 * @brief Release a stack's allocated buffer
2733 *
2734 * If a stack object was given a dynamically allocated buffer via
2735 * k_stack_alloc_init(), this will free it. This function does nothing
2736 * if the buffer wasn't dynamically allocated.
2737 *
2738 * @param stack Address of the stack.
Anas Nashif1ed67d12019-06-16 08:58:10 -04002739 * @retval 0 on success
2740 * @retval -EAGAIN when object is still in use
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002741 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002742 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002743int k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002744
2745/**
2746 * @brief Push an element onto a stack.
2747 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002748 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002749 *
2750 * @note Can be called by ISRs.
2751 *
2752 * @param stack Address of the stack.
2753 * @param data Value to push onto the stack.
2754 *
Anas Nashif1ed67d12019-06-16 08:58:10 -04002755 * @retval 0 on success
2756 * @retval -ENOMEM if stack is full
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002757 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002759__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760
2761/**
2762 * @brief Pop an element from a stack.
2763 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002764 * This routine removes a stack_data_t value from @a stack in a "last in,
2765 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002766 *
2767 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2768 *
2769 * @param stack Address of the stack.
2770 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002771 * @param timeout Non-negative waiting period to obtain a value (in
2772 * milliseconds), or one of the special values K_NO_WAIT and
2773 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002774 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002775 * @retval 0 Element popped from stack.
2776 * @retval -EBUSY Returned without waiting.
2777 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002778 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002780__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2781 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002784 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002786 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002788 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * @param name Name of the stack.
2791 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002792 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002793 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002794#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002795 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002796 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002797 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002798 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002799 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800
Anas Nashif166f5192018-02-25 08:02:36 -06002801/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002802
Allan Stephens6bba9b02016-11-16 14:56:54 -05002803struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002804struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002805
Piotr Zięcik19d83492019-09-27 09:16:25 +02002806/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002807typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2808struct _poller {
2809 volatile bool is_polling;
2810 struct k_thread *thread;
2811 _poller_cb_t cb;
2812};
2813
Allan Stephensc98da842016-11-11 15:45:03 -05002814/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002815 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002816 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817 */
2818
Allan Stephens6bba9b02016-11-16 14:56:54 -05002819/**
2820 * @typedef k_work_handler_t
2821 * @brief Work item handler function type.
2822 *
2823 * A work item's handler function is executed by a workqueue's thread
2824 * when the work item is processed by the workqueue.
2825 *
2826 * @param work Address of the work item.
2827 *
2828 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002829 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002830 */
2831typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832
2833/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002834 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002836
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002838 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002839 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840};
2841
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002843 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844};
2845
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002846struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002847 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848 k_work_handler_t handler;
2849 atomic_t flags[1];
2850};
2851
Allan Stephens6bba9b02016-11-16 14:56:54 -05002852struct k_delayed_work {
2853 struct k_work work;
2854 struct _timeout timeout;
2855 struct k_work_q *work_q;
2856};
2857
Piotr Zięcik19d83492019-09-27 09:16:25 +02002858struct k_work_poll {
2859 struct k_work work;
2860 struct _poller poller;
2861 struct k_poll_event *events;
2862 int num_events;
2863 k_work_handler_t real_handler;
2864 struct _timeout timeout;
2865 int poll_result;
2866};
2867
Allan Stephens6bba9b02016-11-16 14:56:54 -05002868extern struct k_work_q k_sys_work_q;
2869
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002871 * INTERNAL_HIDDEN @endcond
2872 */
2873
Patrik Flykt4344e272019-03-08 14:19:05 -07002874#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002875 { \
2876 ._reserved = NULL, \
2877 .handler = work_handler, \
2878 .flags = { 0 } \
2879 }
2880
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002881#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002882
Allan Stephens6bba9b02016-11-16 14:56:54 -05002883/**
2884 * @brief Initialize a statically-defined work item.
2885 *
2886 * This macro can be used to initialize a statically-defined workqueue work
2887 * item, prior to its first use. For example,
2888 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002889 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002890 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002891 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002892 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002893 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002894 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002895#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002896 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002897
2898/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002899 * @brief Initialize a work item.
2900 *
2901 * This routine initializes a workqueue work item, prior to its first use.
2902 *
2903 * @param work Address of work item.
2904 * @param handler Function to invoke each time work item is processed.
2905 *
2906 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002907 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908 */
2909static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2910{
Patrik Flykt4344e272019-03-08 14:19:05 -07002911 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912}
2913
2914/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002915 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002916 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002917 * This routine submits work item @a work to be processed by workqueue
2918 * @a work_q. If the work item is already pending in the workqueue's queue
2919 * as a result of an earlier submission, this routine has no effect on the
2920 * work item. If the work item has already been processed, or is currently
2921 * being processed, its work is considered complete and the work item can be
2922 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002923 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002924 * @warning
2925 * A submitted work item must not be modified until it has been processed
2926 * by the workqueue.
2927 *
2928 * @note Can be called by ISRs.
2929 *
2930 * @param work_q Address of workqueue.
2931 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002932 *
2933 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002934 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935 */
2936static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2937 struct k_work *work)
2938{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002939 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002940 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941 }
2942}
2943
2944/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002945 * @brief Submit a work item to a user mode workqueue
2946 *
David B. Kinder06d78352018-12-17 14:32:40 -08002947 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002948 * memory allocation is made from the caller's resource pool which is freed
2949 * once the worker thread consumes the k_work item. The workqueue
2950 * thread must have memory access to the k_work item being submitted. The caller
2951 * must have permission granted on the work_q parameter's queue object.
2952 *
2953 * Otherwise this works the same as k_work_submit_to_queue().
2954 *
2955 * @note Can be called by ISRs.
2956 *
2957 * @param work_q Address of workqueue.
2958 * @param work Address of work item.
2959 *
2960 * @retval -EBUSY if the work item was already in some workqueue
2961 * @retval -ENOMEM if no memory for thread resource pool allocation
2962 * @retval 0 Success
2963 * @req K-WORK-001
2964 */
2965static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2966 struct k_work *work)
2967{
2968 int ret = -EBUSY;
2969
2970 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2971 ret = k_queue_alloc_append(&work_q->queue, work);
2972
2973 /* Couldn't insert into the queue. Clear the pending bit
2974 * so the work item can be submitted again
2975 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002976 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002977 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2978 }
2979 }
2980
2981 return ret;
2982}
2983
2984/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002985 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002986 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002987 * This routine indicates if work item @a work is pending in a workqueue's
2988 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002989 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002990 * @note Can be called by ISRs.
2991 *
2992 * @param work Address of work item.
2993 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002994 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002995 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002996 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002997static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002998{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002999 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03003000}
3001
3002/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003003 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003004 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003005 * This routine starts workqueue @a work_q. The workqueue spawns its work
3006 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003008 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07003009 * @param stack Pointer to work queue thread's stack space, as defined by
3010 * K_THREAD_STACK_DEFINE()
3011 * @param stack_size Size of the work queue thread's stack (in bytes), which
3012 * should either be the same constant passed to
3013 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05003014 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003015 *
3016 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003017 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018 */
Andrew Boie507852a2017-07-25 18:47:07 -07003019extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07003020 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05003021 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003022
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003023/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003024 * @brief Start a workqueue in user mode
3025 *
3026 * This works identically to k_work_q_start() except it is callable from user
3027 * mode, and the worker thread created will run in user mode.
3028 * The caller must have permissions granted on both the work_q parameter's
3029 * thread and queue objects, and the same restrictions on priority apply as
3030 * k_thread_create().
3031 *
3032 * @param work_q Address of workqueue.
3033 * @param stack Pointer to work queue thread's stack space, as defined by
3034 * K_THREAD_STACK_DEFINE()
3035 * @param stack_size Size of the work queue thread's stack (in bytes), which
3036 * should either be the same constant passed to
3037 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
3038 * @param prio Priority of the work queue's thread.
3039 *
3040 * @return N/A
3041 * @req K-WORK-001
3042 */
3043extern void k_work_q_user_start(struct k_work_q *work_q,
3044 k_thread_stack_t *stack,
3045 size_t stack_size, int prio);
3046
3047/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003048 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003049 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003050 * This routine initializes a workqueue delayed work item, prior to
3051 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003052 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003053 * @param work Address of delayed work item.
3054 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003055 *
3056 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003057 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003059extern void k_delayed_work_init(struct k_delayed_work *work,
3060 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061
3062/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003063 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003065 * This routine schedules work item @a work to be processed by workqueue
3066 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003067 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003068 * Only when the countdown completes is the work item actually submitted to
3069 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003071 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08003072 * counting down cancels the existing submission and restarts the
3073 * countdown using the new delay. Note that this behavior is
3074 * inherently subject to race conditions with the pre-existing
3075 * timeouts and work queue, so care must be taken to synchronize such
3076 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003078 * @warning
3079 * A delayed work item must not be modified until it has been processed
3080 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003082 * @note Can be called by ISRs.
3083 *
3084 * @param work_q Address of workqueue.
3085 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003086 * @param delay Non-negative delay before submitting the work item (in
3087 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003088 *
3089 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003090 * @retval -EINVAL Work item is being processed or has completed its work.
3091 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003092 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003094extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3095 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003096 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097
3098/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003099 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003101 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003102 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003103 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003104 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003105 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003107 * @note The result of calling this on a k_delayed_work item that has
3108 * not been submitted (i.e. before the return of the
3109 * k_delayed_work_submit_to_queue() call) is undefined.
3110 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003111 * @param work Address of delayed work item.
3112 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003113 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003114 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003115 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003117extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003120 * @brief Submit a work item to the system workqueue.
3121 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003122 * This routine submits work item @a work to be processed by the system
3123 * workqueue. If the work item is already pending in the workqueue's queue
3124 * as a result of an earlier submission, this routine has no effect on the
3125 * work item. If the work item has already been processed, or is currently
3126 * being processed, its work is considered complete and the work item can be
3127 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003128 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003129 * @warning
3130 * Work items submitted to the system workqueue should avoid using handlers
3131 * that block or yield since this may prevent the system workqueue from
3132 * processing other work items in a timely manner.
3133 *
3134 * @note Can be called by ISRs.
3135 *
3136 * @param work Address of work item.
3137 *
3138 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003139 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140 */
3141static inline void k_work_submit(struct k_work *work)
3142{
3143 k_work_submit_to_queue(&k_sys_work_q, work);
3144}
3145
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003146/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003147 * @brief Submit a delayed work item to the system workqueue.
3148 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003149 * This routine schedules work item @a work to be processed by the system
3150 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003151 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003152 * Only when the countdown completes is the work item actually submitted to
3153 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003155 * Submitting a previously submitted delayed work item that is still
3156 * counting down cancels the existing submission and restarts the countdown
3157 * using the new delay. If the work item is currently pending on the
3158 * workqueue's queue because the countdown has completed it is too late to
3159 * resubmit the item, and resubmission fails without impacting the work item.
3160 * If the work item has already been processed, or is currently being processed,
3161 * its work is considered complete and the work item can be resubmitted.
3162 *
3163 * @warning
3164 * Work items submitted to the system workqueue should avoid using handlers
3165 * that block or yield since this may prevent the system workqueue from
3166 * processing other work items in a timely manner.
3167 *
3168 * @note Can be called by ISRs.
3169 *
3170 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003171 * @param delay Non-negative delay before submitting the work item (in
3172 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003173 *
3174 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003175 * @retval -EINVAL Work item is being processed or has completed its work.
3176 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003177 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003178 */
3179static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003180 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003181{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003182 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183}
3184
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003186 * @brief Get time remaining before a delayed work gets scheduled.
3187 *
3188 * This routine computes the (approximate) time remaining before a
3189 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003190 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003191 *
3192 * @param work Delayed work item.
3193 *
3194 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003195 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003196 */
Kumar Galacc334c72017-04-21 10:55:34 -05003197static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003198{
Andy Ross88924062019-10-03 11:43:10 -07003199 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003200}
3201
Piotr Zięcik19d83492019-09-27 09:16:25 +02003202/**
3203 * @brief Initialize a triggered work item.
3204 *
3205 * This routine initializes a workqueue triggered work item, prior to
3206 * its first use.
3207 *
3208 * @param work Address of triggered work item.
3209 * @param handler Function to invoke each time work item is processed.
3210 *
3211 * @return N/A
3212 */
3213extern void k_work_poll_init(struct k_work_poll *work,
3214 k_work_handler_t handler);
3215
3216/**
3217 * @brief Submit a triggered work item.
3218 *
3219 * This routine schedules work item @a work to be processed by workqueue
3220 * @a work_q when one of the given @a events is signaled. The routine
3221 * initiates internal poller for the work item and then returns to the caller.
3222 * Only when one of the watched events happen the work item is actually
3223 * submitted to the workqueue and becomes pending.
3224 *
3225 * Submitting a previously submitted triggered work item that is still
3226 * waiting for the event cancels the existing submission and reschedules it
3227 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003228 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003229 * so care must be taken to synchronize such resubmissions externally.
3230 *
3231 * @note Can be called by ISRs.
3232 *
3233 * @warning
3234 * Provided array of events as well as a triggered work item must be placed
3235 * in persistent memory (valid until work handler execution or work
3236 * cancellation) and cannot be modified after submission.
3237 *
3238 * @param work_q Address of workqueue.
3239 * @param work Address of delayed work item.
3240 * @param events An array of pointers to events which trigger the work.
3241 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003242 * @param timeout Non-negative timeout after which the work will be scheduled
3243 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003244 *
3245 *
3246 * @retval 0 Work item started watching for events.
3247 * @retval -EINVAL Work item is being processed or has completed its work.
3248 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3249 */
3250extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3251 struct k_work_poll *work,
3252 struct k_poll_event *events,
3253 int num_events,
3254 s32_t timeout);
3255
3256/**
3257 * @brief Submit a triggered work item to the system workqueue.
3258 *
3259 * This routine schedules work item @a work to be processed by system
3260 * workqueue when one of the given @a events is signaled. The routine
3261 * initiates internal poller for the work item and then returns to the caller.
3262 * Only when one of the watched events happen the work item is actually
3263 * submitted to the workqueue and becomes pending.
3264 *
3265 * Submitting a previously submitted triggered work item that is still
3266 * waiting for the event cancels the existing submission and reschedules it
3267 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003268 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003269 * so care must be taken to synchronize such resubmissions externally.
3270 *
3271 * @note Can be called by ISRs.
3272 *
3273 * @warning
3274 * Provided array of events as well as a triggered work item must not be
3275 * modified until the item has been processed by the workqueue.
3276 *
3277 * @param work Address of delayed work item.
3278 * @param events An array of pointers to events which trigger the work.
3279 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003280 * @param timeout Non-negative timeout after which the work will be scheduled
3281 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003282 *
3283 * @retval 0 Work item started watching for events.
3284 * @retval -EINVAL Work item is being processed or has completed its work.
3285 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3286 */
3287static inline int k_work_poll_submit(struct k_work_poll *work,
3288 struct k_poll_event *events,
3289 int num_events,
3290 s32_t timeout)
3291{
3292 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3293 events, num_events, timeout);
3294}
3295
3296/**
3297 * @brief Cancel a triggered work item.
3298 *
3299 * This routine cancels the submission of triggered work item @a work.
3300 * A triggered work item can only be canceled if no event triggered work
3301 * submission.
3302 *
3303 * @note Can be called by ISRs.
3304 *
3305 * @param work Address of delayed work item.
3306 *
David B. Kinder73896c02019-10-28 16:27:57 -07003307 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003308 * @retval -EINVAL Work item is being processed or has completed its work.
3309 */
3310extern int k_work_poll_cancel(struct k_work_poll *work);
3311
Anas Nashif166f5192018-02-25 08:02:36 -06003312/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003313/**
Anas Nashifce78d162018-05-24 12:43:11 -05003314 * @defgroup mutex_apis Mutex APIs
3315 * @ingroup kernel_apis
3316 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003317 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318
Anas Nashifce78d162018-05-24 12:43:11 -05003319/**
3320 * Mutex Structure
3321 * @ingroup mutex_apis
3322 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003323struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05003324 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003325 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003326 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003327 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05003328
3329 /** Current lock count */
Kumar Galacc334c72017-04-21 10:55:34 -05003330 u32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05003331
3332 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003335 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003336 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337};
3338
Anas Nashifce78d162018-05-24 12:43:11 -05003339/**
3340 * @cond INTERNAL_HIDDEN
3341 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003342#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003344 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345 .owner = NULL, \
3346 .lock_count = 0, \
3347 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003348 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003349 }
3350
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003351#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003352
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003353/**
Allan Stephensc98da842016-11-11 15:45:03 -05003354 * INTERNAL_HIDDEN @endcond
3355 */
3356
3357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003361 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003362 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003365 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003366 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003368 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003369 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003371/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * Upon completion, the mutex is available and does not have an owner.
3377 *
3378 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003379 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003380 * @retval 0 Mutex object created
3381 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003382 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003383 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003384__syscall int k_mutex_init(struct k_mutex *mutex);
3385
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003386
3387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * This routine locks @a mutex. If the mutex is locked by another thread,
3391 * the calling thread waits until the mutex becomes available or until
3392 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * A thread is permitted to lock a mutex it has already locked. The operation
3395 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003398 * @param timeout Non-negative waiting period to lock the mutex (in
3399 * milliseconds), or one of the special values K_NO_WAIT and
3400 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003401 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003402 * @retval 0 Mutex locked.
3403 * @retval -EBUSY Returned without waiting.
3404 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003405 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003406 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003407__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003408
3409/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * This routine unlocks @a mutex. The mutex must already be locked by the
3413 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003414 *
3415 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003417 * thread.
3418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003420 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003421 * @retval 0 Mutex unlocked.
3422 * @retval -EPERM The current thread does not own the mutex
3423 * @retval -EINVAL The mutex is not locked
3424 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003425 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003426 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003427__syscall int k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428
Allan Stephensc98da842016-11-11 15:45:03 -05003429/**
Anas Nashif166f5192018-02-25 08:02:36 -06003430 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003431 */
3432
3433/**
3434 * @cond INTERNAL_HIDDEN
3435 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003436
3437struct k_sem {
3438 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303439 u32_t count;
3440 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003441 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003443 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003444 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003445};
3446
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003447#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003448 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003449 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003450 .count = initial_count, \
3451 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003452 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003453 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003454 }
3455
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003456#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003457
Allan Stephensc98da842016-11-11 15:45:03 -05003458/**
3459 * INTERNAL_HIDDEN @endcond
3460 */
3461
3462/**
3463 * @defgroup semaphore_apis Semaphore APIs
3464 * @ingroup kernel_apis
3465 * @{
3466 */
3467
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003473 * @param sem Address of the semaphore.
3474 * @param initial_count Initial semaphore count.
3475 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003476 *
Anas Nashif928af3c2019-05-04 10:36:14 -04003477 * @retval 0 Semaphore created successfully
3478 * @retval -EINVAL Invalid values
3479 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003480 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003481 */
Anas Nashif928af3c2019-05-04 10:36:14 -04003482__syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count,
Andrew Boie99280232017-09-29 14:17:47 -07003483 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003484
3485/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003488 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3491 *
3492 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003493 * @param timeout Non-negative waiting period to take the semaphore (in
3494 * milliseconds), or one of the special values K_NO_WAIT and
3495 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003496 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003497 * @retval 0 Semaphore taken.
3498 * @retval -EBUSY Returned without waiting.
3499 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003500 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003501 */
Andrew Boie99280232017-09-29 14:17:47 -07003502__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003503
3504/**
3505 * @brief Give a semaphore.
3506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * This routine gives @a sem, unless the semaphore is already at its maximum
3508 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003512 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003513 *
3514 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003515 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003516 */
Andrew Boie99280232017-09-29 14:17:47 -07003517__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003519/**
3520 * @brief Reset a semaphore's count to zero.
3521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003524 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003525 *
3526 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003527 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003528 */
Andrew Boie990bf162017-10-03 12:36:49 -07003529__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003530
Anas Nashif954d5502018-02-25 08:37:28 -06003531/**
3532 * @internal
3533 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003534static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003535{
Patrik Flykt24d71432019-03-26 19:57:45 -06003536 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003537}
3538
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003539/**
3540 * @brief Get a semaphore's count.
3541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003547 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003548 */
Andrew Boie990bf162017-10-03 12:36:49 -07003549__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003550
Anas Nashif954d5502018-02-25 08:37:28 -06003551/**
3552 * @internal
3553 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003554static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003555{
3556 return sem->count;
3557}
3558
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003563 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003564 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003566 * @param name Name of the semaphore.
3567 * @param initial_count Initial semaphore count.
3568 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003569 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003570 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003571#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003572 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003573 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303574 BUILD_ASSERT(((count_limit) != 0) && \
3575 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003576
Anas Nashif166f5192018-02-25 08:02:36 -06003577/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003578
3579/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003580 * @defgroup msgq_apis Message Queue APIs
3581 * @ingroup kernel_apis
3582 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003583 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003584
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003585/**
3586 * @brief Message Queue Structure
3587 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003588struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05003589 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05003591 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07003592 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05003593 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003594 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003595 /** Maximal number of messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003596 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003597 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003598 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05003599 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003600 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05003601 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003602 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003603 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003604 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003605 /** Number of used messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003606 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003607
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003608 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003609 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05003610
3611 /** Message queue */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003612 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003613};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003614/**
3615 * @cond INTERNAL_HIDDEN
3616 */
3617
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003618
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003619#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003620 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003621 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003622 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003623 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003624 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003625 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003626 .read_ptr = q_buffer, \
3627 .write_ptr = q_buffer, \
3628 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003629 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003630 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003631#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003632/**
3633 * INTERNAL_HIDDEN @endcond
3634 */
3635
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003636
Andrew Boie0fe789f2018-04-12 18:35:56 -07003637#define K_MSGQ_FLAG_ALLOC BIT(0)
3638
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003639/**
3640 * @brief Message Queue Attributes
3641 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303642struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05003643 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303644 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003645 /** Maximal number of messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303646 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003647 /** Used messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303648 u32_t used_msgs;
3649};
3650
Allan Stephensc98da842016-11-11 15:45:03 -05003651
3652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003653 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3656 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003657 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3658 * message is similarly aligned to this boundary, @a q_msg_size must also be
3659 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003661 * The message queue can be accessed outside the module where it is defined
3662 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003663 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003664 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * @param q_name Name of the message queue.
3667 * @param q_msg_size Message size (in bytes).
3668 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003669 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003670 *
3671 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003672 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003673#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3674 static char __noinit __aligned(q_align) \
3675 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3676 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3677 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003678 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003679
Peter Mitsisd7a37502016-10-13 11:37:40 -04003680/**
3681 * @brief Initialize a message queue.
3682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * This routine initializes a message queue object, prior to its first use.
3684 *
Allan Stephensda827222016-11-09 14:23:58 -06003685 * The message queue's ring buffer must contain space for @a max_msgs messages,
3686 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3687 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3688 * that each message is similarly aligned to this boundary, @a q_msg_size
3689 * must also be a multiple of N.
3690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @param q Address of the message queue.
3692 * @param buffer Pointer to ring buffer that holds queued messages.
3693 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003694 * @param max_msgs Maximum number of messages that can be queued.
3695 *
3696 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003697 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003698 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003699void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3700 u32_t max_msgs);
3701
3702/**
3703 * @brief Initialize a message queue.
3704 *
3705 * This routine initializes a message queue object, prior to its first use,
3706 * allocating its internal ring buffer from the calling thread's resource
3707 * pool.
3708 *
3709 * Memory allocated for the ring buffer can be released by calling
3710 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3711 * all of its references.
3712 *
Anas Nashif4b386592019-11-25 09:30:47 -05003713 * @param msgq Address of the message queue.
Andrew Boie0fe789f2018-04-12 18:35:56 -07003714 * @param msg_size Message size (in bytes).
3715 * @param max_msgs Maximum number of messages that can be queued.
3716 *
3717 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3718 * thread's resource pool, or -EINVAL if the size parameters cause
3719 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003720 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003721 */
Anas Nashif4b386592019-11-25 09:30:47 -05003722__syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size,
Andrew Boie0fe789f2018-04-12 18:35:56 -07003723 u32_t max_msgs);
3724
Anas Nashife71293e2019-12-04 20:00:14 -05003725/**
Anas Nashif4b386592019-11-25 09:30:47 -05003726 * @brief Release allocated buffer for a queue
Anas Nashife71293e2019-12-04 20:00:14 -05003727 *
3728 * Releases memory allocated for the ring buffer.
Anas Nashif4b386592019-11-25 09:30:47 -05003729 *
3730 * @param msgq message queue to cleanup
3731 *
Anas Nashif11b93652019-06-16 08:43:48 -04003732 * @retval 0 on success
3733 * @retval -EBUSY Queue not empty
Anas Nashife71293e2019-12-04 20:00:14 -05003734 */
Anas Nashif11b93652019-06-16 08:43:48 -04003735int k_msgq_cleanup(struct k_msgq *msgq);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003736
3737/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003738 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003740 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003741 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003742 * @note Can be called by ISRs.
3743 *
Anas Nashif4b386592019-11-25 09:30:47 -05003744 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003745 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003746 * @param timeout Non-negative waiting period to add the message (in
3747 * milliseconds), or one of the special values K_NO_WAIT and
3748 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003749 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003750 * @retval 0 Message sent.
3751 * @retval -ENOMSG Returned without waiting or queue purged.
3752 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003753 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003754 */
Anas Nashif4b386592019-11-25 09:30:47 -05003755__syscall int k_msgq_put(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003756
3757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003758 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003760 * This routine receives a message from message queue @a q in a "first in,
3761 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003762 *
Allan Stephensc98da842016-11-11 15:45:03 -05003763 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003764 *
Anas Nashif4b386592019-11-25 09:30:47 -05003765 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003766 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003767 * @param timeout Non-negative waiting period to receive the message (in
3768 * milliseconds), or one of the special values K_NO_WAIT and
3769 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003770 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003771 * @retval 0 Message received.
3772 * @retval -ENOMSG Returned without waiting.
3773 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003774 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003775 */
Anas Nashif4b386592019-11-25 09:30:47 -05003776__syscall int k_msgq_get(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003777
3778/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003779 * @brief Peek/read a message from a message queue.
3780 *
3781 * This routine reads a message from message queue @a q in a "first in,
3782 * first out" manner and leaves the message in the queue.
3783 *
3784 * @note Can be called by ISRs.
3785 *
Anas Nashif4b386592019-11-25 09:30:47 -05003786 * @param msgq Address of the message queue.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003787 * @param data Address of area to hold the message read from the queue.
3788 *
3789 * @retval 0 Message read.
3790 * @retval -ENOMSG Returned when the queue has no message.
3791 * @req K-MSGQ-002
3792 */
Anas Nashif4b386592019-11-25 09:30:47 -05003793__syscall int k_msgq_peek(struct k_msgq *msgq, void *data);
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003794
3795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003796 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003798 * This routine discards all unreceived messages in a message queue's ring
3799 * buffer. Any threads that are blocked waiting to send a message to the
3800 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003801 *
Anas Nashif4b386592019-11-25 09:30:47 -05003802 * @param msgq Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003803 *
3804 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003805 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003806 */
Anas Nashif4b386592019-11-25 09:30:47 -05003807__syscall void k_msgq_purge(struct k_msgq *msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003808
Peter Mitsis67be2492016-10-07 11:44:34 -04003809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003810 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003812 * This routine returns the number of unused entries in a message queue's
3813 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003814 *
Anas Nashif4b386592019-11-25 09:30:47 -05003815 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003816 *
3817 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003818 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003819 */
Anas Nashif4b386592019-11-25 09:30:47 -05003820__syscall u32_t k_msgq_num_free_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003821
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303822/**
3823 * @brief Get basic attributes of a message queue.
3824 *
3825 * This routine fetches basic attributes of message queue into attr argument.
3826 *
Anas Nashif4b386592019-11-25 09:30:47 -05003827 * @param msgq Address of the message queue.
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303828 * @param attrs pointer to message queue attribute structure.
3829 *
3830 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003831 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303832 */
Anas Nashif4b386592019-11-25 09:30:47 -05003833__syscall void k_msgq_get_attrs(struct k_msgq *msgq,
3834 struct k_msgq_attrs *attrs);
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303835
3836
Anas Nashif4b386592019-11-25 09:30:47 -05003837static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq)
Peter Mitsis67be2492016-10-07 11:44:34 -04003838{
Anas Nashif4b386592019-11-25 09:30:47 -05003839 return msgq->max_msgs - msgq->used_msgs;
Peter Mitsis67be2492016-10-07 11:44:34 -04003840}
3841
Peter Mitsisd7a37502016-10-13 11:37:40 -04003842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003843 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003846 *
Anas Nashif4b386592019-11-25 09:30:47 -05003847 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003848 *
3849 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003850 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003851 */
Anas Nashif4b386592019-11-25 09:30:47 -05003852__syscall u32_t k_msgq_num_used_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003853
Anas Nashif4b386592019-11-25 09:30:47 -05003854static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855{
Anas Nashif4b386592019-11-25 09:30:47 -05003856 return msgq->used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857}
3858
Anas Nashif166f5192018-02-25 08:02:36 -06003859/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003860
3861/**
3862 * @defgroup mem_pool_apis Memory Pool APIs
3863 * @ingroup kernel_apis
3864 * @{
3865 */
3866
Andy Ross73cb9582017-05-09 10:42:39 -07003867/* Note on sizing: the use of a 20 bit field for block means that,
3868 * assuming a reasonable minimum block size of 16 bytes, we're limited
3869 * to 16M of memory managed by a single pool. Long term it would be
3870 * good to move to a variable bit size based on configuration.
3871 */
3872struct k_mem_block_id {
3873 u32_t pool : 8;
3874 u32_t level : 4;
3875 u32_t block : 20;
3876};
3877
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003878struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003879 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003880 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881};
3882
Anas Nashif166f5192018-02-25 08:02:36 -06003883/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003884
3885/**
3886 * @defgroup mailbox_apis Mailbox APIs
3887 * @ingroup kernel_apis
3888 * @{
3889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003890
Anas Nashife71293e2019-12-04 20:00:14 -05003891/**
3892 * @brief Mailbox Message Structure
3893 *
3894 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003895struct k_mbox_msg {
3896 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003897 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003899 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003900 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003901 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003902 /** sender's message data buffer */
3903 void *tx_data;
3904 /** internal use only - needed for legacy API support */
3905 void *_rx_data;
3906 /** message data block descriptor */
3907 struct k_mem_block tx_block;
3908 /** source thread id */
3909 k_tid_t rx_source_thread;
3910 /** target thread id */
3911 k_tid_t tx_target_thread;
3912 /** internal use only - thread waiting on send (may be a dummy) */
3913 k_tid_t _syncing_thread;
3914#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3915 /** internal use only - semaphore used during asynchronous send */
3916 struct k_sem *_async_sem;
3917#endif
3918};
Anas Nashife71293e2019-12-04 20:00:14 -05003919/**
3920 * @brief Mailbox Structure
3921 *
3922 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003923struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05003924 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003925 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05003926 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003927 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003928 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003929
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003930 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003931 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003932};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003933/**
3934 * @cond INTERNAL_HIDDEN
3935 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003936
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003937#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003939 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3940 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003941 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003942 }
3943
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003944#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003945
Peter Mitsis12092702016-10-14 12:57:23 -04003946/**
Allan Stephensc98da842016-11-11 15:45:03 -05003947 * INTERNAL_HIDDEN @endcond
3948 */
3949
3950/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003951 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003954 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003955 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003957 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003958 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003959 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003960#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003961 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003962 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003963
Peter Mitsis12092702016-10-14 12:57:23 -04003964/**
3965 * @brief Initialize a mailbox.
3966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003967 * This routine initializes a mailbox object, prior to its first use.
3968 *
3969 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003970 *
3971 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003972 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003973 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974extern void k_mbox_init(struct k_mbox *mbox);
3975
Peter Mitsis12092702016-10-14 12:57:23 -04003976/**
3977 * @brief Send a mailbox message in a synchronous manner.
3978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003979 * This routine sends a message to @a mbox and waits for a receiver to both
3980 * receive and process it. The message data may be in a buffer, in a memory
3981 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * @param mbox Address of the mailbox.
3984 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003985 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003986 * milliseconds), or one of the special values K_NO_WAIT
3987 * and K_FOREVER. Once the message has been received,
3988 * this routine waits as long as necessary for the message
3989 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003990 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003991 * @retval 0 Message sent.
3992 * @retval -ENOMSG Returned without waiting.
3993 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003994 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003995 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003996extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003997 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003998
Peter Mitsis12092702016-10-14 12:57:23 -04003999/**
4000 * @brief Send a mailbox message in an asynchronous manner.
4001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004002 * This routine sends a message to @a mbox without waiting for a receiver
4003 * to process it. The message data may be in a buffer, in a memory pool block,
4004 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
4005 * will be given when the message has been both received and completely
4006 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04004007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004008 * @param mbox Address of the mailbox.
4009 * @param tx_msg Address of the transmit message descriptor.
4010 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04004011 *
4012 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004013 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004014 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004015extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004016 struct k_sem *sem);
4017
Peter Mitsis12092702016-10-14 12:57:23 -04004018/**
4019 * @brief Receive a mailbox message.
4020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * This routine receives a message from @a mbox, then optionally retrieves
4022 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * @param mbox Address of the mailbox.
4025 * @param rx_msg Address of the receive message descriptor.
4026 * @param buffer Address of the buffer to receive data, or NULL to defer data
4027 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004028 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004029 * milliseconds), or one of the special values K_NO_WAIT
4030 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004031 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004032 * @retval 0 Message received.
4033 * @retval -ENOMSG Returned without waiting.
4034 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004035 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004036 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004037extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05004038 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004039
4040/**
4041 * @brief Retrieve mailbox message data into a buffer.
4042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004043 * This routine completes the processing of a received message by retrieving
4044 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004045 *
4046 * Alternatively, this routine can be used to dispose of a received message
4047 * without retrieving its data.
4048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004049 * @param rx_msg Address of the receive message descriptor.
4050 * @param buffer Address of the buffer to receive data, or NULL to discard
4051 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04004052 *
4053 * @return N/A
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 void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04004057
4058/**
4059 * @brief Retrieve mailbox message data into a memory pool block.
4060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004061 * This routine completes the processing of a received message by retrieving
4062 * its data into a memory pool block, then disposing of the message.
4063 * The memory pool block that results from successful retrieval must be
4064 * returned to the pool once the data has been processed, even in cases
4065 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04004066 *
4067 * Alternatively, this routine can be used to dispose of a received message
4068 * without retrieving its data. In this case there is no need to return a
4069 * memory pool block to the pool.
4070 *
4071 * This routine allocates a new memory pool block for the data only if the
4072 * data is not already in one. If a new block cannot be allocated, the routine
4073 * returns a failure code and the received message is left unchanged. This
4074 * permits the caller to reattempt data retrieval at a later time or to dispose
4075 * of the received message without retrieving its data.
4076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004077 * @param rx_msg Address of a receive message descriptor.
4078 * @param pool Address of memory pool, or NULL to discard data.
4079 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004080 * @param timeout Non-negative waiting period to wait for a memory pool block
4081 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004082 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004083 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004084 * @retval 0 Data retrieved.
4085 * @retval -ENOMEM Returned without waiting.
4086 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004087 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004088 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004089extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04004090 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05004091 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004092
Anas Nashif166f5192018-02-25 08:02:36 -06004093/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004094
4095/**
Anas Nashifce78d162018-05-24 12:43:11 -05004096 * @defgroup pipe_apis Pipe APIs
4097 * @ingroup kernel_apis
4098 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004099 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004100
Anas Nashifce78d162018-05-24 12:43:11 -05004101/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004102struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004103 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4104 size_t size; /**< Buffer size */
4105 size_t bytes_used; /**< # bytes used in buffer */
4106 size_t read_index; /**< Where in buffer to read from */
4107 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004108 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004109
4110 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004111 _wait_q_t readers; /**< Reader wait queue */
4112 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004113 } wait_q;
4114
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004115 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004116 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05004117 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004118};
4119
Anas Nashifce78d162018-05-24 12:43:11 -05004120/**
4121 * @cond INTERNAL_HIDDEN
4122 */
4123#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4124
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004125#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
4126 { \
4127 .buffer = pipe_buffer, \
4128 .size = pipe_buffer_size, \
4129 .bytes_used = 0, \
4130 .read_index = 0, \
4131 .write_index = 0, \
4132 .lock = {}, \
4133 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004134 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4135 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004136 }, \
4137 _OBJECT_TRACING_INIT \
4138 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004139 }
4140
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004141#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004142
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004143/**
Allan Stephensc98da842016-11-11 15:45:03 -05004144 * INTERNAL_HIDDEN @endcond
4145 */
4146
4147/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004148 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004150 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004151 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004152 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004154 * @param name Name of the pipe.
4155 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4156 * or zero if no ring buffer is used.
4157 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004158 *
4159 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004160 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004161#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004162 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004163 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004164 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004165 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004166
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004167/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004168 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004170 * This routine initializes a pipe object, prior to its first use.
4171 *
4172 * @param pipe Address of the pipe.
4173 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4174 * is used.
4175 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4176 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004177 *
4178 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004179 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004180 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004181void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4182
4183/**
4184 * @brief Release a pipe's allocated buffer
4185 *
4186 * If a pipe object was given a dynamically allocated buffer via
4187 * k_pipe_alloc_init(), this will free it. This function does nothing
4188 * if the buffer wasn't dynamically allocated.
4189 *
4190 * @param pipe Address of the pipe.
Anas Nashif361a84d2019-06-16 08:22:08 -04004191 * @retval 0 on success
4192 * @retval -EAGAIN nothing to cleanup
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004193 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004194 */
Anas Nashif361a84d2019-06-16 08:22:08 -04004195int k_pipe_cleanup(struct k_pipe *pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07004196
4197/**
4198 * @brief Initialize a pipe and allocate a buffer for it
4199 *
4200 * Storage for the buffer region will be allocated from the calling thread's
4201 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4202 * or userspace is enabled and the pipe object loses all references to it.
4203 *
4204 * This function should only be called on uninitialized pipe objects.
4205 *
4206 * @param pipe Address of the pipe.
4207 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4208 * buffer is used.
4209 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004210 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004211 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004212 */
4213__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004214
4215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004216 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004218 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004220 * @param pipe Address of the pipe.
4221 * @param data Address of data to write.
4222 * @param bytes_to_write Size of data (in bytes).
4223 * @param bytes_written Address of area to hold the number of bytes written.
4224 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004225 * @param timeout Non-negative waiting period to wait for the data to be written
4226 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004227 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004228 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004229 * @retval 0 At least @a min_xfer bytes of data were written.
4230 * @retval -EIO Returned without waiting; zero data bytes were written.
4231 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004232 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004233 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004234 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004235__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4236 size_t bytes_to_write, size_t *bytes_written,
4237 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004238
4239/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004240 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004242 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004244 * @param pipe Address of the pipe.
4245 * @param data Address to place the data read from pipe.
4246 * @param bytes_to_read Maximum number of data bytes to read.
4247 * @param bytes_read Address of area to hold the number of bytes read.
4248 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004249 * @param timeout Non-negative waiting period to wait for the data to be read
4250 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004251 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004252 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004253 * @retval 0 At least @a min_xfer bytes of data were read.
Anas Nashif361a84d2019-06-16 08:22:08 -04004254 * @retval -EINVAL invalid parameters supplied
Allan Stephens9ef50f42016-11-16 15:33:31 -05004255 * @retval -EIO Returned without waiting; zero data bytes were read.
4256 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004257 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004258 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004259 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004260__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4261 size_t bytes_to_read, size_t *bytes_read,
4262 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004263
4264/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004265 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004267 * This routine writes the data contained in a memory block to @a pipe.
4268 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004269 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004271 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004272 * @param block Memory block containing data to send
4273 * @param size Number of data bytes in memory block to send
4274 * @param sem Semaphore to signal upon completion (else NULL)
4275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004276 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004277 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004278 */
4279extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4280 size_t size, struct k_sem *sem);
4281
Anas Nashif166f5192018-02-25 08:02:36 -06004282/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004283
Allan Stephensc98da842016-11-11 15:45:03 -05004284/**
4285 * @cond INTERNAL_HIDDEN
4286 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004287
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004288struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004289 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004290 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004291 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004292 char *buffer;
4293 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004294 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004295
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004296 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004297 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004298};
4299
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004300#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004301 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004302 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004303 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004304 .num_blocks = slab_num_blocks, \
4305 .block_size = slab_block_size, \
4306 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004307 .free_list = NULL, \
4308 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004309 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004310 }
4311
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004312#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004313
4314
Peter Mitsis578f9112016-10-07 13:50:31 -04004315/**
Allan Stephensc98da842016-11-11 15:45:03 -05004316 * INTERNAL_HIDDEN @endcond
4317 */
4318
4319/**
4320 * @defgroup mem_slab_apis Memory Slab APIs
4321 * @ingroup kernel_apis
4322 * @{
4323 */
4324
4325/**
Allan Stephensda827222016-11-09 14:23:58 -06004326 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004327 *
Allan Stephensda827222016-11-09 14:23:58 -06004328 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004329 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004330 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4331 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004332 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004333 *
Allan Stephensda827222016-11-09 14:23:58 -06004334 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004335 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004336 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004337 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004339 * @param name Name of the memory slab.
4340 * @param slab_block_size Size of each memory block (in bytes).
4341 * @param slab_num_blocks Number memory blocks.
4342 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004343 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004344 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004345#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004346 char __noinit __aligned(WB_UP(slab_align)) \
4347 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004348 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004349 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004350 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004351
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004352/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004353 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004355 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004356 *
Allan Stephensda827222016-11-09 14:23:58 -06004357 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4358 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004359 * N-byte boundary matching a word boundary, where N is a power of 2
4360 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004361 * To ensure that each memory block is similarly aligned to this boundary,
4362 * @a slab_block_size must also be a multiple of N.
4363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004364 * @param slab Address of the memory slab.
4365 * @param buffer Pointer to buffer used for the memory blocks.
4366 * @param block_size Size of each memory block (in bytes).
4367 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004368 *
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004369 * @retval 0 on success
4370 * @retval -EINVAL invalid data supplied
4371 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004372 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004373 */
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004374extern int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004375 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004376
4377/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004378 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004380 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004382 * @param slab Address of the memory slab.
4383 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004384 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004385 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4386 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004387 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004388 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004389 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004390 * @retval -ENOMEM Returned without waiting.
4391 * @retval -EAGAIN Waiting period timed out.
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004392 * @retval -EINVAL Invalid data supplied
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004393 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004394 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004395extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004396 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004397
4398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004399 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004401 * This routine releases a previously allocated memory block back to its
4402 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004404 * @param slab Address of the memory slab.
4405 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004406 *
4407 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004408 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004409 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004410extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004411
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004413 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004415 * This routine gets the number of memory blocks that are currently
4416 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004418 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004420 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004421 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004422 */
Kumar Galacc334c72017-04-21 10:55:34 -05004423static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004424{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004425 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004426}
4427
Peter Mitsisc001aa82016-10-13 13:53:37 -04004428/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004429 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004431 * This routine gets the number of memory blocks that are currently
4432 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004434 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004436 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004437 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004438 */
Kumar Galacc334c72017-04-21 10:55:34 -05004439static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004440{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004441 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004442}
4443
Anas Nashif166f5192018-02-25 08:02:36 -06004444/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004445
4446/**
4447 * @cond INTERNAL_HIDDEN
4448 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004449
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004450struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004451 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004452 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004453};
4454
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004455/**
Allan Stephensc98da842016-11-11 15:45:03 -05004456 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004457 */
4458
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004459/**
Allan Stephensc98da842016-11-11 15:45:03 -05004460 * @addtogroup mem_pool_apis
4461 * @{
4462 */
4463
4464/**
4465 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004467 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4468 * long. The memory pool allows blocks to be repeatedly partitioned into
4469 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004470 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004471 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004472 * If the pool is to be accessed outside the module where it is defined, it
4473 * can be declared via
4474 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004475 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004477 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004478 * @param minsz Size of the smallest blocks in the pool (in bytes).
4479 * @param maxsz Size of the largest blocks in the pool (in bytes).
4480 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004481 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004482 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004483 */
Andy Ross73cb9582017-05-09 10:42:39 -07004484#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004485 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004486 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004487 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004488 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004489 .base = { \
4490 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004491 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004492 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004493 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004494 .levels = _mpool_lvls_##name, \
4495 .flags = SYS_MEM_POOL_KERNEL \
4496 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004497 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004498 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004499
Peter Mitsis937042c2016-10-13 13:18:26 -04004500/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004501 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004503 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004505 * @param pool Address of the memory pool.
4506 * @param block Pointer to block descriptor for the allocated memory.
4507 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004508 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004509 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4510 * or K_FOREVER to wait as long as necessary.
4511 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004512 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004513 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004514 * @retval -ENOMEM Returned without waiting.
4515 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004516 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004517 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004518extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004519 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004520
4521/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004522 * @brief Allocate memory from a memory pool with malloc() semantics
4523 *
4524 * Such memory must be released using k_free().
4525 *
4526 * @param pool Address of the memory pool.
4527 * @param size Amount of memory to allocate (in bytes).
4528 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004529 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004530 */
4531extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4532
4533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004534 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004536 * This routine releases a previously allocated memory block back to its
4537 * memory pool.
4538 *
4539 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004540 *
4541 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004542 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004543 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004544extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004545
4546/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004547 * @brief Free memory allocated from a memory pool.
4548 *
4549 * This routine releases a previously allocated memory block back to its
4550 * memory pool
4551 *
4552 * @param id Memory block identifier.
4553 *
4554 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004555 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004556 */
4557extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4558
4559/**
Anas Nashif166f5192018-02-25 08:02:36 -06004560 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004561 */
4562
4563/**
4564 * @defgroup heap_apis Heap Memory Pool APIs
4565 * @ingroup kernel_apis
4566 * @{
4567 */
4568
4569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004570 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004572 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004573 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004575 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004577 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004578 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004579 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004580extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004581
4582/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004583 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004584 *
4585 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004586 * returned must have been allocated from the heap memory pool or
4587 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004588 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004589 * If @a ptr is NULL, no operation is performed.
4590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004591 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004592 *
4593 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004594 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004595 */
4596extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004597
Allan Stephensc98da842016-11-11 15:45:03 -05004598/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004599 * @brief Allocate memory from heap, array style
4600 *
4601 * This routine provides traditional calloc() semantics. Memory is
4602 * allocated from the heap memory pool and zeroed.
4603 *
4604 * @param nmemb Number of elements in the requested array
4605 * @param size Size of each array element (in bytes).
4606 *
4607 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004608 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004609 */
4610extern void *k_calloc(size_t nmemb, size_t size);
4611
Anas Nashif166f5192018-02-25 08:02:36 -06004612/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004613
Benjamin Walshacc68c12017-01-29 18:57:45 -05004614/* polling API - PRIVATE */
4615
Benjamin Walshb0179862017-02-02 16:39:57 -05004616#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004617#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004618#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004619#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004620#endif
4621
Benjamin Walshacc68c12017-01-29 18:57:45 -05004622/* private - types bit positions */
4623enum _poll_types_bits {
4624 /* can be used to ignore an event */
4625 _POLL_TYPE_IGNORE,
4626
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004627 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004628 _POLL_TYPE_SIGNAL,
4629
4630 /* semaphore availability */
4631 _POLL_TYPE_SEM_AVAILABLE,
4632
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004633 /* queue/fifo/lifo data availability */
4634 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004635
4636 _POLL_NUM_TYPES
4637};
4638
Patrik Flykt4344e272019-03-08 14:19:05 -07004639#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004640
4641/* private - states bit positions */
4642enum _poll_states_bits {
4643 /* default state when creating event */
4644 _POLL_STATE_NOT_READY,
4645
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004646 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004647 _POLL_STATE_SIGNALED,
4648
4649 /* semaphore is available */
4650 _POLL_STATE_SEM_AVAILABLE,
4651
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004652 /* data is available to read on queue/fifo/lifo */
4653 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004654
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004655 /* queue/fifo/lifo wait was cancelled */
4656 _POLL_STATE_CANCELLED,
4657
Benjamin Walshacc68c12017-01-29 18:57:45 -05004658 _POLL_NUM_STATES
4659};
4660
Patrik Flykt4344e272019-03-08 14:19:05 -07004661#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004662
4663#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004664 (32 - (0 \
4665 + 8 /* tag */ \
4666 + _POLL_NUM_TYPES \
4667 + _POLL_NUM_STATES \
4668 + 1 /* modes */ \
4669 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004670
Benjamin Walshacc68c12017-01-29 18:57:45 -05004671/* end of polling API - PRIVATE */
4672
4673
4674/**
4675 * @defgroup poll_apis Async polling APIs
4676 * @ingroup kernel_apis
4677 * @{
4678 */
4679
4680/* Public polling API */
4681
4682/* public - values for k_poll_event.type bitfield */
4683#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004684#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4685#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4686#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004687#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004688
4689/* public - polling modes */
4690enum k_poll_modes {
4691 /* polling thread does not take ownership of objects when available */
4692 K_POLL_MODE_NOTIFY_ONLY = 0,
4693
4694 K_POLL_NUM_MODES
4695};
4696
4697/* public - values for k_poll_event.state bitfield */
4698#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004699#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4700#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4701#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004702#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004703#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004704
4705/* public - poll signal object */
4706struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05004707 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004708 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004709
Anas Nashife71293e2019-12-04 20:00:14 -05004710 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004711 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4712 * user resets it to 0.
4713 */
4714 unsigned int signaled;
4715
Anas Nashife71293e2019-12-04 20:00:14 -05004716 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004717 int result;
4718};
4719
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004720#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004721 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004722 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004723 .signaled = 0, \
4724 .result = 0, \
4725 }
Anas Nashife71293e2019-12-04 20:00:14 -05004726/**
4727 * @brief Poll Event
4728 *
4729 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004730struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05004731 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004732 sys_dnode_t _node;
4733
Anas Nashife71293e2019-12-04 20:00:14 -05004734 /** PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004735 struct _poller *poller;
4736
Anas Nashife71293e2019-12-04 20:00:14 -05004737 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004738 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004739
Anas Nashife71293e2019-12-04 20:00:14 -05004740 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004741 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004742
Anas Nashife71293e2019-12-04 20:00:14 -05004743 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004744 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004745
Anas Nashife71293e2019-12-04 20:00:14 -05004746 /** mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004747 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004748
Anas Nashife71293e2019-12-04 20:00:14 -05004749 /** unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004750 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004751
Anas Nashife71293e2019-12-04 20:00:14 -05004752 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004753 union {
4754 void *obj;
4755 struct k_poll_signal *signal;
4756 struct k_sem *sem;
4757 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004758 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004759 };
4760};
4761
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004762#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004763 { \
4764 .poller = NULL, \
4765 .type = event_type, \
4766 .state = K_POLL_STATE_NOT_READY, \
4767 .mode = event_mode, \
4768 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004769 { .obj = event_obj }, \
4770 }
4771
4772#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4773 event_tag) \
4774 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004775 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004776 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004777 .state = K_POLL_STATE_NOT_READY, \
4778 .mode = event_mode, \
4779 .unused = 0, \
4780 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004781 }
4782
4783/**
4784 * @brief Initialize one struct k_poll_event instance
4785 *
4786 * After this routine is called on a poll event, the event it ready to be
4787 * placed in an event array to be passed to k_poll().
4788 *
4789 * @param event The event to initialize.
4790 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4791 * values. Only values that apply to the same object being polled
4792 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4793 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004794 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004795 * @param obj Kernel object or poll signal.
4796 *
4797 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004798 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004799 */
4800
Kumar Galacc334c72017-04-21 10:55:34 -05004801extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004802 int mode, void *obj);
4803
4804/**
4805 * @brief Wait for one or many of multiple poll events to occur
4806 *
4807 * This routine allows a thread to wait concurrently for one or many of
4808 * multiple poll events to have occurred. Such events can be a kernel object
4809 * being available, like a semaphore, or a poll signal event.
4810 *
4811 * When an event notifies that a kernel object is available, the kernel object
4812 * is not "given" to the thread calling k_poll(): it merely signals the fact
4813 * that the object was available when the k_poll() call was in effect. Also,
4814 * all threads trying to acquire an object the regular way, i.e. by pending on
4815 * the object, have precedence over the thread polling on the object. This
4816 * means that the polling thread will never get the poll event on an object
4817 * until the object becomes available and its pend queue is empty. For this
4818 * reason, the k_poll() call is more effective when the objects being polled
4819 * only have one thread, the polling thread, trying to acquire them.
4820 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004821 * When k_poll() returns 0, the caller should loop on all the events that were
4822 * passed to k_poll() and check the state field for the values that were
4823 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004824 *
4825 * Before being reused for another call to k_poll(), the user has to reset the
4826 * state field to K_POLL_STATE_NOT_READY.
4827 *
Andrew Boie3772f772018-05-07 16:52:57 -07004828 * When called from user mode, a temporary memory allocation is required from
4829 * the caller's resource pool.
4830 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004831 * @param events An array of pointers to events to be polled for.
4832 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004833 * @param timeout Non-negative waiting period for an event to be ready (in
4834 * milliseconds), or one of the special values K_NO_WAIT and
4835 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004836 *
4837 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004838 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004839 * @retval -EINTR Polling has been interrupted, e.g. with
4840 * k_queue_cancel_wait(). All output events are still set and valid,
4841 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4842 * words, -EINTR status means that at least one of output events is
4843 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004844 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4845 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004846 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004847 */
4848
Andrew Boie3772f772018-05-07 16:52:57 -07004849__syscall int k_poll(struct k_poll_event *events, int num_events,
4850 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004851
4852/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004853 * @brief Initialize a poll signal object.
4854 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004855 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004856 *
4857 * @param signal A poll signal.
4858 *
4859 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004860 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004861 */
4862
Andrew Boie3772f772018-05-07 16:52:57 -07004863__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4864
4865/*
4866 * @brief Reset a poll signal object's state to unsignaled.
4867 *
4868 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004869 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004870 */
4871__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4872
Patrik Flykt4344e272019-03-08 14:19:05 -07004873static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004874{
Patrik Flykt24d71432019-03-26 19:57:45 -06004875 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004876}
4877
4878/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004879 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004880 *
4881 * @param signal A poll signal object
4882 * @param signaled An integer buffer which will be written nonzero if the
4883 * object was signaled
4884 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004885 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004886 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004887 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004888 */
4889__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4890 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004891
4892/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004893 * @brief Signal a poll signal object.
4894 *
4895 * This routine makes ready a poll signal, which is basically a poll event of
4896 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4897 * made ready to run. A @a result value can be specified.
4898 *
4899 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004900 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004901 * k_poll_signal_reset(). It thus has to be reset by the user before being
4902 * passed again to k_poll() or k_poll() will consider it being signaled, and
4903 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004904 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004905 * @note The result is stored and the 'signaled' field is set even if
4906 * this function returns an error indicating that an expiring poll was
4907 * not notified. The next k_poll() will detect the missed raise.
4908 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004909 * @param signal A poll signal.
4910 * @param result The value to store in the result field of the signal.
4911 *
4912 * @retval 0 The signal was delivered successfully.
4913 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004914 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004915 */
4916
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004917__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004918
Anas Nashif954d5502018-02-25 08:37:28 -06004919/**
4920 * @internal
4921 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004922extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004923
Anas Nashif166f5192018-02-25 08:02:36 -06004924/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004925
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004926/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004927 * @defgroup cpu_idle_apis CPU Idling APIs
4928 * @ingroup kernel_apis
4929 * @{
4930 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004931/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004932 * @brief Make the CPU idle.
4933 *
4934 * This function makes the CPU idle until an event wakes it up.
4935 *
4936 * In a regular system, the idle thread should be the only thread responsible
4937 * for making the CPU idle and triggering any type of power management.
4938 * However, in some more constrained systems, such as a single-threaded system,
4939 * the only thread would be responsible for this if needed.
4940 *
4941 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004942 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004943 */
Andrew Boie07525a32019-09-21 16:17:23 -07004944static inline void k_cpu_idle(void)
4945{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004946 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004947}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004948
4949/**
4950 * @brief Make the CPU idle in an atomic fashion.
4951 *
4952 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4953 * must be done atomically before making the CPU idle.
4954 *
4955 * @param key Interrupt locking key obtained from irq_lock().
4956 *
4957 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004958 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004959 */
Andrew Boie07525a32019-09-21 16:17:23 -07004960static inline void k_cpu_atomic_idle(unsigned int key)
4961{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004962 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004963}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004964
Anas Nashif30c3cff2019-01-22 08:18:13 -05004965/**
4966 * @}
4967 */
Anas Nashif954d5502018-02-25 08:37:28 -06004968
4969/**
4970 * @internal
4971 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004972extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004973
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004974#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004975/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004976#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004977#else
4978
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004979#if !defined(CONFIG_ASSERT_NO_FILE_INFO)
4980#define __EXCEPT_LOC() __ASSERT_PRINT("@ %s:%d\n", __FILE__, __LINE__)
4981#else
4982#define __EXCEPT_LOC()
4983#endif
4984
Andrew Boiecdb94d62017-04-18 15:22:05 -07004985/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004986 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004987 *
4988 * We won't have a real exception frame to determine the PC value when
4989 * the oops occurred, so print file and line number before we jump into
4990 * the fatal error handler.
4991 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004992#define z_except_reason(reason) do { \
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004993 __EXCEPT_LOC(); \
Andrew Boie56236372019-07-15 15:22:29 -07004994 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004995 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004996
4997#endif /* _ARCH__EXCEPT */
4998
4999/**
5000 * @brief Fatally terminate a thread
5001 *
5002 * This should be called when a thread has encountered an unrecoverable
5003 * runtime condition and needs to terminate. What this ultimately
5004 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005005 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005006 *
5007 * If this is called from ISR context, the default system fatal error handler
5008 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005009 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07005010 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005011#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005012
5013/**
5014 * @brief Fatally terminate the system
5015 *
5016 * This should be called when the Zephyr kernel has encountered an
5017 * unrecoverable runtime condition and needs to terminate. What this ultimately
5018 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005019 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005020 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07005021 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005022#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005023
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005024/*
5025 * private APIs that are utilized by one or more public APIs
5026 */
5027
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09005028/**
5029 * @internal
5030 */
5031extern void z_init_thread_base(struct _thread_base *thread_base,
5032 int priority, u32_t initial_state,
5033 unsigned int options);
5034
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005035#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06005036/**
5037 * @internal
5038 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005039extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005040#else
Anas Nashif954d5502018-02-25 08:37:28 -06005041/**
5042 * @internal
5043 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005044#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005045#endif
5046
Anas Nashif954d5502018-02-25 08:37:28 -06005047/**
5048 * @internal
5049 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005050extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06005051/**
5052 * @internal
5053 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005054extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005055
Andrew Boiedc5d9352017-06-02 12:56:47 -07005056/* arch/cpu.h may declare an architecture or platform-specific macro
5057 * for properly declaring stacks, compatible with MMU/MPU constraints if
5058 * enabled
5059 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07005060
5061/**
5062 * @brief Obtain an extern reference to a stack
5063 *
5064 * This macro properly brings the symbol of a thread stack declared
5065 * elsewhere into scope.
5066 *
5067 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005068 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07005069 */
5070#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
5071
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005072#ifdef ARCH_THREAD_STACK_DEFINE
5073#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07005074#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005075 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
5076#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
5077#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
5078#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boieb5c68102019-11-21 17:38:17 -08005079#define K_THREAD_STACK_RESERVED ((size_t)ARCH_THREAD_STACK_RESERVED)
Andrew Boie4e5c0932019-04-04 12:05:28 -07005080static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005081{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005082 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07005083}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005084#else
5085/**
5086 * @brief Declare a toplevel thread stack memory region
5087 *
5088 * This declares a region of memory suitable for use as a thread's stack.
5089 *
5090 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5091 * 'noinit' section so that it isn't zeroed at boot
5092 *
Andrew Boie507852a2017-07-25 18:47:07 -07005093 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04005094 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07005095 * inside needs to be examined, examine thread->stack_info for the associated
5096 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005097 *
5098 * It is legal to precede this definition with the 'static' keyword.
5099 *
5100 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
5101 * parameter of k_thread_create(), it may not be the same as the
5102 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
5103 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005104 * Some arches may round the size of the usable stack region up to satisfy
5105 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
5106 * size.
5107 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07005108 * @param sym Thread stack symbol name
5109 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005110 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005111 */
5112#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005113 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005114
5115/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305116 * @brief Calculate size of stacks to be allocated in a stack array
5117 *
5118 * This macro calculates the size to be allocated for the stacks
5119 * inside a stack array. It accepts the indicated "size" as a parameter
5120 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
5121 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
5122 *
5123 * @param size Size of the stack memory region
5124 * @req K-TSTACK-001
5125 */
5126#define K_THREAD_STACK_LEN(size) (size)
5127
5128/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07005129 * @brief Declare a toplevel array of thread stack memory regions
5130 *
5131 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
5132 * definition for additional details and constraints.
5133 *
5134 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5135 * 'noinit' section so that it isn't zeroed at boot
5136 *
5137 * @param sym Thread stack symbol name
5138 * @param nmemb Number of stacks to declare
5139 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005140 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005141 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07005142#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005143 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305144 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005145
5146/**
5147 * @brief Declare an embedded stack memory region
5148 *
5149 * Used for stacks embedded within other data structures. Use is highly
5150 * discouraged but in some cases necessary. For memory protection scenarios,
5151 * it is very important that any RAM preceding this member not be writable
5152 * by threads else a stack overflow will lead to silent corruption. In other
5153 * words, the containing data structure should live in RAM owned by the kernel.
5154 *
5155 * @param sym Thread stack symbol name
5156 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005157 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005158 */
5159#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005160 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005161
5162/**
5163 * @brief Return the size in bytes of a stack memory region
5164 *
5165 * Convenience macro for passing the desired stack size to k_thread_create()
5166 * since the underlying implementation may actually create something larger
5167 * (for instance a guard area).
5168 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005169 * The value returned here is not guaranteed to match the 'size' parameter
5170 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005171 *
5172 * @param sym Stack memory symbol
5173 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005174 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005175 */
5176#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5177
Andrew Boie575abc02019-03-19 10:42:24 -07005178
5179/**
5180 * @brief Indicate how much additional memory is reserved for stack objects
5181 *
5182 * Any given stack declaration may have additional memory in it for guard
5183 * areas or supervisor mode stacks. This macro indicates how much space
5184 * is reserved for this. The memory reserved may not be contiguous within
5185 * the stack object, and does not account for additional space used due to
5186 * enforce alignment.
5187 */
Andrew Boieb5c68102019-11-21 17:38:17 -08005188#define K_THREAD_STACK_RESERVED ((size_t)0U)
Andrew Boie575abc02019-03-19 10:42:24 -07005189
Andrew Boiedc5d9352017-06-02 12:56:47 -07005190/**
5191 * @brief Get a pointer to the physical stack buffer
5192 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005193 * This macro is deprecated. If a stack buffer needs to be examined, the
5194 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005195 *
5196 * @param sym Declared stack symbol name
5197 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005198 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005199 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005200static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005201{
5202 return (char *)sym;
5203}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005204
5205#endif /* _ARCH_DECLARE_STACK */
5206
Chunlin Hane9c97022017-07-07 20:29:30 +08005207/**
5208 * @defgroup mem_domain_apis Memory domain APIs
5209 * @ingroup kernel_apis
5210 * @{
5211 */
5212
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005213/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005214 * @def K_MEM_PARTITION_DEFINE
5215 * @brief Used to declare a memory partition
5216 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005217 */
5218#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5219#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5220 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005221 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005222 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005223#else
5224#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005225 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005226 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005227#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5228
Chunlin Hane9c97022017-07-07 20:29:30 +08005229/* memory partition */
5230struct k_mem_partition {
Anas Nashife71293e2019-12-04 20:00:14 -05005231 /** start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005232 uintptr_t start;
Anas Nashife71293e2019-12-04 20:00:14 -05005233 /** size of memory partition */
Andrew Boiea8248212019-11-13 12:10:56 -08005234 size_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005235#if defined(CONFIG_MEMORY_PROTECTION)
Anas Nashife71293e2019-12-04 20:00:14 -05005236 /** attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305237 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005238#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005239};
5240
Anas Nashife71293e2019-12-04 20:00:14 -05005241/**
5242 * @brief Memory Domain
5243 *
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305244 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005245struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305246#ifdef CONFIG_USERSPACE
Anas Nashife71293e2019-12-04 20:00:14 -05005247 /** partitions in the domain */
Chunlin Hane9c97022017-07-07 20:29:30 +08005248 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305249#endif /* CONFIG_USERSPACE */
Anas Nashife71293e2019-12-04 20:00:14 -05005250 /** domain q */
Chunlin Hane9c97022017-07-07 20:29:30 +08005251 sys_dlist_t mem_domain_q;
Anas Nashife71293e2019-12-04 20:00:14 -05005252 /** number of partitions in the domain */
Leandro Pereira08de6582018-02-28 14:22:57 -08005253 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005254};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305255
Chunlin Hane9c97022017-07-07 20:29:30 +08005256
5257/**
5258 * @brief Initialize a memory domain.
5259 *
5260 * Initialize a memory domain with given name and memory partitions.
5261 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005262 * See documentation for k_mem_domain_add_partition() for details about
5263 * partition constraints.
5264 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005265 * @param domain The memory domain to be initialized.
5266 * @param num_parts The number of array items of "parts" parameter.
5267 * @param parts An array of pointers to the memory partitions. Can be NULL
5268 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005269 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005270 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005271extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005272 struct k_mem_partition *parts[]);
5273/**
5274 * @brief Destroy a memory domain.
5275 *
5276 * Destroy a memory domain.
5277 *
5278 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005279 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005280 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005281extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5282
5283/**
5284 * @brief Add a memory partition into a memory domain.
5285 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005286 * Add a memory partition into a memory domain. Partitions must conform to
5287 * the following constraints:
5288 *
5289 * - Partition bounds must be within system RAM boundaries on MMU-based
5290 * systems.
5291 * - Partitions in the same memory domain may not overlap each other.
5292 * - Partitions must not be defined which expose private kernel
5293 * data structures or kernel objects.
5294 * - The starting address alignment, and the partition size must conform to
5295 * the constraints of the underlying memory management hardware, which
5296 * varies per architecture.
5297 * - Memory domain partitions are only intended to control access to memory
5298 * from user mode threads.
5299 *
5300 * Violating these constraints may lead to CPU exceptions or undefined
5301 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005302 *
5303 * @param domain The memory domain to be added a memory partition.
5304 * @param part The memory partition to be added
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_add_partition(struct k_mem_domain *domain,
5308 struct k_mem_partition *part);
5309
5310/**
5311 * @brief Remove a memory partition from a memory domain.
5312 *
5313 * Remove a memory partition from a memory domain.
5314 *
5315 * @param domain The memory domain to be removed a memory partition.
5316 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005317 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005318 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005319extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5320 struct k_mem_partition *part);
5321
5322/**
5323 * @brief Add a thread into a memory domain.
5324 *
5325 * Add a thread into a memory domain.
5326 *
5327 * @param domain The memory domain that the thread is going to be added into.
5328 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005329 *
5330 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005331 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005332extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5333 k_tid_t thread);
5334
5335/**
5336 * @brief Remove a thread from its memory domain.
5337 *
5338 * Remove a thread from its memory domain.
5339 *
5340 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005341 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005342 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005343extern void k_mem_domain_remove_thread(k_tid_t thread);
5344
Anas Nashif166f5192018-02-25 08:02:36 -06005345/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005346
Andrew Boied76ae462020-01-02 11:57:43 -08005347#ifdef CONFIG_PRINTK
Andrew Boie756f9072017-10-10 16:01:49 -07005348/**
5349 * @brief Emit a character buffer to the console device
5350 *
5351 * @param c String of characters to print
5352 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005353 *
5354 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005355 */
5356__syscall void k_str_out(char *c, size_t n);
Andrew Boied76ae462020-01-02 11:57:43 -08005357#endif
Andrew Boie756f9072017-10-10 16:01:49 -07005358
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005359/**
5360 * @brief Disable preservation of floating point context information.
5361 *
5362 * This routine informs the kernel that the specified thread
5363 * will no longer be using the floating point registers.
5364 *
5365 * @warning
5366 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005367 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005368 *
5369 * @warning
5370 * This routine should only be used to disable floating point support for
5371 * a thread that currently has such support enabled.
5372 *
5373 * @param thread ID of thread.
5374 *
5375 * @retval 0 On success.
5376 * @retval -ENOSYS If the floating point disabling is not implemented.
5377 * -EINVAL If the floating point disabling could not be performed.
5378 */
5379__syscall int k_float_disable(struct k_thread *thread);
5380
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005381#ifdef __cplusplus
5382}
5383#endif
5384
Anas Nashif73008b42020-02-06 09:14:51 -05005385#include <tracing/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005386#include <syscalls/kernel.h>
5387
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005388#endif /* !_ASMLANGUAGE */
5389
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005390#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */