blob: 196a94e78a2e60bf07b116b4dc28cefedaae6a06 [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;
Andy Ross7353c7f2020-02-06 13:39:03 -0800600 /** current syscall frame pointer */
601 void *syscall_frame;
Chunlin Hane9c97022017-07-07 20:29:30 +0800602#endif /* CONFIG_USERSPACE */
603
Andy Ross7353c7f2020-02-06 13:39:03 -0800604
Andy Ross042d8ec2017-12-09 08:37:20 -0800605#if defined(CONFIG_USE_SWITCH)
606 /* When using __switch() a few previously arch-specific items
607 * become part of the core OS
608 */
609
Patrik Flykt4344e272019-03-08 14:19:05 -0700610 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800611 int swap_retval;
612
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800613 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800614 void *switch_handle;
615#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500616 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700617 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800618
Anas Nashifce78d162018-05-24 12:43:11 -0500619 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700620 struct _thread_arch arch;
621};
622
623typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400624typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400625
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400626enum execution_context_types {
627 K_ISR = 0,
628 K_COOP_THREAD,
629 K_PREEMPT_THREAD,
630};
631
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400632/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500633 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100634 * @{
635 */
Anas Nashife71293e2019-12-04 20:00:14 -0500636
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530637typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
638 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100639
640/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530641 * @brief Iterate over all the threads in the system.
642 *
643 * This routine iterates over all the threads in the system and
644 * calls the user_cb function for each thread.
645 *
646 * @param user_cb Pointer to the user callback function.
647 * @param user_data Pointer to user data.
648 *
649 * @note CONFIG_THREAD_MONITOR must be set for this function
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100650 * to be effective.
651 * @note This API uses @ref k_spin_lock to protect the _kernel.threads
652 * list which means creation of new threads and terminations of existing
653 * threads are blocked until this API returns.
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530654 *
655 * @return N/A
656 */
657extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
658
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100659/**
660 * @brief Iterate over all the threads in the system without locking.
661 *
662 * This routine works exactly the same like @ref k_thread_foreach
663 * but unlocks interrupts when user_cb is executed.
664 *
665 * @param user_cb Pointer to the user callback function.
666 * @param user_data Pointer to user data.
667 *
668 * @note CONFIG_THREAD_MONITOR must be set for this function
669 * to be effective.
670 * @note This API uses @ref k_spin_lock only when accessing the _kernel.threads
671 * queue elements. It unlocks it during user callback function processing.
672 * If a new task is created when this @c foreach function is in progress,
673 * the added new task would not be included in the enumeration.
674 * If a task is aborted during this enumeration, there would be a race here
675 * and there is a possibility that this aborted task would be included in the
676 * enumeration.
677 * @note If the task is aborted and the memory occupied by its @c k_thread
678 * structure is reused when this @c k_thread_foreach_unlocked is in progress
679 * it might even lead to the system behave unstable.
680 * This function may never return, as it would follow some @c next task
681 * pointers treating given pointer as a pointer to the k_thread structure
682 * while it is something different right now.
683 * Do not reuse the memory that was occupied by k_thread structure of aborted
684 * task if it was aborted after this function was called in any context.
685 */
686extern void k_thread_foreach_unlocked(
687 k_thread_user_cb_t user_cb, void *user_data);
688
Anas Nashif166f5192018-02-25 08:02:36 -0600689/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100690
691/**
Allan Stephensc98da842016-11-11 15:45:03 -0500692 * @defgroup thread_apis Thread APIs
693 * @ingroup kernel_apis
694 * @{
695 */
696
Benjamin Walshed240f22017-01-22 13:05:08 -0500697#endif /* !_ASMLANGUAGE */
698
699
700/*
701 * Thread user options. May be needed by assembly code. Common part uses low
702 * bits, arch-specific use high bits.
703 */
704
Anas Nashifa541e932018-05-24 11:19:16 -0500705/**
706 * @brief system thread that must not abort
707 * @req K-THREAD-000
708 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700709#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500710
711#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500712/**
713 * @brief thread uses floating point registers
714 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700715#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500716#endif
717
Anas Nashifa541e932018-05-24 11:19:16 -0500718/**
719 * @brief user mode thread
720 *
721 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700722 * has additional restrictions
723 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700724#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700725
Anas Nashifa541e932018-05-24 11:19:16 -0500726/**
727 * @brief Inherit Permissions
728 *
729 * @details
730 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700731 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
732 * is not enabled.
733 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700734#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700735
Benjamin Walshed240f22017-01-22 13:05:08 -0500736#ifdef CONFIG_X86
737/* x86 Bitmask definitions for threads user options */
738
739#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
740/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700741#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500742#endif
743#endif
744
745/* end - thread options */
746
747#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400748/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700749 * @brief Create a thread.
750 *
751 * This routine initializes a thread, then schedules it for execution.
752 *
753 * The new thread may be scheduled for immediate execution or a delayed start.
754 * If the newly spawned thread does not have a delayed start the kernel
755 * scheduler may preempt the current thread to allow the new thread to
756 * execute.
757 *
758 * Thread options are architecture-specific, and can include K_ESSENTIAL,
759 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
760 * them using "|" (the logical OR operator).
761 *
762 * Historically, users often would use the beginning of the stack memory region
763 * to store the struct k_thread data, although corruption will occur if the
764 * stack overflows this region and stack protection features may not detect this
765 * situation.
766 *
767 * @param new_thread Pointer to uninitialized struct k_thread
768 * @param stack Pointer to the stack space.
769 * @param stack_size Stack size in bytes.
770 * @param entry Thread entry function.
771 * @param p1 1st entry point parameter.
772 * @param p2 2nd entry point parameter.
773 * @param p3 3rd entry point parameter.
774 * @param prio Thread priority.
775 * @param options Thread options.
776 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
777 *
778 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400779 *
780 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700781 */
Andrew Boie662c3452017-10-02 10:51:18 -0700782__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700783 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700784 size_t stack_size,
785 k_thread_entry_t entry,
786 void *p1, void *p2, void *p3,
787 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700788
Andrew Boie3f091b52017-08-30 14:34:14 -0700789/**
790 * @brief Drop a thread's privileges permanently to user mode
791 *
792 * @param entry Function to start executing from
793 * @param p1 1st entry point parameter
794 * @param p2 2nd entry point parameter
795 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400796 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700797 */
798extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
799 void *p1, void *p2,
800 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700801
Andrew Boied26cf2d2017-03-30 13:07:02 -0700802/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530803 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700804 *
805 * This is a convenience function. For the provided thread, grant access to
806 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700807 *
808 * The thread object must be initialized (i.e. running). The objects don't
809 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530810 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700811 *
812 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530813 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400814 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700815 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530816#define k_thread_access_grant(thread, ...) \
817 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700818
819/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700820 * @brief Assign a resource memory pool to a thread
821 *
822 * By default, threads have no resource pool assigned unless their parent
823 * thread has a resource pool, in which case it is inherited. Multiple
824 * threads may be assigned to the same memory pool.
825 *
826 * Changing a thread's resource pool will not migrate allocations from the
827 * previous pool.
828 *
829 * @param thread Target thread to assign a memory pool for resource requests,
830 * or NULL if the thread should no longer have a memory pool.
831 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400832 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700833 */
834static inline void k_thread_resource_pool_assign(struct k_thread *thread,
835 struct k_mem_pool *pool)
836{
837 thread->resource_pool = pool;
838}
839
Andrew Boieefc5fe02020-02-05 10:41:58 -0800840#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
841/**
842 * @brief Obtain stack usage information for the specified thread
843 *
844 * User threads will need to have permission on the target thread object.
845 *
846 * Some hardware may prevent inspection of a stack buffer currently in use.
847 * If this API is called from supervisor mode, on the currently running thread,
848 * on a platform which selects CONFIG_NO_UNUSED_STACK_INSPECTION, an error
849 * will be generated.
850 *
851 * @param thread Thread to inspect stack information
852 * @param unused_ptr Output parameter, filled in with the unused stack space
853 * of the target thread in bytes.
854 * @return 0 on success
855 * @return -EBADF Bad thread object (user mode only)
856 * @return -EPERM No permissions on thread object (user mode only)
857 * #return -ENOTSUP Forbidden by hardware policy
858 * @return -EINVAL Thread is uninitialized or exited (user mode only)
859 * @return -EFAULT Bad memory address for unused_ptr (user mode only)
860 */
861__syscall int k_thread_stack_space_get(const struct k_thread *thread,
862 size_t *unused_ptr);
863#endif
864
Andrew Boie92e5bd72018-04-12 17:12:15 -0700865#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
866/**
867 * @brief Assign the system heap as a thread's resource pool
868 *
869 * Similar to k_thread_resource_pool_assign(), but the thread will use
870 * the kernel heap to draw memory.
871 *
872 * Use with caution, as a malicious thread could perform DoS attacks on the
873 * kernel heap.
874 *
875 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400876 *
877 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700878 */
879void k_thread_system_pool_assign(struct k_thread *thread);
880#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
881
882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700885 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700887 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200889 * @return Zero if the requested time has elapsed or the number of milliseconds
890 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700892__syscall s32_t k_sleep(s32_t ms);
893
894/**
895 * @brief Put the current thread to sleep with microsecond resolution.
896 *
897 * This function is unlikely to work as expected without kernel tuning.
898 * In particular, because the lower bound on the duration of a sleep is
899 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
900 * to achieve the resolution desired. The implications of doing this must
901 * be understood before attempting to use k_usleep(). Use with caution.
902 *
903 * @param us Number of microseconds to sleep.
904 *
905 * @return Zero if the requested time has elapsed or the number of microseconds
906 * left to sleep, if thread was woken up by \ref k_wakeup call.
907 */
908__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400909
910/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500911 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400912 *
913 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400916 * @return N/A
917 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800918__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400919
920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926 *
927 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400928 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 */
Andrew Boie468190a2017-09-29 14:00:48 -0700930__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400931
932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500933 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * If @a thread is not currently sleeping, the routine has no effect.
938 *
939 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 *
941 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400942 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400943 */
Andrew Boie468190a2017-09-29 14:00:48 -0700944__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400945
946/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500947 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400950 *
951 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400952 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700953__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400954
955/**
Allan Stephensc98da842016-11-11 15:45:03 -0500956 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500958 * This routine permanently stops execution of @a thread. The thread is taken
959 * off all kernel queues it is part of (i.e. the ready queue, the timeout
960 * queue, or a kernel object wait queue). However, any kernel resources the
961 * thread might currently own (such as mutexes or memory blocks) are not
962 * released. It is the responsibility of the caller of this routine to ensure
963 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400966 *
967 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400968 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 */
Andrew Boie468190a2017-09-29 14:00:48 -0700970__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400971
Andrew Boie7d627c52017-08-30 11:01:56 -0700972
973/**
974 * @brief Start an inactive thread
975 *
976 * If a thread was created with K_FOREVER in the delay parameter, it will
977 * not be added to the scheduling queue until this function is called
978 * on it.
979 *
980 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400981 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700982 */
Andrew Boie468190a2017-09-29 14:00:48 -0700983__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700984
Allan Stephensc98da842016-11-11 15:45:03 -0500985/**
986 * @cond INTERNAL_HIDDEN
987 */
988
Benjamin Walshd211a522016-12-06 11:44:01 -0500989/* timeout has timed out and is not on _timeout_q anymore */
990#define _EXPIRED (-2)
991
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400992struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700993 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700994 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400995 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700996 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500997 void *init_p1;
998 void *init_p2;
999 void *init_p3;
1000 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -05001001 u32_t init_options;
1002 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -05001003 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -06001004 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001005};
1006
Andrew Boied26cf2d2017-03-30 13:07:02 -07001007#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001008 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -06001009 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001010 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001011 .init_thread = (thread), \
1012 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001013 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -07001014 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001015 .init_p1 = (void *)p1, \
1016 .init_p2 = (void *)p2, \
1017 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001018 .init_prio = (prio), \
1019 .init_options = (options), \
1020 .init_delay = (delay), \
1021 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -06001022 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001023 }
1024
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001025/**
Allan Stephensc98da842016-11-11 15:45:03 -05001026 * INTERNAL_HIDDEN @endcond
1027 */
1028
1029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @brief Statically define and initialize a thread.
1031 *
1032 * The thread may be scheduled for immediate execution or a delayed start.
1033 *
1034 * Thread options are architecture-specific, and can include K_ESSENTIAL,
1035 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
1036 * them using "|" (the logical OR operator).
1037 *
1038 * The ID of the thread can be accessed using:
1039 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001040 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 *
1042 * @param name Name of the thread.
1043 * @param stack_size Stack size in bytes.
1044 * @param entry Thread entry function.
1045 * @param p1 1st entry point parameter.
1046 * @param p2 2nd entry point parameter.
1047 * @param p3 3rd entry point parameter.
1048 * @param prio Thread priority.
1049 * @param options Thread options.
1050 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001051 *
Anas Nashif47420d02018-05-24 14:20:56 -04001052 * @req K-THREAD-010
1053 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001054 * @internal It has been observed that the x86 compiler by default aligns
1055 * these _static_thread_data structures to 32-byte boundaries, thereby
1056 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -04001057 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001058 */
Allan Stephens6cfe1322016-10-26 10:16:51 -05001059#define K_THREAD_DEFINE(name, stack_size, \
1060 entry, p1, p2, p3, \
1061 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -07001062 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001063 struct k_thread _k_thread_obj_##name; \
1064 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -07001065 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
1066 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001067 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -06001068 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001069 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001070
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001071/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001072 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * @param thread ID of thread whose priority is needed.
1077 *
1078 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001079 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001081__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082
1083/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001084 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001086 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001087 *
1088 * Rescheduling can occur immediately depending on the priority @a thread is
1089 * set to:
1090 *
1091 * - If its priority is raised above the priority of the caller of this
1092 * function, and the caller is preemptible, @a thread will be scheduled in.
1093 *
1094 * - If the caller operates on itself, it lowers its priority below that of
1095 * other threads in the system, and the caller is preemptible, the thread of
1096 * highest priority will be scheduled in.
1097 *
1098 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1099 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1100 * highest priority.
1101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 * @param prio New priority.
1104 *
1105 * @warning Changing the priority of a thread currently involved in mutex
1106 * priority inheritance may result in undefined behavior.
1107 *
1108 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001109 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001110 */
Andrew Boie468190a2017-09-29 14:00:48 -07001111__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001112
Andy Ross4a2e50f2018-05-15 11:06:25 -07001113
1114#ifdef CONFIG_SCHED_DEADLINE
1115/**
1116 * @brief Set deadline expiration time for scheduler
1117 *
1118 * This sets the "deadline" expiration as a time delta from the
1119 * current time, in the same units used by k_cycle_get_32(). The
1120 * scheduler (when deadline scheduling is enabled) will choose the
1121 * next expiring thread when selecting between threads at the same
1122 * static priority. Threads at different priorities will be scheduled
1123 * according to their static priority.
1124 *
1125 * @note Deadlines that are negative (i.e. in the past) are still seen
1126 * as higher priority than others, even if the thread has "finished"
1127 * its work. If you don't want it scheduled anymore, you have to
1128 * reset the deadline into the future, block/pend the thread, or
1129 * modify its priority with k_thread_priority_set().
1130 *
1131 * @note Despite the API naming, the scheduler makes no guarantees the
1132 * the thread WILL be scheduled within that deadline, nor does it take
1133 * extra metadata (like e.g. the "runtime" and "period" parameters in
1134 * Linux sched_setattr()) that allows the kernel to validate the
1135 * scheduling for achievability. Such features could be implemented
1136 * above this call, which is simply input to the priority selection
1137 * logic.
1138 *
Anas Nashif240c5162019-06-10 12:25:50 -04001139 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001140 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001141 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1142 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001143 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001144 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001145 * @param thread A thread on which to set the deadline
1146 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001147 *
1148 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001149 */
1150__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1151#endif
1152
Andy Rossab46b1b2019-01-30 15:00:42 -08001153#ifdef CONFIG_SCHED_CPU_MASK
1154/**
1155 * @brief Sets all CPU enable masks to zero
1156 *
1157 * After this returns, the thread will no longer be schedulable on any
1158 * CPUs. The thread must not be currently runnable.
1159 *
Anas Nashif240c5162019-06-10 12:25:50 -04001160 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001161 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001162 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1163 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001164 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001165 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001166 * @param thread Thread to operate upon
1167 * @return Zero on success, otherwise error code
1168 */
1169int k_thread_cpu_mask_clear(k_tid_t thread);
1170
1171/**
1172 * @brief Sets all CPU enable masks to one
1173 *
1174 * After this returns, the thread will be schedulable on any CPU. The
1175 * thread must not be currently runnable.
1176 *
Anas Nashif240c5162019-06-10 12:25:50 -04001177 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001178 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001179 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1180 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001181 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001182 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001183 * @param thread Thread to operate upon
1184 * @return Zero on success, otherwise error code
1185 */
1186int k_thread_cpu_mask_enable_all(k_tid_t thread);
1187
1188/**
1189 * @brief Enable thread to run on specified CPU
1190 *
1191 * The thread must not be currently runnable.
1192 *
Anas Nashif240c5162019-06-10 12:25:50 -04001193 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001194 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001195 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1196 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001197 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001198 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001199 * @param thread Thread to operate upon
1200 * @param cpu CPU index
1201 * @return Zero on success, otherwise error code
1202 */
1203int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1204
1205/**
1206 * @brief Prevent thread to run on specified CPU
1207 *
1208 * The thread must not be currently runnable.
1209 *
Anas Nashif240c5162019-06-10 12:25:50 -04001210 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001211 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001212 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1213 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001214 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001215 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001216 * @param thread Thread to operate upon
1217 * @param cpu CPU index
1218 * @return Zero on success, otherwise error code
1219 */
1220int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1221#endif
1222
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
Andy Ross50d09422019-11-19 11:20:07 -08001226 * This routine prevents the kernel scheduler from making @a thread
1227 * the current thread. All other internal operations on @a thread are
1228 * still performed; for example, kernel objects it is waiting on are
1229 * still handed to it. Note that any existing timeouts
1230 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1231 * will be canceled. On resume, the thread will begin running
1232 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * If @a thread is already suspended, the routine has no effect.
1235 *
1236 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001237 *
1238 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001239 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240 */
Andrew Boie468190a2017-09-29 14:00:48 -07001241__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242
1243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001246 * This routine allows the kernel scheduler to make @a thread the current
1247 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001249 * If @a thread is not currently suspended, the routine has no effect.
1250 *
1251 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001252 *
1253 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001254 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001255 */
Andrew Boie468190a2017-09-29 14:00:48 -07001256__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001257
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001259 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001261 * This routine specifies how the scheduler will perform time slicing of
1262 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001264 * To enable time slicing, @a slice must be non-zero. The scheduler
1265 * ensures that no thread runs for more than the specified time limit
1266 * before other threads of that priority are given a chance to execute.
1267 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001268 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001270 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001271 * execute. Once the scheduler selects a thread for execution, there is no
1272 * minimum guaranteed time the thread will execute before threads of greater or
1273 * equal priority are scheduled.
1274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001275 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276 * for execution, this routine has no effect; the thread is immediately
1277 * rescheduled after the slice period expires.
1278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * To disable timeslicing, set both @a slice and @a prio to zero.
1280 *
1281 * @param slice Maximum time slice length (in milliseconds).
1282 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283 *
1284 * @return N/A
1285 */
Kumar Galacc334c72017-04-21 10:55:34 -05001286extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287
Anas Nashif166f5192018-02-25 08:02:36 -06001288/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001289
1290/**
1291 * @addtogroup isr_apis
1292 * @{
1293 */
1294
1295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 *
Allan Stephensc98da842016-11-11 15:45:03 -05001298 * This routine allows the caller to customize its actions, depending on
1299 * whether it is a thread or an ISR.
1300 *
1301 * @note Can be called by ISRs.
1302 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001303 * @return false if invoked by a thread.
1304 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001306extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001307
Benjamin Walsh445830d2016-11-10 15:54:27 -05001308/**
1309 * @brief Determine if code is running in a preemptible thread.
1310 *
Allan Stephensc98da842016-11-11 15:45:03 -05001311 * This routine allows the caller to customize its actions, depending on
1312 * whether it can be preempted by another thread. The routine returns a 'true'
1313 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001314 *
Allan Stephensc98da842016-11-11 15:45:03 -05001315 * - The code is running in a thread, not at ISR.
1316 * - The thread's priority is in the preemptible range.
1317 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001318 *
Allan Stephensc98da842016-11-11 15:45:03 -05001319 * @note Can be called by ISRs.
1320 *
1321 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001322 * @return Non-zero if invoked by a preemptible thread.
1323 */
Andrew Boie468190a2017-09-29 14:00:48 -07001324__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001325
Allan Stephensc98da842016-11-11 15:45:03 -05001326/**
Peter Bigot74ef3952019-12-23 11:48:43 -06001327 * @brief Test whether startup is in the before-main-task phase.
1328 *
1329 * This routine allows the caller to customize its actions, depending on
1330 * whether it being invoked before the kernel is fully active.
1331 *
1332 * @note Can be called by ISRs.
1333 *
1334 * @return true if invoked before post-kernel initialization
1335 * @return false if invoked during/after post-kernel initialization
1336 */
1337static inline bool k_is_pre_kernel(void)
1338{
1339 extern bool z_sys_post_kernel; /* in init.c */
1340
1341 return !z_sys_post_kernel;
1342}
1343
1344/**
Anas Nashif166f5192018-02-25 08:02:36 -06001345 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001346 */
1347
1348/**
1349 * @addtogroup thread_apis
1350 * @{
1351 */
1352
1353/**
1354 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001355 *
Allan Stephensc98da842016-11-11 15:45:03 -05001356 * This routine prevents the current thread from being preempted by another
1357 * thread by instructing the scheduler to treat it as a cooperative thread.
1358 * If the thread subsequently performs an operation that makes it unready,
1359 * it will be context switched out in the normal manner. When the thread
1360 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001361 *
Allan Stephensc98da842016-11-11 15:45:03 -05001362 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001363 *
Allan Stephensc98da842016-11-11 15:45:03 -05001364 * @note k_sched_lock() and k_sched_unlock() should normally be used
1365 * when the operation being performed can be safely interrupted by ISRs.
1366 * However, if the amount of processing involved is very small, better
1367 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001368 *
1369 * @return N/A
1370 */
1371extern void k_sched_lock(void);
1372
Allan Stephensc98da842016-11-11 15:45:03 -05001373/**
1374 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001375 *
Allan Stephensc98da842016-11-11 15:45:03 -05001376 * This routine reverses the effect of a previous call to k_sched_lock().
1377 * A thread must call the routine once for each time it called k_sched_lock()
1378 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001379 *
1380 * @return N/A
1381 */
1382extern void k_sched_unlock(void);
1383
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001385 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001387 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * Custom data is not used by the kernel itself, and is freely available
1390 * for a thread to use as it sees fit. It can be used as a framework
1391 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001394 *
1395 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001396 *
1397 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001398 */
Andrew Boie468190a2017-09-29 14:00:48 -07001399__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001400
1401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001402 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001404 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001407 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408 */
Andrew Boie468190a2017-09-29 14:00:48 -07001409__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410
1411/**
Anas Nashif57554052018-03-03 02:31:05 -06001412 * @brief Set current thread name
1413 *
1414 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1415 * tracing and debugging.
1416 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001417 * @param thread_id Thread to set name, or NULL to set the current thread
1418 * @param value Name string
1419 * @retval 0 on success
1420 * @retval -EFAULT Memory access error with supplied string
1421 * @retval -ENOSYS Thread name configuration option not enabled
1422 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001423 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001424__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001425
1426/**
1427 * @brief Get thread name
1428 *
1429 * Get the name of a thread
1430 *
1431 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001432 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001433 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001434const char *k_thread_name_get(k_tid_t thread_id);
1435
1436/**
1437 * @brief Copy the thread name into a supplied buffer
1438 *
1439 * @param thread_id Thread to obtain name information
1440 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001441 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001442 * @retval -ENOSPC Destination buffer too small
1443 * @retval -EFAULT Memory access error
1444 * @retval -ENOSYS Thread name feature not enabled
1445 * @retval 0 Success
1446 */
1447__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1448 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001449
1450/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001451 * @brief Get thread state string
1452 *
1453 * Get the human friendly thread state string
1454 *
1455 * @param thread_id Thread ID
1456 * @retval Thread state string, empty if no state flag is set
1457 */
1458const char *k_thread_state_str(k_tid_t thread_id);
1459
1460/**
Andy Rosscfe62032018-09-29 07:34:55 -07001461 * @}
1462 */
1463
1464/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001465 * @addtogroup clock_apis
1466 * @{
1467 */
1468
1469/**
1470 * @brief Generate null timeout delay.
1471 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001472 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001473 * not to wait if the requested operation cannot be performed immediately.
1474 *
1475 * @return Timeout delay value.
1476 */
1477#define K_NO_WAIT 0
1478
1479/**
1480 * @brief Generate timeout delay from milliseconds.
1481 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001482 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001483 * to wait up to @a ms milliseconds to perform the requested operation.
1484 *
1485 * @param ms Duration in milliseconds.
1486 *
1487 * @return Timeout delay value.
1488 */
Johan Hedberg14471692016-11-13 10:52:15 +02001489#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001490
1491/**
1492 * @brief Generate timeout delay from seconds.
1493 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001494 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001495 * to wait up to @a s seconds to perform the requested operation.
1496 *
1497 * @param s Duration in seconds.
1498 *
1499 * @return Timeout delay value.
1500 */
Johan Hedberg14471692016-11-13 10:52:15 +02001501#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001502
1503/**
1504 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001505
1506 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001507 * to wait up to @a m minutes to perform the requested operation.
1508 *
1509 * @param m Duration in minutes.
1510 *
1511 * @return Timeout delay value.
1512 */
Johan Hedberg14471692016-11-13 10:52:15 +02001513#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001514
1515/**
1516 * @brief Generate timeout delay from hours.
1517 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001518 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001519 * to wait up to @a h hours to perform the requested operation.
1520 *
1521 * @param h Duration in hours.
1522 *
1523 * @return Timeout delay value.
1524 */
Johan Hedberg14471692016-11-13 10:52:15 +02001525#define K_HOURS(h) K_MINUTES((h) * 60)
1526
Allan Stephensc98da842016-11-11 15:45:03 -05001527/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001528 * @brief Generate infinite timeout delay.
1529 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001530 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001531 * to wait as long as necessary to perform the requested operation.
1532 *
1533 * @return Timeout delay value.
1534 */
1535#define K_FOREVER (-1)
1536
1537/**
Anas Nashif166f5192018-02-25 08:02:36 -06001538 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001539 */
1540
1541/**
Allan Stephensc98da842016-11-11 15:45:03 -05001542 * @cond INTERNAL_HIDDEN
1543 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001544
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545struct k_timer {
1546 /*
1547 * _timeout structure must be first here if we want to use
1548 * dynamic timer allocation. timeout.node is used in the double-linked
1549 * list of free timers
1550 */
1551 struct _timeout timeout;
1552
Allan Stephens45bfa372016-10-12 12:39:42 -05001553 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001554 _wait_q_t wait_q;
1555
1556 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001557 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001558
1559 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001560 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561
1562 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001563 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001564
Allan Stephens45bfa372016-10-12 12:39:42 -05001565 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001566 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001567
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001568 /* user-specific data, also used to support legacy features */
1569 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001571 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001572 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001573};
1574
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001575#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001576 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001577 .timeout = { \
1578 .node = {},\
1579 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001580 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001581 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001582 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001583 .expiry_fn = expiry, \
1584 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001585 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001586 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001587 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001588 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001589 }
1590
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001591#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001592
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001593/**
Allan Stephensc98da842016-11-11 15:45:03 -05001594 * INTERNAL_HIDDEN @endcond
1595 */
1596
1597/**
1598 * @defgroup timer_apis Timer APIs
1599 * @ingroup kernel_apis
1600 * @{
1601 */
1602
1603/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001604 * @typedef k_timer_expiry_t
1605 * @brief Timer expiry function type.
1606 *
1607 * A timer's expiry function is executed by the system clock interrupt handler
1608 * each time the timer expires. The expiry function is optional, and is only
1609 * invoked if the timer has been initialized with one.
1610 *
1611 * @param timer Address of timer.
1612 *
1613 * @return N/A
1614 */
1615typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1616
1617/**
1618 * @typedef k_timer_stop_t
1619 * @brief Timer stop function type.
1620 *
1621 * A timer's stop function is executed if the timer is stopped prematurely.
1622 * The function runs in the context of the thread that stops the timer.
1623 * The stop function is optional, and is only invoked if the timer has been
1624 * initialized with one.
1625 *
1626 * @param timer Address of timer.
1627 *
1628 * @return N/A
1629 */
1630typedef void (*k_timer_stop_t)(struct k_timer *timer);
1631
1632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001635 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001636 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001637 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001638 *
1639 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001640 * @param expiry_fn Function to invoke each time the timer expires.
1641 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001642 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001643#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001644 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001645 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001646
Allan Stephens45bfa372016-10-12 12:39:42 -05001647/**
1648 * @brief Initialize a timer.
1649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001651 *
1652 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @param expiry_fn Function to invoke each time the timer expires.
1654 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001655 *
1656 * @return N/A
1657 */
1658extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001659 k_timer_expiry_t expiry_fn,
1660 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001661
Allan Stephens45bfa372016-10-12 12:39:42 -05001662/**
1663 * @brief Start a timer.
1664 *
1665 * This routine starts a timer, and resets its status to zero. The timer
1666 * begins counting down using the specified duration and period values.
1667 *
1668 * Attempting to start a timer that is already running is permitted.
1669 * The timer's status is reset to zero and the timer begins counting down
1670 * using the new duration and period values.
1671 *
1672 * @param timer Address of timer.
1673 * @param duration Initial timer duration (in milliseconds).
1674 * @param period Timer period (in milliseconds).
1675 *
1676 * @return N/A
1677 */
Andrew Boiea354d492017-09-29 16:22:28 -07001678__syscall void k_timer_start(struct k_timer *timer,
1679 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001680
1681/**
1682 * @brief Stop a timer.
1683 *
1684 * This routine stops a running timer prematurely. The timer's stop function,
1685 * if one exists, is invoked by the caller.
1686 *
1687 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001689 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001690 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1691 * if @a k_timer_stop is to be called from ISRs.
1692 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001693 * @param timer Address of timer.
1694 *
1695 * @return N/A
1696 */
Andrew Boiea354d492017-09-29 16:22:28 -07001697__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001698
1699/**
1700 * @brief Read timer status.
1701 *
1702 * This routine reads the timer's status, which indicates the number of times
1703 * it has expired since its status was last read.
1704 *
1705 * Calling this routine resets the timer's status to zero.
1706 *
1707 * @param timer Address of timer.
1708 *
1709 * @return Timer status.
1710 */
Andrew Boiea354d492017-09-29 16:22:28 -07001711__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001712
1713/**
1714 * @brief Synchronize thread to timer expiration.
1715 *
1716 * This routine blocks the calling thread until the timer's status is non-zero
1717 * (indicating that it has expired at least once since it was last examined)
1718 * or the timer is stopped. If the timer status is already non-zero,
1719 * or the timer is already stopped, the caller continues without waiting.
1720 *
1721 * Calling this routine resets the timer's status to zero.
1722 *
1723 * This routine must not be used by interrupt handlers, since they are not
1724 * allowed to block.
1725 *
1726 * @param timer Address of timer.
1727 *
1728 * @return Timer status.
1729 */
Andrew Boiea354d492017-09-29 16:22:28 -07001730__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001731
Andy Ross52e444b2018-09-28 09:06:37 -07001732extern s32_t z_timeout_remaining(struct _timeout *timeout);
1733
Allan Stephens45bfa372016-10-12 12:39:42 -05001734/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001736 *
1737 * This routine computes the (approximate) time remaining before a running
1738 * timer next expires. If the timer is not running, it returns zero.
1739 *
1740 * @param timer Address of timer.
1741 *
1742 * @return Remaining time (in milliseconds).
1743 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001744__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001745
Patrik Flykt4344e272019-03-08 14:19:05 -07001746static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001747{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001748 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001749 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001750}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001751
Allan Stephensc98da842016-11-11 15:45:03 -05001752/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001753 * @brief Associate user-specific data with a timer.
1754 *
1755 * This routine records the @a user_data with the @a timer, to be retrieved
1756 * later.
1757 *
1758 * It can be used e.g. in a timer handler shared across multiple subsystems to
1759 * retrieve data specific to the subsystem this timer is associated with.
1760 *
1761 * @param timer Address of timer.
1762 * @param user_data User data to associate with the timer.
1763 *
1764 * @return N/A
1765 */
Andrew Boiea354d492017-09-29 16:22:28 -07001766__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1767
Anas Nashif954d5502018-02-25 08:37:28 -06001768/**
1769 * @internal
1770 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001771static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001772 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001773{
1774 timer->user_data = user_data;
1775}
1776
1777/**
1778 * @brief Retrieve the user-specific data from a timer.
1779 *
1780 * @param timer Address of timer.
1781 *
1782 * @return The user data.
1783 */
Andrew Boiea354d492017-09-29 16:22:28 -07001784__syscall void *k_timer_user_data_get(struct k_timer *timer);
1785
Patrik Flykt4344e272019-03-08 14:19:05 -07001786static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001787{
1788 return timer->user_data;
1789}
1790
Anas Nashif166f5192018-02-25 08:02:36 -06001791/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001792
Allan Stephensc98da842016-11-11 15:45:03 -05001793/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001794 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001795 * @{
1796 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001797
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * This routine returns the elapsed time since the system booted,
1802 * in milliseconds.
1803 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001804 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001805 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001806 * While this function returns time in milliseconds, it does
1807 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001808 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001809 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001810 *
1811 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001812 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001813__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001814
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001815/**
1816 * @brief Enable clock always on in tickless kernel
1817 *
Andy Ross1db9f182019-06-25 10:09:45 -07001818 * Deprecated. This does nothing (it was always just a hint). This
1819 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1820 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001821 *
1822 * @retval prev_status Previous status of always on flag
1823 */
Andy Ross1db9f182019-06-25 10:09:45 -07001824/* LCOV_EXCL_START */
1825__deprecated static inline int k_enable_sys_clock_always_on(void)
1826{
1827 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1828 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1829
1830 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1831}
1832/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001833
1834/**
1835 * @brief Disable clock always on in tickless kernel
1836 *
Andy Ross1db9f182019-06-25 10:09:45 -07001837 * Deprecated. This does nothing (it was always just a hint). This
1838 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1839 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001840 */
Andy Ross1db9f182019-06-25 10:09:45 -07001841/* LCOV_EXCL_START */
1842__deprecated static inline void k_disable_sys_clock_always_on(void)
1843{
1844 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1845 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1846}
1847/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001848
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001851 *
Peter Bigota6067a32019-08-28 08:19:26 -05001852 * This routine returns the lower 32 bits of the system uptime in
1853 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001854 *
Peter Bigota6067a32019-08-28 08:19:26 -05001855 * Because correct conversion requires full precision of the system
1856 * clock there is no benefit to using this over k_uptime_get() unless
1857 * you know the application will never run long enough for the system
1858 * clock to approach 2^32 ticks. Calls to this function may involve
1859 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001860 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001861 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001862 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001863 * While this function returns time in milliseconds, it does
1864 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001865 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001866 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001867 *
Peter Bigota6067a32019-08-28 08:19:26 -05001868 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001869 */
Peter Bigota6067a32019-08-28 08:19:26 -05001870static inline u32_t k_uptime_get_32(void)
1871{
1872 return (u32_t)k_uptime_get();
1873}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001874
1875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001876 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * This routine computes the elapsed time between the current system uptime
1879 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001881 * @param reftime Pointer to a reference time, which is updated to the current
1882 * uptime upon return.
1883 *
1884 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001885 */
Andy Ross987c0e52018-09-27 16:50:00 -07001886static inline s64_t k_uptime_delta(s64_t *reftime)
1887{
1888 s64_t uptime, delta;
1889
1890 uptime = k_uptime_get();
1891 delta = uptime - *reftime;
1892 *reftime = uptime;
1893
1894 return delta;
1895}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001896
1897/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001898 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001900 * This routine computes the elapsed time between the current system uptime
1901 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001903 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1904 * need for interrupt locking and 64-bit math. However, the 32-bit result
1905 * cannot hold an elapsed time larger than approximately 50 days, so the
1906 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001908 * @param reftime Pointer to a reference time, which is updated to the current
1909 * uptime upon return.
1910 *
1911 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001912 */
Andy Ross987c0e52018-09-27 16:50:00 -07001913static inline u32_t k_uptime_delta_32(s64_t *reftime)
1914{
1915 return (u32_t)k_uptime_delta(reftime);
1916}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001917
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001921 * This routine returns the current time, as measured by the system's hardware
1922 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001926static inline u32_t k_cycle_get_32(void)
1927{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001928 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001929}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931/**
Anas Nashif166f5192018-02-25 08:02:36 -06001932 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001933 */
1934
Allan Stephensc98da842016-11-11 15:45:03 -05001935/**
1936 * @cond INTERNAL_HIDDEN
1937 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001939struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001940 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001941 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001942 union {
1943 _wait_q_t wait_q;
1944
1945 _POLL_EVENT;
1946 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001947
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001948 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001949 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001950};
1951
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001952#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001953 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001954 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001955 .lock = { }, \
1956 { \
1957 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1958 _POLL_EVENT_OBJ_INIT(obj) \
1959 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001960 _OBJECT_TRACING_INIT \
1961 }
1962
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001963#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001964
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001965extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1966
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001967/**
1968 * INTERNAL_HIDDEN @endcond
1969 */
1970
1971/**
1972 * @defgroup queue_apis Queue APIs
1973 * @ingroup kernel_apis
1974 * @{
1975 */
1976
1977/**
1978 * @brief Initialize a queue.
1979 *
1980 * This routine initializes a queue object, prior to its first use.
1981 *
1982 * @param queue Address of the queue.
1983 *
1984 * @return N/A
1985 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001986__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001987
1988/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001989 * @brief Cancel waiting on a queue.
1990 *
1991 * This routine causes first thread pending on @a queue, if any, to
1992 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001993 * If the queue is being waited on by k_poll(), it will return with
1994 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1995 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001996 *
1997 * @note Can be called by ISRs.
1998 *
1999 * @param queue Address of the queue.
2000 *
2001 * @return N/A
2002 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002003__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002004
2005/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002006 * @brief Append an element to the end of a queue.
2007 *
2008 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002009 * aligned on a word boundary, and the first word of the item is reserved
2010 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002011 *
2012 * @note Can be called by ISRs.
2013 *
2014 * @param queue Address of the queue.
2015 * @param data Address of the data item.
2016 *
2017 * @return N/A
2018 */
2019extern void k_queue_append(struct k_queue *queue, void *data);
2020
2021/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002022 * @brief Append an element to a queue.
2023 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002024 * This routine appends a data item to @a queue. There is an implicit memory
2025 * allocation to create an additional temporary bookkeeping data structure from
2026 * the calling thread's resource pool, which is automatically freed when the
2027 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002028 *
2029 * @note Can be called by ISRs.
2030 *
2031 * @param queue Address of the queue.
2032 * @param data Address of the data item.
2033 *
2034 * @retval 0 on success
2035 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2036 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302037__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002038
2039/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002040 * @brief Prepend an element to a queue.
2041 *
2042 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002043 * aligned on a word boundary, and the first word of the item is reserved
2044 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002045 *
2046 * @note Can be called by ISRs.
2047 *
2048 * @param queue Address of the queue.
2049 * @param data Address of the data item.
2050 *
2051 * @return N/A
2052 */
2053extern void k_queue_prepend(struct k_queue *queue, void *data);
2054
2055/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002056 * @brief Prepend an element to a queue.
2057 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002058 * This routine prepends a data item to @a queue. There is an implicit memory
2059 * allocation to create an additional temporary bookkeeping data structure from
2060 * the calling thread's resource pool, which is automatically freed when the
2061 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002062 *
2063 * @note Can be called by ISRs.
2064 *
2065 * @param queue Address of the queue.
2066 * @param data Address of the data item.
2067 *
2068 * @retval 0 on success
2069 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2070 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302071__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002072
2073/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002074 * @brief Inserts an element to a queue.
2075 *
2076 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002077 * data item must be aligned on a word boundary, and the first word of
2078 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002079 *
2080 * @note Can be called by ISRs.
2081 *
2082 * @param queue Address of the queue.
2083 * @param prev Address of the previous data item.
2084 * @param data Address of the data item.
2085 *
2086 * @return N/A
2087 */
2088extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2089
2090/**
2091 * @brief Atomically append a list of elements to a queue.
2092 *
2093 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002094 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002095 * in each data item pointing to the next data item; the list must be
2096 * NULL-terminated.
2097 *
2098 * @note Can be called by ISRs.
2099 *
2100 * @param queue Address of the queue.
2101 * @param head Pointer to first node in singly-linked list.
2102 * @param tail Pointer to last node in singly-linked list.
2103 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002104 * @retval 0 on success
2105 * @retval -EINVAL on invalid supplied data
2106 *
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002107 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002108extern int k_queue_append_list(struct k_queue *queue, void *head, void *tail);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002109
2110/**
2111 * @brief Atomically add a list of elements to a queue.
2112 *
2113 * This routine adds a list of data items to @a queue in one operation.
2114 * The data items must be in a singly-linked list implemented using a
2115 * sys_slist_t object. Upon completion, the original list is empty.
2116 *
2117 * @note Can be called by ISRs.
2118 *
2119 * @param queue Address of the queue.
2120 * @param list Pointer to sys_slist_t object.
2121 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002122 * @retval 0 on success
2123 * @retval -EINVAL on invalid data
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002124 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002125extern int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002126
2127/**
2128 * @brief Get an element from a queue.
2129 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002130 * This routine removes first data item from @a queue. The first word of the
2131 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002132 *
2133 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2134 *
2135 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002136 * @param timeout Non-negative waiting period to obtain a data item (in
2137 * milliseconds), or one of the special values K_NO_WAIT and
2138 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002139 *
2140 * @return Address of the data item if successful; NULL if returned
2141 * without waiting, or waiting period timed out.
2142 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002143__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002144
2145/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002146 * @brief Remove an element from a queue.
2147 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002148 * This routine removes data item from @a queue. The first word of the
2149 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002150 * rely on sys_slist_find_and_remove which is not a constant time operation.
2151 *
2152 * @note Can be called by ISRs
2153 *
2154 * @param queue Address of the queue.
2155 * @param data Address of the data item.
2156 *
2157 * @return true if data item was removed
2158 */
2159static inline bool k_queue_remove(struct k_queue *queue, void *data)
2160{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002161 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002162}
2163
2164/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002165 * @brief Append an element to a queue only if it's not present already.
2166 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002167 * This routine appends data item to @a queue. The first word of the data
2168 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002169 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2170 *
2171 * @note Can be called by ISRs
2172 *
2173 * @param queue Address of the queue.
2174 * @param data Address of the data item.
2175 *
2176 * @return true if data item was added, false if not
2177 */
2178static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2179{
2180 sys_sfnode_t *test;
2181
2182 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2183 if (test == (sys_sfnode_t *) data) {
2184 return false;
2185 }
2186 }
2187
2188 k_queue_append(queue, data);
2189 return true;
2190}
2191
2192/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002193 * @brief Query a queue to see if it has data available.
2194 *
2195 * Note that the data might be already gone by the time this function returns
2196 * if other threads are also trying to read from the queue.
2197 *
2198 * @note Can be called by ISRs.
2199 *
2200 * @param queue Address of the queue.
2201 *
2202 * @return Non-zero if the queue is empty.
2203 * @return 0 if data is available.
2204 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002205__syscall int k_queue_is_empty(struct k_queue *queue);
2206
Patrik Flykt4344e272019-03-08 14:19:05 -07002207static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002208{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002209 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002210}
2211
2212/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002213 * @brief Peek element at the head of queue.
2214 *
2215 * Return element from the head of queue without removing it.
2216 *
2217 * @param queue Address of the queue.
2218 *
2219 * @return Head element, or NULL if queue is empty.
2220 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002221__syscall void *k_queue_peek_head(struct k_queue *queue);
2222
Patrik Flykt4344e272019-03-08 14:19:05 -07002223static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002224{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002225 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002226}
2227
2228/**
2229 * @brief Peek element at the tail of queue.
2230 *
2231 * Return element from the tail of queue without removing it.
2232 *
2233 * @param queue Address of the queue.
2234 *
2235 * @return Tail element, or NULL if queue is empty.
2236 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002237__syscall void *k_queue_peek_tail(struct k_queue *queue);
2238
Patrik Flykt4344e272019-03-08 14:19:05 -07002239static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002240{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002241 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002242}
2243
2244/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002245 * @brief Statically define and initialize a queue.
2246 *
2247 * The queue can be accessed outside the module where it is defined using:
2248 *
2249 * @code extern struct k_queue <name>; @endcode
2250 *
2251 * @param name Name of the queue.
2252 */
2253#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002254 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002255 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002256
Anas Nashif166f5192018-02-25 08:02:36 -06002257/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002258
Wentong Wu5611e922019-06-20 23:51:27 +08002259#ifdef CONFIG_USERSPACE
2260/**
2261 * @brief futex structure
2262 *
2263 * A k_futex is a lightweight mutual exclusion primitive designed
2264 * to minimize kernel involvement. Uncontended operation relies
2265 * only on atomic access to shared memory. k_futex are tracked as
2266 * kernel objects and can live in user memory so any access bypass
2267 * the kernel object permission management mechanism.
2268 */
2269struct k_futex {
2270 atomic_t val;
2271};
2272
2273/**
2274 * @brief futex kernel data structure
2275 *
2276 * z_futex_data are the helper data structure for k_futex to complete
2277 * futex contended operation on kernel side, structure z_futex_data
2278 * of every futex object is invisible in user mode.
2279 */
2280struct z_futex_data {
2281 _wait_q_t wait_q;
2282 struct k_spinlock lock;
2283};
2284
2285#define Z_FUTEX_DATA_INITIALIZER(obj) \
2286 { \
2287 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2288 }
2289
2290/**
2291 * @defgroup futex_apis FUTEX APIs
2292 * @ingroup kernel_apis
2293 * @{
2294 */
2295
2296/**
Wentong Wu5611e922019-06-20 23:51:27 +08002297 * @brief Pend the current thread on a futex
2298 *
2299 * Tests that the supplied futex contains the expected value, and if so,
2300 * goes to sleep until some other thread calls k_futex_wake() on it.
2301 *
2302 * @param futex Address of the futex.
2303 * @param expected Expected value of the futex, if it is different the caller
2304 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002305 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2306 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002307 * @retval -EACCES Caller does not have read access to futex address.
2308 * @retval -EAGAIN If the futex value did not match the expected parameter.
2309 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2310 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2311 * @retval 0 if the caller went to sleep and was woken up. The caller
2312 * should check the futex's value on wakeup to determine if it needs
2313 * to block again.
2314 */
2315__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2316
2317/**
2318 * @brief Wake one/all threads pending on a futex
2319 *
2320 * Wake up the highest priority thread pending on the supplied futex, or
2321 * wakeup all the threads pending on the supplied futex, and the behavior
2322 * depends on wake_all.
2323 *
2324 * @param futex Futex to wake up pending threads.
2325 * @param wake_all If true, wake up all pending threads; If false,
2326 * wakeup the highest priority thread.
2327 * @retval -EACCES Caller does not have access to the futex address.
2328 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2329 * @retval Number of threads that were woken up.
2330 */
2331__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2332
2333/** @} */
2334#endif
2335
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002337 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338};
2339
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002340/**
2341 * @cond INTERNAL_HIDDEN
2342 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002343#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002344 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002345 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002346 }
2347
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002348#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002349
Allan Stephensc98da842016-11-11 15:45:03 -05002350/**
2351 * INTERNAL_HIDDEN @endcond
2352 */
2353
2354/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002355 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002356 * @ingroup kernel_apis
2357 * @{
2358 */
2359
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002361 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002363 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002365 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366 *
2367 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002368 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002370#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002371 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372
2373/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002374 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002375 *
2376 * This routine causes first thread pending on @a fifo, if any, to
2377 * return from k_fifo_get() call with NULL value (as if timeout
2378 * expired).
2379 *
2380 * @note Can be called by ISRs.
2381 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002382 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002383 *
2384 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002385 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002386 */
2387#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002388 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002389
2390/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002391 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002393 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002394 * aligned on a word boundary, and the first word of the item is reserved
2395 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @note Can be called by ISRs.
2398 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002399 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002400 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 *
2402 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002403 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002405#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002406 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407
2408/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002409 * @brief Add an element to a FIFO queue.
2410 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002411 * This routine adds a data item to @a fifo. There is an implicit memory
2412 * allocation to create an additional temporary bookkeeping data structure from
2413 * the calling thread's resource pool, which is automatically freed when the
2414 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002415 *
2416 * @note Can be called by ISRs.
2417 *
2418 * @param fifo Address of the FIFO.
2419 * @param data Address of the data item.
2420 *
2421 * @retval 0 on success
2422 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002423 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002424 */
2425#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002426 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002427
2428/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002429 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002432 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 * each data item pointing to the next data item; the list must be
2434 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002438 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439 * @param head Pointer to first node in singly-linked list.
2440 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 *
2442 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002443 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002445#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002446 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002447
2448/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002449 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 * This routine adds a list of data items to @a fifo in one operation.
2452 * The data items must be in a singly-linked list implemented using a
2453 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454 * and must be re-initialized via sys_slist_init().
2455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @note Can be called by ISRs.
2457 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002458 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002459 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 *
2461 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002462 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002464#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002465 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466
2467/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002468 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002471 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2474 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002475 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 * or one of the special values K_NO_WAIT and K_FOREVER.
2478 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002479 * @return Address of the data item if successful; NULL if returned
2480 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002481 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002483#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002484 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002487 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002488 *
2489 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002490 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002491 *
2492 * @note Can be called by ISRs.
2493 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002494 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002495 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002496 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002497 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002498 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002499 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002500#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002501 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002502
2503/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002504 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002505 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002506 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302507 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002508 * on each iteration of processing, a head container will be peeked,
2509 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002510 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002511 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002512 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002513 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002514 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002515 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002516 */
2517#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002518 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002519
2520/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002521 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002522 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002523 * Return element from the tail of FIFO queue (without removing it). A usecase
2524 * for this is if elements of the FIFO queue are themselves containers. Then
2525 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002526 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002527 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002528 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002529 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002530 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002531 */
2532#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002533 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002534
2535/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002536 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002538 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002540 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002542 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002543 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002544 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002546 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002547 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548
Anas Nashif166f5192018-02-25 08:02:36 -06002549/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002550
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002552 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553};
2554
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002555/**
2556 * @cond INTERNAL_HIDDEN
2557 */
2558
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002559#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002560 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002561 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002562 }
2563
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002564#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002565
Allan Stephensc98da842016-11-11 15:45:03 -05002566/**
2567 * INTERNAL_HIDDEN @endcond
2568 */
2569
2570/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002571 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002572 * @ingroup kernel_apis
2573 * @{
2574 */
2575
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002576/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002577 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002579 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002580 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002581 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002582 *
2583 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002584 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002585 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002586#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002587 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002588
2589/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002590 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002591 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002592 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002593 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002594 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 * @note Can be called by ISRs.
2597 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002598 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002600 *
2601 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002602 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002604#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002605 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002606
2607/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002608 * @brief Add an element to a LIFO queue.
2609 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002610 * This routine adds a data item to @a lifo. There is an implicit memory
2611 * allocation to create an additional temporary bookkeeping data structure from
2612 * the calling thread's resource pool, which is automatically freed when the
2613 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002614 *
2615 * @note Can be called by ISRs.
2616 *
2617 * @param lifo Address of the LIFO.
2618 * @param data Address of the data item.
2619 *
2620 * @retval 0 on success
2621 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002622 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002623 */
2624#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002625 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002626
2627/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002628 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002631 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002633 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2634 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002635 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002637 * or one of the special values K_NO_WAIT and K_FOREVER.
2638 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002639 * @return Address of the data item if successful; NULL if returned
2640 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002641 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002643#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002644 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002647 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002648 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002649 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002650 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002651 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002653 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002654 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002655 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002657 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002658 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659
Anas Nashif166f5192018-02-25 08:02:36 -06002660/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002661
2662/**
2663 * @cond INTERNAL_HIDDEN
2664 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302665#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002667typedef uintptr_t stack_data_t;
2668
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669struct k_stack {
2670 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002671 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002672 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002674 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002675 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002676 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677};
2678
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002679#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002680 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002681 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002682 .base = stack_buffer, \
2683 .next = stack_buffer, \
2684 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002685 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002686 }
2687
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002688#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002689
Allan Stephensc98da842016-11-11 15:45:03 -05002690/**
2691 * INTERNAL_HIDDEN @endcond
2692 */
2693
2694/**
2695 * @defgroup stack_apis Stack APIs
2696 * @ingroup kernel_apis
2697 * @{
2698 */
2699
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700/**
2701 * @brief Initialize a stack.
2702 *
2703 * This routine initializes a stack object, prior to its first use.
2704 *
2705 * @param stack Address of the stack.
2706 * @param buffer Address of array used to hold stacked values.
2707 * @param num_entries Maximum number of values that can be stacked.
2708 *
2709 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002710 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 */
Andrew Boief3bee952018-05-02 17:44:39 -07002712void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002713 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002714
2715
2716/**
2717 * @brief Initialize a stack.
2718 *
2719 * This routine initializes a stack object, prior to its first use. Internal
2720 * buffers will be allocated from the calling thread's resource pool.
2721 * This memory will be released if k_stack_cleanup() is called, or
2722 * userspace is enabled and the stack object loses all references to it.
2723 *
2724 * @param stack Address of the stack.
2725 * @param num_entries Maximum number of values that can be stacked.
2726 *
2727 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002728 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002729 */
2730
Adithya Baglody28080d32018-10-15 11:48:51 +05302731__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2732 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002733
2734/**
2735 * @brief Release a stack's allocated buffer
2736 *
2737 * If a stack object was given a dynamically allocated buffer via
2738 * k_stack_alloc_init(), this will free it. This function does nothing
2739 * if the buffer wasn't dynamically allocated.
2740 *
2741 * @param stack Address of the stack.
Anas Nashif1ed67d12019-06-16 08:58:10 -04002742 * @retval 0 on success
2743 * @retval -EAGAIN when object is still in use
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002744 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002745 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002746int k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747
2748/**
2749 * @brief Push an element onto a stack.
2750 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002751 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002752 *
2753 * @note Can be called by ISRs.
2754 *
2755 * @param stack Address of the stack.
2756 * @param data Value to push onto the stack.
2757 *
Anas Nashif1ed67d12019-06-16 08:58:10 -04002758 * @retval 0 on success
2759 * @retval -ENOMEM if stack is full
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002760 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002762__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002763
2764/**
2765 * @brief Pop an element from a stack.
2766 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002767 * This routine removes a stack_data_t value from @a stack in a "last in,
2768 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002769 *
2770 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2771 *
2772 * @param stack Address of the stack.
2773 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002774 * @param timeout Non-negative waiting period to obtain a value (in
2775 * milliseconds), or one of the special values K_NO_WAIT and
2776 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002777 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002778 * @retval 0 Element popped from stack.
2779 * @retval -EBUSY Returned without waiting.
2780 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002781 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002782 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002783__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2784 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002785
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002787 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002789 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002790 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002791 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * @param name Name of the stack.
2794 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002795 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002796 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002797#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002798 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002799 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002800 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002801 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002802 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803
Anas Nashif166f5192018-02-25 08:02:36 -06002804/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002805
Allan Stephens6bba9b02016-11-16 14:56:54 -05002806struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002807struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002808
Piotr Zięcik19d83492019-09-27 09:16:25 +02002809/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002810typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2811struct _poller {
2812 volatile bool is_polling;
2813 struct k_thread *thread;
2814 _poller_cb_t cb;
2815};
2816
Allan Stephensc98da842016-11-11 15:45:03 -05002817/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002818 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002819 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820 */
2821
Allan Stephens6bba9b02016-11-16 14:56:54 -05002822/**
2823 * @typedef k_work_handler_t
2824 * @brief Work item handler function type.
2825 *
2826 * A work item's handler function is executed by a workqueue's thread
2827 * when the work item is processed by the workqueue.
2828 *
2829 * @param work Address of the work item.
2830 *
2831 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002832 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002833 */
2834typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835
2836/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002837 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002839
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002841 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002842 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843};
2844
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002846 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847};
2848
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002850 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002851 k_work_handler_t handler;
2852 atomic_t flags[1];
2853};
2854
Allan Stephens6bba9b02016-11-16 14:56:54 -05002855struct k_delayed_work {
2856 struct k_work work;
2857 struct _timeout timeout;
2858 struct k_work_q *work_q;
2859};
2860
Piotr Zięcik19d83492019-09-27 09:16:25 +02002861struct k_work_poll {
2862 struct k_work work;
2863 struct _poller poller;
2864 struct k_poll_event *events;
2865 int num_events;
2866 k_work_handler_t real_handler;
2867 struct _timeout timeout;
2868 int poll_result;
2869};
2870
Allan Stephens6bba9b02016-11-16 14:56:54 -05002871extern struct k_work_q k_sys_work_q;
2872
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002873/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002874 * INTERNAL_HIDDEN @endcond
2875 */
2876
Patrik Flykt4344e272019-03-08 14:19:05 -07002877#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002878 { \
2879 ._reserved = NULL, \
2880 .handler = work_handler, \
2881 .flags = { 0 } \
2882 }
2883
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002884#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002885
Allan Stephens6bba9b02016-11-16 14:56:54 -05002886/**
2887 * @brief Initialize a statically-defined work item.
2888 *
2889 * This macro can be used to initialize a statically-defined workqueue work
2890 * item, prior to its first use. For example,
2891 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002892 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002893 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002894 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002895 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002896 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002897 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002898#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002899 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900
2901/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002902 * @brief Initialize a work item.
2903 *
2904 * This routine initializes a workqueue work item, prior to its first use.
2905 *
2906 * @param work Address of work item.
2907 * @param handler Function to invoke each time work item is processed.
2908 *
2909 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002910 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002911 */
2912static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2913{
Patrik Flykt4344e272019-03-08 14:19:05 -07002914 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915}
2916
2917/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002918 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002919 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002920 * This routine submits work item @a work to be processed by workqueue
2921 * @a work_q. If the work item is already pending in the workqueue's queue
2922 * as a result of an earlier submission, this routine has no effect on the
2923 * work item. If the work item has already been processed, or is currently
2924 * being processed, its work is considered complete and the work item can be
2925 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002926 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002927 * @warning
2928 * A submitted work item must not be modified until it has been processed
2929 * by the workqueue.
2930 *
2931 * @note Can be called by ISRs.
2932 *
2933 * @param work_q Address of workqueue.
2934 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002935 *
2936 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002937 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938 */
2939static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2940 struct k_work *work)
2941{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002942 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002943 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 }
2945}
2946
2947/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002948 * @brief Submit a work item to a user mode workqueue
2949 *
David B. Kinder06d78352018-12-17 14:32:40 -08002950 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002951 * memory allocation is made from the caller's resource pool which is freed
2952 * once the worker thread consumes the k_work item. The workqueue
2953 * thread must have memory access to the k_work item being submitted. The caller
2954 * must have permission granted on the work_q parameter's queue object.
2955 *
2956 * Otherwise this works the same as k_work_submit_to_queue().
2957 *
2958 * @note Can be called by ISRs.
2959 *
2960 * @param work_q Address of workqueue.
2961 * @param work Address of work item.
2962 *
2963 * @retval -EBUSY if the work item was already in some workqueue
2964 * @retval -ENOMEM if no memory for thread resource pool allocation
2965 * @retval 0 Success
2966 * @req K-WORK-001
2967 */
2968static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2969 struct k_work *work)
2970{
2971 int ret = -EBUSY;
2972
2973 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2974 ret = k_queue_alloc_append(&work_q->queue, work);
2975
2976 /* Couldn't insert into the queue. Clear the pending bit
2977 * so the work item can be submitted again
2978 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002979 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002980 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2981 }
2982 }
2983
2984 return ret;
2985}
2986
2987/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002988 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002989 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002990 * This routine indicates if work item @a work is pending in a workqueue's
2991 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002992 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002993 * @note Can be called by ISRs.
2994 *
2995 * @param work Address of work item.
2996 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002997 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002998 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002999 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08003000static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03003001{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03003002 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03003003}
3004
3005/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003006 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003008 * This routine starts workqueue @a work_q. The workqueue spawns its work
3009 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003010 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003011 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07003012 * @param stack Pointer to work queue thread's stack space, as defined by
3013 * K_THREAD_STACK_DEFINE()
3014 * @param stack_size Size of the work queue thread's stack (in bytes), which
3015 * should either be the same constant passed to
3016 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05003017 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003018 *
3019 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003020 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021 */
Andrew Boie507852a2017-07-25 18:47:07 -07003022extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07003023 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05003024 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003026/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08003027 * @brief Start a workqueue in user mode
3028 *
3029 * This works identically to k_work_q_start() except it is callable from user
3030 * mode, and the worker thread created will run in user mode.
3031 * The caller must have permissions granted on both the work_q parameter's
3032 * thread and queue objects, and the same restrictions on priority apply as
3033 * k_thread_create().
3034 *
3035 * @param work_q Address of workqueue.
3036 * @param stack Pointer to work queue thread's stack space, as defined by
3037 * K_THREAD_STACK_DEFINE()
3038 * @param stack_size Size of the work queue thread's stack (in bytes), which
3039 * should either be the same constant passed to
3040 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
3041 * @param prio Priority of the work queue's thread.
3042 *
3043 * @return N/A
3044 * @req K-WORK-001
3045 */
3046extern void k_work_q_user_start(struct k_work_q *work_q,
3047 k_thread_stack_t *stack,
3048 size_t stack_size, int prio);
3049
3050/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003051 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003052 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003053 * This routine initializes a workqueue delayed work item, prior to
3054 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003055 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003056 * @param work Address of delayed work item.
3057 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003058 *
3059 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003060 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003062extern void k_delayed_work_init(struct k_delayed_work *work,
3063 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064
3065/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003066 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003068 * This routine schedules work item @a work to be processed by workqueue
3069 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003070 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003071 * Only when the countdown completes is the work item actually submitted to
3072 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003074 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08003075 * counting down cancels the existing submission and restarts the
3076 * countdown using the new delay. Note that this behavior is
3077 * inherently subject to race conditions with the pre-existing
3078 * timeouts and work queue, so care must be taken to synchronize such
3079 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003081 * @warning
3082 * A delayed work item must not be modified until it has been processed
3083 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003085 * @note Can be called by ISRs.
3086 *
3087 * @param work_q Address of workqueue.
3088 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003089 * @param delay Non-negative delay before submitting the work item (in
3090 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003091 *
3092 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003093 * @retval -EINVAL Work item is being processed or has completed its work.
3094 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003095 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003097extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3098 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003099 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100
3101/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003102 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003104 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003105 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003106 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003108 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003110 * @note The result of calling this on a k_delayed_work item that has
3111 * not been submitted (i.e. before the return of the
3112 * k_delayed_work_submit_to_queue() call) is undefined.
3113 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003114 * @param work Address of delayed work item.
3115 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003116 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003117 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003118 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003120extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003121
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003122/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123 * @brief Submit a work item to the system workqueue.
3124 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003125 * This routine submits work item @a work to be processed by the system
3126 * workqueue. If the work item is already pending in the workqueue's queue
3127 * as a result of an earlier submission, this routine has no effect on the
3128 * work item. If the work item has already been processed, or is currently
3129 * being processed, its work is considered complete and the work item can be
3130 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003131 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003132 * @warning
3133 * Work items submitted to the system workqueue should avoid using handlers
3134 * that block or yield since this may prevent the system workqueue from
3135 * processing other work items in a timely manner.
3136 *
3137 * @note Can be called by ISRs.
3138 *
3139 * @param work Address of work item.
3140 *
3141 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003142 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003143 */
3144static inline void k_work_submit(struct k_work *work)
3145{
3146 k_work_submit_to_queue(&k_sys_work_q, work);
3147}
3148
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150 * @brief Submit a delayed work item to the system workqueue.
3151 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003152 * This routine schedules work item @a work to be processed by the system
3153 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003154 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003155 * Only when the countdown completes is the work item actually submitted to
3156 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003158 * Submitting a previously submitted delayed work item that is still
3159 * counting down cancels the existing submission and restarts the countdown
3160 * using the new delay. If the work item is currently pending on the
3161 * workqueue's queue because the countdown has completed it is too late to
3162 * resubmit the item, and resubmission fails without impacting the work item.
3163 * If the work item has already been processed, or is currently being processed,
3164 * its work is considered complete and the work item can be resubmitted.
3165 *
3166 * @warning
3167 * Work items submitted to the system workqueue should avoid using handlers
3168 * that block or yield since this may prevent the system workqueue from
3169 * processing other work items in a timely manner.
3170 *
3171 * @note Can be called by ISRs.
3172 *
3173 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003174 * @param delay Non-negative delay before submitting the work item (in
3175 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003176 *
3177 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003178 * @retval -EINVAL Work item is being processed or has completed its work.
3179 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003180 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003181 */
3182static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003183 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003185 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003186}
3187
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003189 * @brief Get time remaining before a delayed work gets scheduled.
3190 *
3191 * This routine computes the (approximate) time remaining before a
3192 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003193 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003194 *
3195 * @param work Delayed work item.
3196 *
3197 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003198 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003199 */
Kumar Galacc334c72017-04-21 10:55:34 -05003200static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003201{
Andy Ross88924062019-10-03 11:43:10 -07003202 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003203}
3204
Piotr Zięcik19d83492019-09-27 09:16:25 +02003205/**
3206 * @brief Initialize a triggered work item.
3207 *
3208 * This routine initializes a workqueue triggered work item, prior to
3209 * its first use.
3210 *
3211 * @param work Address of triggered work item.
3212 * @param handler Function to invoke each time work item is processed.
3213 *
3214 * @return N/A
3215 */
3216extern void k_work_poll_init(struct k_work_poll *work,
3217 k_work_handler_t handler);
3218
3219/**
3220 * @brief Submit a triggered work item.
3221 *
3222 * This routine schedules work item @a work to be processed by workqueue
3223 * @a work_q when one of the given @a events is signaled. The routine
3224 * initiates internal poller for the work item and then returns to the caller.
3225 * Only when one of the watched events happen the work item is actually
3226 * submitted to the workqueue and becomes pending.
3227 *
3228 * Submitting a previously submitted triggered work item that is still
3229 * waiting for the event cancels the existing submission and reschedules it
3230 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003231 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003232 * so care must be taken to synchronize such resubmissions externally.
3233 *
3234 * @note Can be called by ISRs.
3235 *
3236 * @warning
3237 * Provided array of events as well as a triggered work item must be placed
3238 * in persistent memory (valid until work handler execution or work
3239 * cancellation) and cannot be modified after submission.
3240 *
3241 * @param work_q Address of workqueue.
3242 * @param work Address of delayed work item.
3243 * @param events An array of pointers to events which trigger the work.
3244 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003245 * @param timeout Non-negative timeout after which the work will be scheduled
3246 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003247 *
3248 *
3249 * @retval 0 Work item started watching for events.
3250 * @retval -EINVAL Work item is being processed or has completed its work.
3251 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3252 */
3253extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3254 struct k_work_poll *work,
3255 struct k_poll_event *events,
3256 int num_events,
3257 s32_t timeout);
3258
3259/**
3260 * @brief Submit a triggered work item to the system workqueue.
3261 *
3262 * This routine schedules work item @a work to be processed by system
3263 * workqueue when one of the given @a events is signaled. The routine
3264 * initiates internal poller for the work item and then returns to the caller.
3265 * Only when one of the watched events happen the work item is actually
3266 * submitted to the workqueue and becomes pending.
3267 *
3268 * Submitting a previously submitted triggered work item that is still
3269 * waiting for the event cancels the existing submission and reschedules it
3270 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003271 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003272 * so care must be taken to synchronize such resubmissions externally.
3273 *
3274 * @note Can be called by ISRs.
3275 *
3276 * @warning
3277 * Provided array of events as well as a triggered work item must not be
3278 * modified until the item has been processed by the workqueue.
3279 *
3280 * @param work Address of delayed work item.
3281 * @param events An array of pointers to events which trigger the work.
3282 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003283 * @param timeout Non-negative timeout after which the work will be scheduled
3284 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003285 *
3286 * @retval 0 Work item started watching for events.
3287 * @retval -EINVAL Work item is being processed or has completed its work.
3288 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3289 */
3290static inline int k_work_poll_submit(struct k_work_poll *work,
3291 struct k_poll_event *events,
3292 int num_events,
3293 s32_t timeout)
3294{
3295 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3296 events, num_events, timeout);
3297}
3298
3299/**
3300 * @brief Cancel a triggered work item.
3301 *
3302 * This routine cancels the submission of triggered work item @a work.
3303 * A triggered work item can only be canceled if no event triggered work
3304 * submission.
3305 *
3306 * @note Can be called by ISRs.
3307 *
3308 * @param work Address of delayed work item.
3309 *
David B. Kinder73896c02019-10-28 16:27:57 -07003310 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003311 * @retval -EINVAL Work item is being processed or has completed its work.
3312 */
3313extern int k_work_poll_cancel(struct k_work_poll *work);
3314
Anas Nashif166f5192018-02-25 08:02:36 -06003315/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003316/**
Anas Nashifce78d162018-05-24 12:43:11 -05003317 * @defgroup mutex_apis Mutex APIs
3318 * @ingroup kernel_apis
3319 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003320 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003321
Anas Nashifce78d162018-05-24 12:43:11 -05003322/**
3323 * Mutex Structure
3324 * @ingroup mutex_apis
3325 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05003327 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003329 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003330 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05003331
3332 /** Current lock count */
Kumar Galacc334c72017-04-21 10:55:34 -05003333 u32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05003334
3335 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003338 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003339 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003340};
3341
Anas Nashifce78d162018-05-24 12:43:11 -05003342/**
3343 * @cond INTERNAL_HIDDEN
3344 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003345#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003346 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003347 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348 .owner = NULL, \
3349 .lock_count = 0, \
3350 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003351 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352 }
3353
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003354#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003355
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003356/**
Allan Stephensc98da842016-11-11 15:45:03 -05003357 * INTERNAL_HIDDEN @endcond
3358 */
3359
3360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003364 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003365 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003368 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003369 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003371 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003372 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003373
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003374/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * Upon completion, the mutex is available and does not have an owner.
3380 *
3381 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003382 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003383 * @retval 0 Mutex object created
3384 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003385 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003386 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003387__syscall int k_mutex_init(struct k_mutex *mutex);
3388
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003389
3390/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * This routine locks @a mutex. If the mutex is locked by another thread,
3394 * the calling thread waits until the mutex becomes available or until
3395 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * A thread is permitted to lock a mutex it has already locked. The operation
3398 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003401 * @param timeout Non-negative waiting period to lock the mutex (in
3402 * milliseconds), or one of the special values K_NO_WAIT and
3403 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003404 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003405 * @retval 0 Mutex locked.
3406 * @retval -EBUSY Returned without waiting.
3407 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003408 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003409 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003410__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003411
3412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * This routine unlocks @a mutex. The mutex must already be locked by the
3416 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003417 *
3418 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003420 * thread.
3421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003422 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003423 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003424 * @retval 0 Mutex unlocked.
3425 * @retval -EPERM The current thread does not own the mutex
3426 * @retval -EINVAL The mutex is not locked
3427 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003428 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003429 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003430__syscall int k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431
Allan Stephensc98da842016-11-11 15:45:03 -05003432/**
Anas Nashif166f5192018-02-25 08:02:36 -06003433 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003434 */
3435
3436/**
3437 * @cond INTERNAL_HIDDEN
3438 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439
3440struct k_sem {
3441 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303442 u32_t count;
3443 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003444 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003445
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003446 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003447 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003448};
3449
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003450#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003451 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003452 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003453 .count = initial_count, \
3454 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003455 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003456 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003457 }
3458
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003459#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003460
Allan Stephensc98da842016-11-11 15:45:03 -05003461/**
3462 * INTERNAL_HIDDEN @endcond
3463 */
3464
3465/**
3466 * @defgroup semaphore_apis Semaphore APIs
3467 * @ingroup kernel_apis
3468 * @{
3469 */
3470
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003472 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * @param sem Address of the semaphore.
3477 * @param initial_count Initial semaphore count.
3478 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003479 *
Anas Nashif928af3c2019-05-04 10:36:14 -04003480 * @retval 0 Semaphore created successfully
3481 * @retval -EINVAL Invalid values
3482 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003483 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003484 */
Anas Nashif928af3c2019-05-04 10:36:14 -04003485__syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count,
Andrew Boie99280232017-09-29 14:17:47 -07003486 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003487
3488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003493 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3494 *
3495 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003496 * @param timeout Non-negative waiting period to take the semaphore (in
3497 * milliseconds), or one of the special values K_NO_WAIT and
3498 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003499 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003500 * @retval 0 Semaphore taken.
3501 * @retval -EBUSY Returned without waiting.
3502 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003503 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003504 */
Andrew Boie99280232017-09-29 14:17:47 -07003505__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003506
3507/**
3508 * @brief Give a semaphore.
3509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * This routine gives @a sem, unless the semaphore is already at its maximum
3511 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003513 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003516 *
3517 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003518 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003519 */
Andrew Boie99280232017-09-29 14:17:47 -07003520__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003522/**
3523 * @brief Reset a semaphore's count to zero.
3524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003527 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003528 *
3529 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003530 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003531 */
Andrew Boie990bf162017-10-03 12:36:49 -07003532__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003533
Anas Nashif954d5502018-02-25 08:37:28 -06003534/**
3535 * @internal
3536 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003537static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538{
Patrik Flykt24d71432019-03-26 19:57:45 -06003539 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540}
3541
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003542/**
3543 * @brief Get a semaphore's count.
3544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003545 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003547 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003549 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003550 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003551 */
Andrew Boie990bf162017-10-03 12:36:49 -07003552__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003553
Anas Nashif954d5502018-02-25 08:37:28 -06003554/**
3555 * @internal
3556 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003557static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003558{
3559 return sem->count;
3560}
3561
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003562/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003566 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003567 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * @param name Name of the semaphore.
3570 * @param initial_count Initial semaphore count.
3571 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003572 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003573 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003574#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003575 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003576 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303577 BUILD_ASSERT(((count_limit) != 0) && \
3578 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579
Anas Nashif166f5192018-02-25 08:02:36 -06003580/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003581
3582/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003583 * @defgroup msgq_apis Message Queue APIs
3584 * @ingroup kernel_apis
3585 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003586 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003587
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003588/**
3589 * @brief Message Queue Structure
3590 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003591struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05003592 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05003594 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07003595 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05003596 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003597 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003598 /** Maximal number of messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003599 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003600 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003601 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05003602 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003603 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05003604 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003605 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003606 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003607 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003608 /** Number of used messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003609 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003610
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003611 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003612 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05003613
3614 /** Message queue */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003615 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003617/**
3618 * @cond INTERNAL_HIDDEN
3619 */
3620
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003621
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003622#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003623 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003624 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003625 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003626 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003627 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003628 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003629 .read_ptr = q_buffer, \
3630 .write_ptr = q_buffer, \
3631 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003632 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003633 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003634#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003635/**
3636 * INTERNAL_HIDDEN @endcond
3637 */
3638
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003639
Andrew Boie0fe789f2018-04-12 18:35:56 -07003640#define K_MSGQ_FLAG_ALLOC BIT(0)
3641
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003642/**
3643 * @brief Message Queue Attributes
3644 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303645struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05003646 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303647 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003648 /** Maximal number of messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303649 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003650 /** Used messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303651 u32_t used_msgs;
3652};
3653
Allan Stephensc98da842016-11-11 15:45:03 -05003654
3655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3659 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003660 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3661 * message is similarly aligned to this boundary, @a q_msg_size must also be
3662 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 * The message queue can be accessed outside the module where it is defined
3665 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003666 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003667 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * @param q_name Name of the message queue.
3670 * @param q_msg_size Message size (in bytes).
3671 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003672 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003673 *
3674 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003675 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003676#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3677 static char __noinit __aligned(q_align) \
3678 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3679 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3680 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003681 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003682
Peter Mitsisd7a37502016-10-13 11:37:40 -04003683/**
3684 * @brief Initialize a message queue.
3685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * This routine initializes a message queue object, prior to its first use.
3687 *
Allan Stephensda827222016-11-09 14:23:58 -06003688 * The message queue's ring buffer must contain space for @a max_msgs messages,
3689 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3690 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3691 * that each message is similarly aligned to this boundary, @a q_msg_size
3692 * must also be a multiple of N.
3693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * @param q Address of the message queue.
3695 * @param buffer Pointer to ring buffer that holds queued messages.
3696 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003697 * @param max_msgs Maximum number of messages that can be queued.
3698 *
3699 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003700 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003701 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003702void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3703 u32_t max_msgs);
3704
3705/**
3706 * @brief Initialize a message queue.
3707 *
3708 * This routine initializes a message queue object, prior to its first use,
3709 * allocating its internal ring buffer from the calling thread's resource
3710 * pool.
3711 *
3712 * Memory allocated for the ring buffer can be released by calling
3713 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3714 * all of its references.
3715 *
Anas Nashif4b386592019-11-25 09:30:47 -05003716 * @param msgq Address of the message queue.
Andrew Boie0fe789f2018-04-12 18:35:56 -07003717 * @param msg_size Message size (in bytes).
3718 * @param max_msgs Maximum number of messages that can be queued.
3719 *
3720 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3721 * thread's resource pool, or -EINVAL if the size parameters cause
3722 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003723 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003724 */
Anas Nashif4b386592019-11-25 09:30:47 -05003725__syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size,
Andrew Boie0fe789f2018-04-12 18:35:56 -07003726 u32_t max_msgs);
3727
Anas Nashife71293e2019-12-04 20:00:14 -05003728/**
Anas Nashif4b386592019-11-25 09:30:47 -05003729 * @brief Release allocated buffer for a queue
Anas Nashife71293e2019-12-04 20:00:14 -05003730 *
3731 * Releases memory allocated for the ring buffer.
Anas Nashif4b386592019-11-25 09:30:47 -05003732 *
3733 * @param msgq message queue to cleanup
3734 *
Anas Nashif11b93652019-06-16 08:43:48 -04003735 * @retval 0 on success
3736 * @retval -EBUSY Queue not empty
Anas Nashife71293e2019-12-04 20:00:14 -05003737 */
Anas Nashif11b93652019-06-16 08:43:48 -04003738int k_msgq_cleanup(struct k_msgq *msgq);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003739
3740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003743 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003744 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003745 * @note Can be called by ISRs.
3746 *
Anas Nashif4b386592019-11-25 09:30:47 -05003747 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003749 * @param timeout Non-negative waiting period to add the message (in
3750 * milliseconds), or one of the special values K_NO_WAIT and
3751 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003752 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003753 * @retval 0 Message sent.
3754 * @retval -ENOMSG Returned without waiting or queue purged.
3755 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003756 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003757 */
Anas Nashif4b386592019-11-25 09:30:47 -05003758__syscall int k_msgq_put(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003759
3760/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003761 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * This routine receives a message from message queue @a q in a "first in,
3764 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003765 *
Allan Stephensc98da842016-11-11 15:45:03 -05003766 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003767 *
Anas Nashif4b386592019-11-25 09:30:47 -05003768 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003769 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003770 * @param timeout Non-negative waiting period to receive the message (in
3771 * milliseconds), or one of the special values K_NO_WAIT and
3772 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003773 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003774 * @retval 0 Message received.
3775 * @retval -ENOMSG Returned without waiting.
3776 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003777 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003778 */
Anas Nashif4b386592019-11-25 09:30:47 -05003779__syscall int k_msgq_get(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003780
3781/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003782 * @brief Peek/read a message from a message queue.
3783 *
3784 * This routine reads a message from message queue @a q in a "first in,
3785 * first out" manner and leaves the message in the queue.
3786 *
3787 * @note Can be called by ISRs.
3788 *
Anas Nashif4b386592019-11-25 09:30:47 -05003789 * @param msgq Address of the message queue.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003790 * @param data Address of area to hold the message read from the queue.
3791 *
3792 * @retval 0 Message read.
3793 * @retval -ENOMSG Returned when the queue has no message.
3794 * @req K-MSGQ-002
3795 */
Anas Nashif4b386592019-11-25 09:30:47 -05003796__syscall int k_msgq_peek(struct k_msgq *msgq, void *data);
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003797
3798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003799 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003801 * This routine discards all unreceived messages in a message queue's ring
3802 * buffer. Any threads that are blocked waiting to send a message to the
3803 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003804 *
Anas Nashif4b386592019-11-25 09:30:47 -05003805 * @param msgq Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003806 *
3807 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003808 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003809 */
Anas Nashif4b386592019-11-25 09:30:47 -05003810__syscall void k_msgq_purge(struct k_msgq *msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811
Peter Mitsis67be2492016-10-07 11:44:34 -04003812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 * This routine returns the number of unused entries in a message queue's
3816 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003817 *
Anas Nashif4b386592019-11-25 09:30:47 -05003818 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003819 *
3820 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003821 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003822 */
Anas Nashif4b386592019-11-25 09:30:47 -05003823__syscall u32_t k_msgq_num_free_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003824
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303825/**
3826 * @brief Get basic attributes of a message queue.
3827 *
3828 * This routine fetches basic attributes of message queue into attr argument.
3829 *
Anas Nashif4b386592019-11-25 09:30:47 -05003830 * @param msgq Address of the message queue.
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303831 * @param attrs pointer to message queue attribute structure.
3832 *
3833 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003834 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303835 */
Anas Nashif4b386592019-11-25 09:30:47 -05003836__syscall void k_msgq_get_attrs(struct k_msgq *msgq,
3837 struct k_msgq_attrs *attrs);
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303838
3839
Anas Nashif4b386592019-11-25 09:30:47 -05003840static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq)
Peter Mitsis67be2492016-10-07 11:44:34 -04003841{
Anas Nashif4b386592019-11-25 09:30:47 -05003842 return msgq->max_msgs - msgq->used_msgs;
Peter Mitsis67be2492016-10-07 11:44:34 -04003843}
3844
Peter Mitsisd7a37502016-10-13 11:37:40 -04003845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003848 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003849 *
Anas Nashif4b386592019-11-25 09:30:47 -05003850 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 *
3852 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003853 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003854 */
Anas Nashif4b386592019-11-25 09:30:47 -05003855__syscall u32_t k_msgq_num_used_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003856
Anas Nashif4b386592019-11-25 09:30:47 -05003857static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003858{
Anas Nashif4b386592019-11-25 09:30:47 -05003859 return msgq->used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003860}
3861
Anas Nashif166f5192018-02-25 08:02:36 -06003862/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003863
3864/**
3865 * @defgroup mem_pool_apis Memory Pool APIs
3866 * @ingroup kernel_apis
3867 * @{
3868 */
3869
Andy Ross73cb9582017-05-09 10:42:39 -07003870/* Note on sizing: the use of a 20 bit field for block means that,
3871 * assuming a reasonable minimum block size of 16 bytes, we're limited
3872 * to 16M of memory managed by a single pool. Long term it would be
3873 * good to move to a variable bit size based on configuration.
3874 */
3875struct k_mem_block_id {
3876 u32_t pool : 8;
3877 u32_t level : 4;
3878 u32_t block : 20;
3879};
3880
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003882 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003883 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003884};
3885
Anas Nashif166f5192018-02-25 08:02:36 -06003886/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003887
3888/**
3889 * @defgroup mailbox_apis Mailbox APIs
3890 * @ingroup kernel_apis
3891 * @{
3892 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003893
Anas Nashife71293e2019-12-04 20:00:14 -05003894/**
3895 * @brief Mailbox Message Structure
3896 *
3897 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898struct k_mbox_msg {
3899 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003900 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003901 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003902 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003903 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003904 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003905 /** sender's message data buffer */
3906 void *tx_data;
3907 /** internal use only - needed for legacy API support */
3908 void *_rx_data;
3909 /** message data block descriptor */
3910 struct k_mem_block tx_block;
3911 /** source thread id */
3912 k_tid_t rx_source_thread;
3913 /** target thread id */
3914 k_tid_t tx_target_thread;
3915 /** internal use only - thread waiting on send (may be a dummy) */
3916 k_tid_t _syncing_thread;
3917#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3918 /** internal use only - semaphore used during asynchronous send */
3919 struct k_sem *_async_sem;
3920#endif
3921};
Anas Nashife71293e2019-12-04 20:00:14 -05003922/**
3923 * @brief Mailbox Structure
3924 *
3925 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003926struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05003927 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003928 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05003929 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003930 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003931 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003932
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003933 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003934 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003935};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003936/**
3937 * @cond INTERNAL_HIDDEN
3938 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003939
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003940#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003941 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003942 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3943 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003944 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003945 }
3946
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003947#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003948
Peter Mitsis12092702016-10-14 12:57:23 -04003949/**
Allan Stephensc98da842016-11-11 15:45:03 -05003950 * INTERNAL_HIDDEN @endcond
3951 */
3952
3953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003954 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003956 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003957 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003958 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003961 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003962 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003963#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003964 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003965 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966
Peter Mitsis12092702016-10-14 12:57:23 -04003967/**
3968 * @brief Initialize a mailbox.
3969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003970 * This routine initializes a mailbox object, prior to its first use.
3971 *
3972 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003973 *
3974 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003975 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003977extern void k_mbox_init(struct k_mbox *mbox);
3978
Peter Mitsis12092702016-10-14 12:57:23 -04003979/**
3980 * @brief Send a mailbox message in a synchronous manner.
3981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003982 * This routine sends a message to @a mbox and waits for a receiver to both
3983 * receive and process it. The message data may be in a buffer, in a memory
3984 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003986 * @param mbox Address of the mailbox.
3987 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003988 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003989 * milliseconds), or one of the special values K_NO_WAIT
3990 * and K_FOREVER. Once the message has been received,
3991 * this routine waits as long as necessary for the message
3992 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003993 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003994 * @retval 0 Message sent.
3995 * @retval -ENOMSG Returned without waiting.
3996 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003997 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003998 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003999extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05004000 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004001
Peter Mitsis12092702016-10-14 12:57:23 -04004002/**
4003 * @brief Send a mailbox message in an asynchronous manner.
4004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004005 * This routine sends a message to @a mbox without waiting for a receiver
4006 * to process it. The message data may be in a buffer, in a memory pool block,
4007 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
4008 * will be given when the message has been both received and completely
4009 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04004010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004011 * @param mbox Address of the mailbox.
4012 * @param tx_msg Address of the transmit message descriptor.
4013 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04004014 *
4015 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004016 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004017 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004018extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004019 struct k_sem *sem);
4020
Peter Mitsis12092702016-10-14 12:57:23 -04004021/**
4022 * @brief Receive a mailbox message.
4023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * This routine receives a message from @a mbox, then optionally retrieves
4025 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004027 * @param mbox Address of the mailbox.
4028 * @param rx_msg Address of the receive message descriptor.
4029 * @param buffer Address of the buffer to receive data, or NULL to defer data
4030 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004031 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * milliseconds), or one of the special values K_NO_WAIT
4033 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004034 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004035 * @retval 0 Message received.
4036 * @retval -ENOMSG Returned without waiting.
4037 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004038 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004039 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004040extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05004041 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004042
4043/**
4044 * @brief Retrieve mailbox message data into a buffer.
4045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004046 * This routine completes the processing of a received message by retrieving
4047 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004048 *
4049 * Alternatively, this routine can be used to dispose of a received message
4050 * without retrieving its data.
4051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004052 * @param rx_msg Address of the receive message descriptor.
4053 * @param buffer Address of the buffer to receive data, or NULL to discard
4054 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04004055 *
4056 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004057 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004058 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004059extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04004060
4061/**
4062 * @brief Retrieve mailbox message data into a memory pool block.
4063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004064 * This routine completes the processing of a received message by retrieving
4065 * its data into a memory pool block, then disposing of the message.
4066 * The memory pool block that results from successful retrieval must be
4067 * returned to the pool once the data has been processed, even in cases
4068 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04004069 *
4070 * Alternatively, this routine can be used to dispose of a received message
4071 * without retrieving its data. In this case there is no need to return a
4072 * memory pool block to the pool.
4073 *
4074 * This routine allocates a new memory pool block for the data only if the
4075 * data is not already in one. If a new block cannot be allocated, the routine
4076 * returns a failure code and the received message is left unchanged. This
4077 * permits the caller to reattempt data retrieval at a later time or to dispose
4078 * of the received message without retrieving its data.
4079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004080 * @param rx_msg Address of a receive message descriptor.
4081 * @param pool Address of memory pool, or NULL to discard data.
4082 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004083 * @param timeout Non-negative waiting period to wait for a memory pool block
4084 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004085 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004086 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004087 * @retval 0 Data retrieved.
4088 * @retval -ENOMEM Returned without waiting.
4089 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004090 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004091 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004092extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04004093 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05004094 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004095
Anas Nashif166f5192018-02-25 08:02:36 -06004096/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004097
4098/**
Anas Nashifce78d162018-05-24 12:43:11 -05004099 * @defgroup pipe_apis Pipe APIs
4100 * @ingroup kernel_apis
4101 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004102 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004103
Anas Nashifce78d162018-05-24 12:43:11 -05004104/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004106 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4107 size_t size; /**< Buffer size */
4108 size_t bytes_used; /**< # bytes used in buffer */
4109 size_t read_index; /**< Where in buffer to read from */
4110 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004111 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004112
4113 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004114 _wait_q_t readers; /**< Reader wait queue */
4115 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004116 } wait_q;
4117
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004118 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004119 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05004120 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004121};
4122
Anas Nashifce78d162018-05-24 12:43:11 -05004123/**
4124 * @cond INTERNAL_HIDDEN
4125 */
4126#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4127
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004128#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
4129 { \
4130 .buffer = pipe_buffer, \
4131 .size = pipe_buffer_size, \
4132 .bytes_used = 0, \
4133 .read_index = 0, \
4134 .write_index = 0, \
4135 .lock = {}, \
4136 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004137 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4138 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004139 }, \
4140 _OBJECT_TRACING_INIT \
4141 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004142 }
4143
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004144#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004145
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004146/**
Allan Stephensc98da842016-11-11 15:45:03 -05004147 * INTERNAL_HIDDEN @endcond
4148 */
4149
4150/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004151 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004153 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004154 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004155 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004157 * @param name Name of the pipe.
4158 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4159 * or zero if no ring buffer is used.
4160 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004161 *
4162 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004163 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004164#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004165 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004166 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004167 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004168 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004169
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004171 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004173 * This routine initializes a pipe object, prior to its first use.
4174 *
4175 * @param pipe Address of the pipe.
4176 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4177 * is used.
4178 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4179 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004180 *
4181 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004182 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004183 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004184void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4185
4186/**
4187 * @brief Release a pipe's allocated buffer
4188 *
4189 * If a pipe object was given a dynamically allocated buffer via
4190 * k_pipe_alloc_init(), this will free it. This function does nothing
4191 * if the buffer wasn't dynamically allocated.
4192 *
4193 * @param pipe Address of the pipe.
Anas Nashif361a84d2019-06-16 08:22:08 -04004194 * @retval 0 on success
4195 * @retval -EAGAIN nothing to cleanup
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004196 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004197 */
Anas Nashif361a84d2019-06-16 08:22:08 -04004198int k_pipe_cleanup(struct k_pipe *pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07004199
4200/**
4201 * @brief Initialize a pipe and allocate a buffer for it
4202 *
4203 * Storage for the buffer region will be allocated from the calling thread's
4204 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4205 * or userspace is enabled and the pipe object loses all references to it.
4206 *
4207 * This function should only be called on uninitialized pipe objects.
4208 *
4209 * @param pipe Address of the pipe.
4210 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4211 * buffer is used.
4212 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004213 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004214 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004215 */
4216__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004217
4218/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004219 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004221 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004223 * @param pipe Address of the pipe.
4224 * @param data Address of data to write.
4225 * @param bytes_to_write Size of data (in bytes).
4226 * @param bytes_written Address of area to hold the number of bytes written.
4227 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004228 * @param timeout Non-negative waiting period to wait for the data to be written
4229 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004230 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004231 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004232 * @retval 0 At least @a min_xfer bytes of data were written.
4233 * @retval -EIO Returned without waiting; zero data bytes were written.
4234 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004235 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004236 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004237 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004238__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4239 size_t bytes_to_write, size_t *bytes_written,
4240 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004241
4242/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004243 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004245 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004247 * @param pipe Address of the pipe.
4248 * @param data Address to place the data read from pipe.
4249 * @param bytes_to_read Maximum number of data bytes to read.
4250 * @param bytes_read Address of area to hold the number of bytes read.
4251 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004252 * @param timeout Non-negative waiting period to wait for the data to be read
4253 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004254 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004255 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004256 * @retval 0 At least @a min_xfer bytes of data were read.
Anas Nashif361a84d2019-06-16 08:22:08 -04004257 * @retval -EINVAL invalid parameters supplied
Allan Stephens9ef50f42016-11-16 15:33:31 -05004258 * @retval -EIO Returned without waiting; zero data bytes were read.
4259 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004260 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004261 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004262 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004263__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4264 size_t bytes_to_read, size_t *bytes_read,
4265 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004266
4267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004268 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004270 * This routine writes the data contained in a memory block to @a pipe.
4271 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004272 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004274 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004275 * @param block Memory block containing data to send
4276 * @param size Number of data bytes in memory block to send
4277 * @param sem Semaphore to signal upon completion (else NULL)
4278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004279 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004280 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004281 */
4282extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4283 size_t size, struct k_sem *sem);
4284
Anas Nashif166f5192018-02-25 08:02:36 -06004285/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004286
Allan Stephensc98da842016-11-11 15:45:03 -05004287/**
4288 * @cond INTERNAL_HIDDEN
4289 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004290
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004291struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004292 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004293 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004294 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004295 char *buffer;
4296 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004297 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004298
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004299 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004300 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004301};
4302
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004303#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004304 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004305 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004306 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004307 .num_blocks = slab_num_blocks, \
4308 .block_size = slab_block_size, \
4309 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004310 .free_list = NULL, \
4311 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004312 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004313 }
4314
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004315#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004316
4317
Peter Mitsis578f9112016-10-07 13:50:31 -04004318/**
Allan Stephensc98da842016-11-11 15:45:03 -05004319 * INTERNAL_HIDDEN @endcond
4320 */
4321
4322/**
4323 * @defgroup mem_slab_apis Memory Slab APIs
4324 * @ingroup kernel_apis
4325 * @{
4326 */
4327
4328/**
Allan Stephensda827222016-11-09 14:23:58 -06004329 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004330 *
Allan Stephensda827222016-11-09 14:23:58 -06004331 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004332 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004333 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4334 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004335 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004336 *
Allan Stephensda827222016-11-09 14:23:58 -06004337 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004338 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004339 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004340 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004342 * @param name Name of the memory slab.
4343 * @param slab_block_size Size of each memory block (in bytes).
4344 * @param slab_num_blocks Number memory blocks.
4345 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004346 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004347 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004348#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004349 char __noinit __aligned(WB_UP(slab_align)) \
4350 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004351 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004352 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004353 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004354
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004355/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004356 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004358 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004359 *
Allan Stephensda827222016-11-09 14:23:58 -06004360 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4361 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004362 * N-byte boundary matching a word boundary, where N is a power of 2
4363 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004364 * To ensure that each memory block is similarly aligned to this boundary,
4365 * @a slab_block_size must also be a multiple of N.
4366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004367 * @param slab Address of the memory slab.
4368 * @param buffer Pointer to buffer used for the memory blocks.
4369 * @param block_size Size of each memory block (in bytes).
4370 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004371 *
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004372 * @retval 0 on success
4373 * @retval -EINVAL invalid data supplied
4374 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004375 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004376 */
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004377extern int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004378 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004379
4380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004381 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004383 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004385 * @param slab Address of the memory slab.
4386 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004387 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004388 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4389 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004390 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004391 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004392 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004393 * @retval -ENOMEM Returned without waiting.
4394 * @retval -EAGAIN Waiting period timed out.
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004395 * @retval -EINVAL Invalid data supplied
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004396 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004397 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004398extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004399 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004400
4401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004402 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004404 * This routine releases a previously allocated memory block back to its
4405 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004407 * @param slab Address of the memory slab.
4408 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004409 *
4410 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004411 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004412 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004413extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004414
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004416 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004418 * This routine gets the number of memory blocks that are currently
4419 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004421 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004423 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004424 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004425 */
Kumar Galacc334c72017-04-21 10:55:34 -05004426static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004427{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004428 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004429}
4430
Peter Mitsisc001aa82016-10-13 13:53:37 -04004431/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004432 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004434 * This routine gets the number of memory blocks that are currently
4435 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004437 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004439 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004440 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004441 */
Kumar Galacc334c72017-04-21 10:55:34 -05004442static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004443{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004444 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004445}
4446
Anas Nashif166f5192018-02-25 08:02:36 -06004447/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004448
4449/**
4450 * @cond INTERNAL_HIDDEN
4451 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004452
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004453struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004454 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004455 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004456};
4457
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004458/**
Allan Stephensc98da842016-11-11 15:45:03 -05004459 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004460 */
4461
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004462/**
Allan Stephensc98da842016-11-11 15:45:03 -05004463 * @addtogroup mem_pool_apis
4464 * @{
4465 */
4466
4467/**
4468 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004470 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4471 * long. The memory pool allows blocks to be repeatedly partitioned into
4472 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004473 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004474 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004475 * If the pool is to be accessed outside the module where it is defined, it
4476 * can be declared via
4477 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004478 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004480 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004481 * @param minsz Size of the smallest blocks in the pool (in bytes).
4482 * @param maxsz Size of the largest blocks in the pool (in bytes).
4483 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004484 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004485 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004486 */
Andy Ross73cb9582017-05-09 10:42:39 -07004487#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004488 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004489 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004490 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004491 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004492 .base = { \
4493 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004494 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004495 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004496 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004497 .levels = _mpool_lvls_##name, \
4498 .flags = SYS_MEM_POOL_KERNEL \
4499 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004500 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004501 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004502
Peter Mitsis937042c2016-10-13 13:18:26 -04004503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004504 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004506 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004508 * @param pool Address of the memory pool.
4509 * @param block Pointer to block descriptor for the allocated memory.
4510 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004511 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004512 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4513 * or K_FOREVER to wait as long as necessary.
4514 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004515 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004516 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004517 * @retval -ENOMEM Returned without waiting.
4518 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004519 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004520 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004521extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004522 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004523
4524/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004525 * @brief Allocate memory from a memory pool with malloc() semantics
4526 *
4527 * Such memory must be released using k_free().
4528 *
4529 * @param pool Address of the memory pool.
4530 * @param size Amount of memory to allocate (in bytes).
4531 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004532 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004533 */
4534extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4535
4536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004537 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004539 * This routine releases a previously allocated memory block back to its
4540 * memory pool.
4541 *
4542 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004543 *
4544 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004545 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004546 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004547extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004548
4549/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004550 * @brief Free memory allocated from a memory pool.
4551 *
4552 * This routine releases a previously allocated memory block back to its
4553 * memory pool
4554 *
4555 * @param id Memory block identifier.
4556 *
4557 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004558 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004559 */
4560extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4561
4562/**
Anas Nashif166f5192018-02-25 08:02:36 -06004563 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004564 */
4565
4566/**
4567 * @defgroup heap_apis Heap Memory Pool APIs
4568 * @ingroup kernel_apis
4569 * @{
4570 */
4571
4572/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004573 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004575 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004576 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004578 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004580 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004581 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004582 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004583extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004584
4585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004586 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004587 *
4588 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004589 * returned must have been allocated from the heap memory pool or
4590 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004591 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004592 * If @a ptr is NULL, no operation is performed.
4593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004594 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004595 *
4596 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004597 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004598 */
4599extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004600
Allan Stephensc98da842016-11-11 15:45:03 -05004601/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004602 * @brief Allocate memory from heap, array style
4603 *
4604 * This routine provides traditional calloc() semantics. Memory is
4605 * allocated from the heap memory pool and zeroed.
4606 *
4607 * @param nmemb Number of elements in the requested array
4608 * @param size Size of each array element (in bytes).
4609 *
4610 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004611 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004612 */
4613extern void *k_calloc(size_t nmemb, size_t size);
4614
Anas Nashif166f5192018-02-25 08:02:36 -06004615/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004616
Benjamin Walshacc68c12017-01-29 18:57:45 -05004617/* polling API - PRIVATE */
4618
Benjamin Walshb0179862017-02-02 16:39:57 -05004619#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004620#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004621#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004622#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004623#endif
4624
Benjamin Walshacc68c12017-01-29 18:57:45 -05004625/* private - types bit positions */
4626enum _poll_types_bits {
4627 /* can be used to ignore an event */
4628 _POLL_TYPE_IGNORE,
4629
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004630 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004631 _POLL_TYPE_SIGNAL,
4632
4633 /* semaphore availability */
4634 _POLL_TYPE_SEM_AVAILABLE,
4635
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004636 /* queue/fifo/lifo data availability */
4637 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004638
4639 _POLL_NUM_TYPES
4640};
4641
Patrik Flykt4344e272019-03-08 14:19:05 -07004642#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004643
4644/* private - states bit positions */
4645enum _poll_states_bits {
4646 /* default state when creating event */
4647 _POLL_STATE_NOT_READY,
4648
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004649 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004650 _POLL_STATE_SIGNALED,
4651
4652 /* semaphore is available */
4653 _POLL_STATE_SEM_AVAILABLE,
4654
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004655 /* data is available to read on queue/fifo/lifo */
4656 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004657
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004658 /* queue/fifo/lifo wait was cancelled */
4659 _POLL_STATE_CANCELLED,
4660
Benjamin Walshacc68c12017-01-29 18:57:45 -05004661 _POLL_NUM_STATES
4662};
4663
Patrik Flykt4344e272019-03-08 14:19:05 -07004664#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004665
4666#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004667 (32 - (0 \
4668 + 8 /* tag */ \
4669 + _POLL_NUM_TYPES \
4670 + _POLL_NUM_STATES \
4671 + 1 /* modes */ \
4672 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004673
Benjamin Walshacc68c12017-01-29 18:57:45 -05004674/* end of polling API - PRIVATE */
4675
4676
4677/**
4678 * @defgroup poll_apis Async polling APIs
4679 * @ingroup kernel_apis
4680 * @{
4681 */
4682
4683/* Public polling API */
4684
4685/* public - values for k_poll_event.type bitfield */
4686#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004687#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4688#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4689#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004690#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004691
4692/* public - polling modes */
4693enum k_poll_modes {
4694 /* polling thread does not take ownership of objects when available */
4695 K_POLL_MODE_NOTIFY_ONLY = 0,
4696
4697 K_POLL_NUM_MODES
4698};
4699
4700/* public - values for k_poll_event.state bitfield */
4701#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004702#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4703#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4704#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004705#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004706#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004707
4708/* public - poll signal object */
4709struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05004710 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004711 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004712
Anas Nashife71293e2019-12-04 20:00:14 -05004713 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004714 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4715 * user resets it to 0.
4716 */
4717 unsigned int signaled;
4718
Anas Nashife71293e2019-12-04 20:00:14 -05004719 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004720 int result;
4721};
4722
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004723#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004724 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004725 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004726 .signaled = 0, \
4727 .result = 0, \
4728 }
Anas Nashife71293e2019-12-04 20:00:14 -05004729/**
4730 * @brief Poll Event
4731 *
4732 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004733struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05004734 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004735 sys_dnode_t _node;
4736
Anas Nashife71293e2019-12-04 20:00:14 -05004737 /** PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004738 struct _poller *poller;
4739
Anas Nashife71293e2019-12-04 20:00:14 -05004740 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004741 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004742
Anas Nashife71293e2019-12-04 20:00:14 -05004743 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004744 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004745
Anas Nashife71293e2019-12-04 20:00:14 -05004746 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004747 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004748
Anas Nashife71293e2019-12-04 20:00:14 -05004749 /** mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004750 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004751
Anas Nashife71293e2019-12-04 20:00:14 -05004752 /** unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004753 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004754
Anas Nashife71293e2019-12-04 20:00:14 -05004755 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004756 union {
4757 void *obj;
4758 struct k_poll_signal *signal;
4759 struct k_sem *sem;
4760 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004761 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004762 };
4763};
4764
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004765#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004766 { \
4767 .poller = NULL, \
4768 .type = event_type, \
4769 .state = K_POLL_STATE_NOT_READY, \
4770 .mode = event_mode, \
4771 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004772 { .obj = event_obj }, \
4773 }
4774
4775#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4776 event_tag) \
4777 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004778 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004779 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004780 .state = K_POLL_STATE_NOT_READY, \
4781 .mode = event_mode, \
4782 .unused = 0, \
4783 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004784 }
4785
4786/**
4787 * @brief Initialize one struct k_poll_event instance
4788 *
4789 * After this routine is called on a poll event, the event it ready to be
4790 * placed in an event array to be passed to k_poll().
4791 *
4792 * @param event The event to initialize.
4793 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4794 * values. Only values that apply to the same object being polled
4795 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4796 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004797 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004798 * @param obj Kernel object or poll signal.
4799 *
4800 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004801 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004802 */
4803
Kumar Galacc334c72017-04-21 10:55:34 -05004804extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004805 int mode, void *obj);
4806
4807/**
4808 * @brief Wait for one or many of multiple poll events to occur
4809 *
4810 * This routine allows a thread to wait concurrently for one or many of
4811 * multiple poll events to have occurred. Such events can be a kernel object
4812 * being available, like a semaphore, or a poll signal event.
4813 *
4814 * When an event notifies that a kernel object is available, the kernel object
4815 * is not "given" to the thread calling k_poll(): it merely signals the fact
4816 * that the object was available when the k_poll() call was in effect. Also,
4817 * all threads trying to acquire an object the regular way, i.e. by pending on
4818 * the object, have precedence over the thread polling on the object. This
4819 * means that the polling thread will never get the poll event on an object
4820 * until the object becomes available and its pend queue is empty. For this
4821 * reason, the k_poll() call is more effective when the objects being polled
4822 * only have one thread, the polling thread, trying to acquire them.
4823 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004824 * When k_poll() returns 0, the caller should loop on all the events that were
4825 * passed to k_poll() and check the state field for the values that were
4826 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004827 *
4828 * Before being reused for another call to k_poll(), the user has to reset the
4829 * state field to K_POLL_STATE_NOT_READY.
4830 *
Andrew Boie3772f772018-05-07 16:52:57 -07004831 * When called from user mode, a temporary memory allocation is required from
4832 * the caller's resource pool.
4833 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004834 * @param events An array of pointers to events to be polled for.
4835 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004836 * @param timeout Non-negative waiting period for an event to be ready (in
4837 * milliseconds), or one of the special values K_NO_WAIT and
4838 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004839 *
4840 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004841 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004842 * @retval -EINTR Polling has been interrupted, e.g. with
4843 * k_queue_cancel_wait(). All output events are still set and valid,
4844 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4845 * words, -EINTR status means that at least one of output events is
4846 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004847 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4848 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004849 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004850 */
4851
Andrew Boie3772f772018-05-07 16:52:57 -07004852__syscall int k_poll(struct k_poll_event *events, int num_events,
4853 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004854
4855/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004856 * @brief Initialize a poll signal object.
4857 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004858 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004859 *
4860 * @param signal A poll signal.
4861 *
4862 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004863 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004864 */
4865
Andrew Boie3772f772018-05-07 16:52:57 -07004866__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4867
4868/*
4869 * @brief Reset a poll signal object's state to unsignaled.
4870 *
4871 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004872 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004873 */
4874__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4875
Patrik Flykt4344e272019-03-08 14:19:05 -07004876static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004877{
Patrik Flykt24d71432019-03-26 19:57:45 -06004878 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004879}
4880
4881/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004882 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004883 *
4884 * @param signal A poll signal object
4885 * @param signaled An integer buffer which will be written nonzero if the
4886 * object was signaled
4887 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004888 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004889 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004890 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004891 */
4892__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4893 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004894
4895/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004896 * @brief Signal a poll signal object.
4897 *
4898 * This routine makes ready a poll signal, which is basically a poll event of
4899 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4900 * made ready to run. A @a result value can be specified.
4901 *
4902 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004903 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004904 * k_poll_signal_reset(). It thus has to be reset by the user before being
4905 * passed again to k_poll() or k_poll() will consider it being signaled, and
4906 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004907 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004908 * @note The result is stored and the 'signaled' field is set even if
4909 * this function returns an error indicating that an expiring poll was
4910 * not notified. The next k_poll() will detect the missed raise.
4911 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004912 * @param signal A poll signal.
4913 * @param result The value to store in the result field of the signal.
4914 *
4915 * @retval 0 The signal was delivered successfully.
4916 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004917 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004918 */
4919
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004920__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004921
Anas Nashif954d5502018-02-25 08:37:28 -06004922/**
4923 * @internal
4924 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004925extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004926
Anas Nashif166f5192018-02-25 08:02:36 -06004927/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004928
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004929/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004930 * @defgroup cpu_idle_apis CPU Idling APIs
4931 * @ingroup kernel_apis
4932 * @{
4933 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004934/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004935 * @brief Make the CPU idle.
4936 *
4937 * This function makes the CPU idle until an event wakes it up.
4938 *
4939 * In a regular system, the idle thread should be the only thread responsible
4940 * for making the CPU idle and triggering any type of power management.
4941 * However, in some more constrained systems, such as a single-threaded system,
4942 * the only thread would be responsible for this if needed.
4943 *
4944 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004945 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004946 */
Andrew Boie07525a32019-09-21 16:17:23 -07004947static inline void k_cpu_idle(void)
4948{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004949 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004950}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004951
4952/**
4953 * @brief Make the CPU idle in an atomic fashion.
4954 *
4955 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4956 * must be done atomically before making the CPU idle.
4957 *
4958 * @param key Interrupt locking key obtained from irq_lock().
4959 *
4960 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004961 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004962 */
Andrew Boie07525a32019-09-21 16:17:23 -07004963static inline void k_cpu_atomic_idle(unsigned int key)
4964{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004965 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004966}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004967
Anas Nashif30c3cff2019-01-22 08:18:13 -05004968/**
4969 * @}
4970 */
Anas Nashif954d5502018-02-25 08:37:28 -06004971
4972/**
4973 * @internal
4974 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004975extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004976
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004977#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004978/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004979#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004980#else
4981
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004982#if !defined(CONFIG_ASSERT_NO_FILE_INFO)
4983#define __EXCEPT_LOC() __ASSERT_PRINT("@ %s:%d\n", __FILE__, __LINE__)
4984#else
4985#define __EXCEPT_LOC()
4986#endif
4987
Andrew Boiecdb94d62017-04-18 15:22:05 -07004988/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004989 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004990 *
4991 * We won't have a real exception frame to determine the PC value when
4992 * the oops occurred, so print file and line number before we jump into
4993 * the fatal error handler.
4994 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004995#define z_except_reason(reason) do { \
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004996 __EXCEPT_LOC(); \
Andrew Boie56236372019-07-15 15:22:29 -07004997 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004998 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004999
5000#endif /* _ARCH__EXCEPT */
5001
5002/**
5003 * @brief Fatally terminate a thread
5004 *
5005 * This should be called when a thread has encountered an unrecoverable
5006 * runtime condition and needs to terminate. What this ultimately
5007 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005008 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005009 *
5010 * If this is called from ISR context, the default system fatal error handler
5011 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005012 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07005013 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005014#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005015
5016/**
5017 * @brief Fatally terminate the system
5018 *
5019 * This should be called when the Zephyr kernel has encountered an
5020 * unrecoverable runtime condition and needs to terminate. What this ultimately
5021 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005022 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005023 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07005024 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005025#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005026
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005027/*
5028 * private APIs that are utilized by one or more public APIs
5029 */
5030
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09005031/**
5032 * @internal
5033 */
5034extern void z_init_thread_base(struct _thread_base *thread_base,
5035 int priority, u32_t initial_state,
5036 unsigned int options);
5037
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005038#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06005039/**
5040 * @internal
5041 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005042extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005043#else
Anas Nashif954d5502018-02-25 08:37:28 -06005044/**
5045 * @internal
5046 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005047#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005048#endif
5049
Anas Nashif954d5502018-02-25 08:37:28 -06005050/**
5051 * @internal
5052 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005053extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06005054/**
5055 * @internal
5056 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005057extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005058
Andrew Boiedc5d9352017-06-02 12:56:47 -07005059/* arch/cpu.h may declare an architecture or platform-specific macro
5060 * for properly declaring stacks, compatible with MMU/MPU constraints if
5061 * enabled
5062 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07005063
5064/**
5065 * @brief Obtain an extern reference to a stack
5066 *
5067 * This macro properly brings the symbol of a thread stack declared
5068 * elsewhere into scope.
5069 *
5070 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005071 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07005072 */
5073#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
5074
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005075#ifdef ARCH_THREAD_STACK_DEFINE
5076#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07005077#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005078 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
5079#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
5080#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
5081#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boieb5c68102019-11-21 17:38:17 -08005082#define K_THREAD_STACK_RESERVED ((size_t)ARCH_THREAD_STACK_RESERVED)
Andrew Boie4e5c0932019-04-04 12:05:28 -07005083static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005084{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005085 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07005086}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005087#else
5088/**
5089 * @brief Declare a toplevel thread stack memory region
5090 *
5091 * This declares a region of memory suitable for use as a thread's stack.
5092 *
5093 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5094 * 'noinit' section so that it isn't zeroed at boot
5095 *
Andrew Boie507852a2017-07-25 18:47:07 -07005096 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04005097 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07005098 * inside needs to be examined, examine thread->stack_info for the associated
5099 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005100 *
5101 * It is legal to precede this definition with the 'static' keyword.
5102 *
5103 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
5104 * parameter of k_thread_create(), it may not be the same as the
5105 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
5106 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005107 * Some arches may round the size of the usable stack region up to satisfy
5108 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
5109 * size.
5110 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07005111 * @param sym Thread stack symbol name
5112 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005113 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005114 */
5115#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005116 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005117
5118/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305119 * @brief Calculate size of stacks to be allocated in a stack array
5120 *
5121 * This macro calculates the size to be allocated for the stacks
5122 * inside a stack array. It accepts the indicated "size" as a parameter
5123 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
5124 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
5125 *
5126 * @param size Size of the stack memory region
5127 * @req K-TSTACK-001
5128 */
5129#define K_THREAD_STACK_LEN(size) (size)
5130
5131/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07005132 * @brief Declare a toplevel array of thread stack memory regions
5133 *
5134 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
5135 * definition for additional details and constraints.
5136 *
5137 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5138 * 'noinit' section so that it isn't zeroed at boot
5139 *
5140 * @param sym Thread stack symbol name
5141 * @param nmemb Number of stacks to declare
5142 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005143 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005144 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07005145#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005146 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305147 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005148
5149/**
5150 * @brief Declare an embedded stack memory region
5151 *
5152 * Used for stacks embedded within other data structures. Use is highly
5153 * discouraged but in some cases necessary. For memory protection scenarios,
5154 * it is very important that any RAM preceding this member not be writable
5155 * by threads else a stack overflow will lead to silent corruption. In other
5156 * words, the containing data structure should live in RAM owned by the kernel.
5157 *
5158 * @param sym Thread stack symbol name
5159 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005160 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005161 */
5162#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005163 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005164
5165/**
5166 * @brief Return the size in bytes of a stack memory region
5167 *
5168 * Convenience macro for passing the desired stack size to k_thread_create()
5169 * since the underlying implementation may actually create something larger
5170 * (for instance a guard area).
5171 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005172 * The value returned here is not guaranteed to match the 'size' parameter
5173 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005174 *
5175 * @param sym Stack memory symbol
5176 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005177 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005178 */
5179#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5180
Andrew Boie575abc02019-03-19 10:42:24 -07005181
5182/**
5183 * @brief Indicate how much additional memory is reserved for stack objects
5184 *
5185 * Any given stack declaration may have additional memory in it for guard
5186 * areas or supervisor mode stacks. This macro indicates how much space
5187 * is reserved for this. The memory reserved may not be contiguous within
5188 * the stack object, and does not account for additional space used due to
5189 * enforce alignment.
5190 */
Andrew Boieb5c68102019-11-21 17:38:17 -08005191#define K_THREAD_STACK_RESERVED ((size_t)0U)
Andrew Boie575abc02019-03-19 10:42:24 -07005192
Andrew Boiedc5d9352017-06-02 12:56:47 -07005193/**
5194 * @brief Get a pointer to the physical stack buffer
5195 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005196 * This macro is deprecated. If a stack buffer needs to be examined, the
5197 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005198 *
5199 * @param sym Declared stack symbol name
5200 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005201 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005202 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005203static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005204{
5205 return (char *)sym;
5206}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005207
5208#endif /* _ARCH_DECLARE_STACK */
5209
Chunlin Hane9c97022017-07-07 20:29:30 +08005210/**
5211 * @defgroup mem_domain_apis Memory domain APIs
5212 * @ingroup kernel_apis
5213 * @{
5214 */
5215
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005216/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005217 * @def K_MEM_PARTITION_DEFINE
5218 * @brief Used to declare a memory partition
5219 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005220 */
5221#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5222#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5223 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005224 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005225 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005226#else
5227#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005228 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005229 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005230#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5231
Chunlin Hane9c97022017-07-07 20:29:30 +08005232/* memory partition */
5233struct k_mem_partition {
Anas Nashife71293e2019-12-04 20:00:14 -05005234 /** start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005235 uintptr_t start;
Anas Nashife71293e2019-12-04 20:00:14 -05005236 /** size of memory partition */
Andrew Boiea8248212019-11-13 12:10:56 -08005237 size_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005238#if defined(CONFIG_MEMORY_PROTECTION)
Anas Nashife71293e2019-12-04 20:00:14 -05005239 /** attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305240 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005241#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005242};
5243
Anas Nashife71293e2019-12-04 20:00:14 -05005244/**
5245 * @brief Memory Domain
5246 *
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305247 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005248struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305249#ifdef CONFIG_USERSPACE
Anas Nashife71293e2019-12-04 20:00:14 -05005250 /** partitions in the domain */
Chunlin Hane9c97022017-07-07 20:29:30 +08005251 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305252#endif /* CONFIG_USERSPACE */
Anas Nashife71293e2019-12-04 20:00:14 -05005253 /** domain q */
Chunlin Hane9c97022017-07-07 20:29:30 +08005254 sys_dlist_t mem_domain_q;
Anas Nashife71293e2019-12-04 20:00:14 -05005255 /** number of partitions in the domain */
Leandro Pereira08de6582018-02-28 14:22:57 -08005256 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005257};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305258
Chunlin Hane9c97022017-07-07 20:29:30 +08005259
5260/**
5261 * @brief Initialize a memory domain.
5262 *
5263 * Initialize a memory domain with given name and memory partitions.
5264 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005265 * See documentation for k_mem_domain_add_partition() for details about
5266 * partition constraints.
5267 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005268 * @param domain The memory domain to be initialized.
5269 * @param num_parts The number of array items of "parts" parameter.
5270 * @param parts An array of pointers to the memory partitions. Can be NULL
5271 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005272 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005273 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005274extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005275 struct k_mem_partition *parts[]);
5276/**
5277 * @brief Destroy a memory domain.
5278 *
5279 * Destroy a memory domain.
5280 *
5281 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005282 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005283 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005284extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5285
5286/**
5287 * @brief Add a memory partition into a memory domain.
5288 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005289 * Add a memory partition into a memory domain. Partitions must conform to
5290 * the following constraints:
5291 *
5292 * - Partition bounds must be within system RAM boundaries on MMU-based
5293 * systems.
5294 * - Partitions in the same memory domain may not overlap each other.
5295 * - Partitions must not be defined which expose private kernel
5296 * data structures or kernel objects.
5297 * - The starting address alignment, and the partition size must conform to
5298 * the constraints of the underlying memory management hardware, which
5299 * varies per architecture.
5300 * - Memory domain partitions are only intended to control access to memory
5301 * from user mode threads.
5302 *
5303 * Violating these constraints may lead to CPU exceptions or undefined
5304 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005305 *
5306 * @param domain The memory domain to be added a memory partition.
5307 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005308 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005309 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005310extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5311 struct k_mem_partition *part);
5312
5313/**
5314 * @brief Remove a memory partition from a memory domain.
5315 *
5316 * Remove a memory partition from a memory domain.
5317 *
5318 * @param domain The memory domain to be removed a memory partition.
5319 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005320 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005321 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005322extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5323 struct k_mem_partition *part);
5324
5325/**
5326 * @brief Add a thread into a memory domain.
5327 *
5328 * Add a thread into a memory domain.
5329 *
5330 * @param domain The memory domain that the thread is going to be added into.
5331 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005332 *
5333 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005334 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005335extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5336 k_tid_t thread);
5337
5338/**
5339 * @brief Remove a thread from its memory domain.
5340 *
5341 * Remove a thread from its memory domain.
5342 *
5343 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005344 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005345 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005346extern void k_mem_domain_remove_thread(k_tid_t thread);
5347
Anas Nashif166f5192018-02-25 08:02:36 -06005348/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005349
Andrew Boied76ae462020-01-02 11:57:43 -08005350#ifdef CONFIG_PRINTK
Andrew Boie756f9072017-10-10 16:01:49 -07005351/**
5352 * @brief Emit a character buffer to the console device
5353 *
5354 * @param c String of characters to print
5355 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005356 *
5357 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005358 */
5359__syscall void k_str_out(char *c, size_t n);
Andrew Boied76ae462020-01-02 11:57:43 -08005360#endif
Andrew Boie756f9072017-10-10 16:01:49 -07005361
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005362/**
5363 * @brief Disable preservation of floating point context information.
5364 *
5365 * This routine informs the kernel that the specified thread
5366 * will no longer be using the floating point registers.
5367 *
5368 * @warning
5369 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005370 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005371 *
5372 * @warning
5373 * This routine should only be used to disable floating point support for
5374 * a thread that currently has such support enabled.
5375 *
5376 * @param thread ID of thread.
5377 *
5378 * @retval 0 On success.
5379 * @retval -ENOSYS If the floating point disabling is not implemented.
5380 * -EINVAL If the floating point disabling could not be performed.
5381 */
5382__syscall int k_float_disable(struct k_thread *thread);
5383
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005384#ifdef __cplusplus
5385}
5386#endif
5387
Anas Nashif73008b42020-02-06 09:14:51 -05005388#include <tracing/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005389#include <syscalls/kernel.h>
5390
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005391#endif /* !_ASMLANGUAGE */
5392
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005393#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */