blob: f51fd3046bbfa0d9c4127f682df28eb42b8a72ac [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070019#include <stdbool.h>
Stephanos Ioannidis33fbe002019-09-09 21:26:59 +090020#include <toolchain.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
Anas Nashifbbb157d2017-01-15 08:46:31 -050026/**
27 * @brief Kernel APIs
28 * @defgroup kernel_apis Kernel APIs
29 * @{
30 * @}
31 */
32
Anas Nashif61f4b242016-11-18 10:53:59 -050033#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
35#else
36#define K_DEBUG(fmt, ...)
37#endif
38
Benjamin Walsh2f280412017-01-14 19:23:46 -050039#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
40#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
41#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
42#elif defined(CONFIG_COOP_ENABLED)
43#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
44#define _NUM_PREEMPT_PRIO (0)
45#elif defined(CONFIG_PREEMPT_ENABLED)
46#define _NUM_COOP_PRIO (0)
47#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
48#else
49#error "invalid configuration"
50#endif
51
52#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_PRIO_PREEMPT(x) (x)
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#define K_ANY NULL
56#define K_END NULL
57
Benjamin Walshedb35702017-01-14 18:47:22 -050058#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050060#elif defined(CONFIG_COOP_ENABLED)
61#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
62#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050064#else
65#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#endif
67
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050068#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
70#else
71#define K_LOWEST_THREAD_PRIO -1
72#endif
73
Benjamin Walshfab8d922016-11-08 15:36:36 -050074#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
75
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
77#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
78
Andy Ross225c74b2018-06-27 11:20:50 -070079#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070080
81typedef struct {
82 struct _priq_rb waitq;
83} _wait_q_t;
84
Patrik Flykt4344e272019-03-08 14:19:05 -070085extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070086
Patrik Flykt4344e272019-03-08 14:19:05 -070087#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070088
89#else
90
Andy Rossccf3bf72018-05-10 11:10:34 -070091typedef struct {
92 sys_dlist_t waitq;
93} _wait_q_t;
94
Patrik Flykt4344e272019-03-08 14:19:05 -070095#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Andy Ross1acd8c22018-05-03 14:51:49 -070097#endif
98
Anas Nashif2f203c22016-12-18 06:57:45 -050099#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800100#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +0800101#define _OBJECT_TRACING_LINKED_FLAG u8_t __linked;
102#define _OBJECT_TRACING_INIT \
103 .__next = NULL, \
104 .__linked = 0,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500106#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800107#define _OBJECT_TRACING_NEXT_PTR(type)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +0800108#define _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400109#endif
110
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300112#define _POLL_EVENT_OBJ_INIT(obj) \
113 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
114#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500115#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300116#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500117#define _POLL_EVENT
118#endif
119
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500120struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mutex;
122struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_msgq;
124struct k_mbox;
125struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200126struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127struct k_fifo;
128struct k_lifo;
129struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400130struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400131struct k_mem_pool;
132struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500133struct k_poll_event;
134struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800135struct k_mem_domain;
136struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800137struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400138
Anas Nashife71293e2019-12-04 20:00:14 -0500139/**
140 * @brief Kernel Object Types
141 *
142 * This enumeration needs to be kept in sync with the lists of kernel objects
Andrew Boie5bd891d2017-09-27 12:59:28 -0700143 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
144 * function in kernel/userspace.c
145 */
Andrew Boie945af952017-08-22 13:15:23 -0700146enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700147 K_OBJ_ANY,
148
Leandro Pereirac2003672018-04-04 13:50:32 -0700149 /** @cond
150 * Doxygen should ignore this build-time generated include file
151 * when genrating API documentation. Enumeration values are
152 * generated during build by gen_kobject_list.py. It includes
153 * basic kernel objects (e.g. pipes and mutexes) and driver types.
154 */
155#include <kobj-types-enum.h>
156 /** @endcond
157 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700158
Andrew Boie945af952017-08-22 13:15:23 -0700159 K_OBJ_LAST
160};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500161/**
162 * @defgroup usermode_apis User Mode APIs
163 * @ingroup kernel_apis
164 * @{
165 */
Andrew Boie945af952017-08-22 13:15:23 -0700166
167#ifdef CONFIG_USERSPACE
168/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700169 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700170struct _k_object {
Andrew Boie22553a72019-11-19 18:27:42 -0800171 void *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700172 u8_t perms[CONFIG_MAX_THREAD_BYTES];
173 u8_t type;
174 u8_t flags;
Andrew Boiee48ed6a2019-11-13 12:52:00 -0800175 uintptr_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700176} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700177
Andrew Boie877f82e2017-10-17 11:20:22 -0700178struct _k_object_assignment {
179 struct k_thread *thread;
180 void * const *objects;
181};
182
183/**
184 * @brief Grant a static thread access to a list of kernel objects
185 *
186 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
187 * a set of kernel objects. These objects do not need to be in an initialized
188 * state. The permissions will be granted when the threads are initialized
189 * in the early boot sequence.
190 *
191 * All arguments beyond the first must be pointers to kernel objects.
192 *
193 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
194 */
195#define K_THREAD_ACCESS_GRANT(name_, ...) \
196 static void * const _CONCAT(_object_list_, name_)[] = \
197 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400198 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
199 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700200 { (&_k_thread_obj_ ## name_), \
201 (_CONCAT(_object_list_, name_)) }
202
Anas Nashife71293e2019-12-04 20:00:14 -0500203/** Object initialized */
Andrew Boie945af952017-08-22 13:15:23 -0700204#define K_OBJ_FLAG_INITIALIZED BIT(0)
Anas Nashife71293e2019-12-04 20:00:14 -0500205/** Object is Public */
Andrew Boie04caa672017-10-13 13:57:07 -0700206#define K_OBJ_FLAG_PUBLIC BIT(1)
Anas Nashife71293e2019-12-04 20:00:14 -0500207/** Object allocated */
Andrew Boie97bf0012018-04-24 17:01:37 -0700208#define K_OBJ_FLAG_ALLOC BIT(2)
Anas Nashife71293e2019-12-04 20:00:14 -0500209/** Driver Object */
Andrew Boie78757072019-07-23 13:29:30 -0700210#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700211
212/**
213 * Lookup a kernel object and init its metadata if it exists
214 *
215 * Calling this on an object will make it usable from userspace.
216 * Intended to be called as the last statement in kernel object init
217 * functions.
218 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500219 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700220 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700221void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700222#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700223/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700224#define K_THREAD_ACCESS_GRANT(thread, ...)
225
Anas Nashif954d5502018-02-25 08:37:28 -0600226/**
227 * @internal
228 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700229static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700230{
231 ARG_UNUSED(obj);
232}
233
Anas Nashif954d5502018-02-25 08:37:28 -0600234/**
235 * @internal
236 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700237static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700238 struct k_thread *thread)
239{
240 ARG_UNUSED(object);
241 ARG_UNUSED(thread);
242}
243
Anas Nashif954d5502018-02-25 08:37:28 -0600244/**
245 * @internal
246 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700247static inline void k_object_access_revoke(void *object,
248 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700249{
250 ARG_UNUSED(object);
251 ARG_UNUSED(thread);
252}
253
Andrew Boiee9cfc542018-04-13 13:15:28 -0700254/**
255 * @internal
256 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700257static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700258{
259 ARG_UNUSED(object);
260}
261
Andrew Boie41bab6e2017-10-14 14:42:23 -0700262static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700263{
264 ARG_UNUSED(object);
265}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700266/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700267#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700268
269/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600270 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700271 *
272 * The thread will be granted access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700274 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700275 *
276 * @param object Address of kernel object
277 * @param thread Thread to grant access to the object
278 */
Andrew Boie743e4682017-10-04 12:25:50 -0700279__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700280
Andrew Boiea89bf012017-10-09 14:47:55 -0700281/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600282 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700283 *
284 * The thread will lose access to the object if the caller is from
285 * supervisor mode, or the caller is from user mode AND has permissions
286 * on both the object and the thread whose access is being revoked.
287 *
288 * @param object Address of kernel object
289 * @param thread Thread to remove access to the object
290 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700291void k_object_access_revoke(void *object, struct k_thread *thread);
292
Anas Nashife71293e2019-12-04 20:00:14 -0500293/**
294 * @brief Release an object
295 *
296 * Allows user threads to drop their own permission on an object
297 * Their permissions are automatically cleared when a thread terminates.
298 *
299 * @param object The object to be released
300 *
301 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700302__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700303
304/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600305 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700306 *
307 * If the caller is from supervisor mode, or the caller is from user mode and
308 * have sufficient permissions on the object, then that object will have
309 * permissions granted to it for *all* current and future threads running in
310 * the system, effectively becoming a public kernel object.
311 *
312 * Use of this API should be avoided on systems that are running untrusted code
313 * as it is possible for such code to derive the addresses of kernel objects
314 * and perform unwanted operations on them.
315 *
Andrew Boie04caa672017-10-13 13:57:07 -0700316 * It is not possible to revoke permissions on public objects; once public,
317 * any thread may use it.
318 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700319 * @param object Address of kernel object
320 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700321void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700322
Andrew Boie31bdfc02017-11-08 16:38:03 -0800323/**
324 * Allocate a kernel object of a designated type
325 *
326 * This will instantiate at runtime a kernel object of the specified type,
327 * returning a pointer to it. The object will be returned in an uninitialized
328 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700329 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800330 *
331 * Currently, allocation of thread stacks is not supported.
332 *
333 * @param otype Requested kernel object type
334 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
335 * available
336 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700337__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338
Andrew Boie97bf0012018-04-24 17:01:37 -0700339#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800340/**
341 * Free a kernel object previously allocated with k_object_alloc()
342 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700343 * This will return memory for a kernel object back to resource pool it was
344 * allocated from. Care must be exercised that the object will not be used
345 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800346 *
347 * @param obj Pointer to the kernel object memory address.
348 */
349void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700350#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700351/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700352static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700353{
Kumar Gala85699f72018-05-17 09:26:03 -0500354 ARG_UNUSED(otype);
355
Andrew Boie97bf0012018-04-24 17:01:37 -0700356 return NULL;
357}
Anas Nashife71293e2019-12-04 20:00:14 -0500358/**
359 * @brief Free an object
360 *
361 * @param obj
362 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700363static inline void k_obj_free(void *obj)
364{
365 ARG_UNUSED(obj);
366}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700367/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800368#endif /* CONFIG_DYNAMIC_OBJECTS */
369
Anas Nashif4bcb2942019-01-23 23:06:29 -0500370/** @} */
371
Andrew Boiebca15da2017-10-15 14:17:48 -0700372/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700373 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700374 *
375 * The purpose of this data type is to clearly distinguish between the
376 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
377 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400378 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700379 * stack buffer region with guard areas that trigger a MPU or MMU fault
380 * if written to.
381 *
382 * APIs that want to work with the buffer inside should continue to use
383 * char *.
384 *
385 * Stacks should always be created with K_THREAD_STACK_DEFINE().
386 */
387struct __packed _k_thread_stack_element {
388 char data;
389};
Daniel Leung7476a6e2019-11-25 13:58:40 -0800390
391/**
392 * @typedef k_thread_stack_t
393 * @brief Typedef of struct _k_thread_stack_element
394 *
395 * @see _k_thread_stack_element
396 */
Andrew Boiebca15da2017-10-15 14:17:48 -0700397
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700398/**
399 * @typedef k_thread_entry_t
400 * @brief Thread entry point function type.
401 *
402 * A thread's entry point function is invoked when the thread starts executing.
403 * Up to 3 argument values can be passed to the function.
404 *
405 * The thread terminates execution permanently if the entry point function
406 * returns. The thread is responsible for releasing any shared resources
407 * it may own (such as mutexes and dynamically allocated memory), prior to
408 * returning.
409 *
410 * @param p1 First argument.
411 * @param p2 Second argument.
412 * @param p3 Third argument.
413 *
414 * @return N/A
415 */
Andrew Boie73abd322017-04-04 13:19:13 -0700416
417#ifdef CONFIG_THREAD_MONITOR
418struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700419 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700420 void *parameter1;
421 void *parameter2;
422 void *parameter3;
423};
424#endif
425
426/* can be used for creating 'dummy' threads, e.g. for pending on objects */
427struct _thread_base {
428
429 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700430 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600431 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700432 struct rbnode qnode_rb;
433 };
434
Andy Ross1acd8c22018-05-03 14:51:49 -0700435 /* wait queue on which the thread is pended (needed only for
436 * trees, not dumb lists)
437 */
438 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700439
440 /* user facing 'thread options'; values defined in include/kernel.h */
441 u8_t user_options;
442
443 /* thread state */
444 u8_t thread_state;
445
446 /*
447 * scheduler lock count and thread priority
448 *
449 * These two fields control the preemptibility of a thread.
450 *
451 * When the scheduler is locked, sched_locked is decremented, which
452 * means that the scheduler is locked for values from 0xff to 0x01. A
453 * thread is coop if its prio is negative, thus 0x80 to 0xff when
454 * looked at the value as unsigned.
455 *
456 * By putting them end-to-end, this means that a thread is
457 * non-preemptible if the bundled value is greater than or equal to
458 * 0x0080.
459 */
460 union {
461 struct {
462#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
463 u8_t sched_locked;
464 s8_t prio;
465#else /* LITTLE and PDP */
466 s8_t prio;
467 u8_t sched_locked;
468#endif
469 };
470 u16_t preempt;
471 };
472
Andy Ross4a2e50f2018-05-15 11:06:25 -0700473#ifdef CONFIG_SCHED_DEADLINE
474 int prio_deadline;
475#endif
476
Andy Ross1acd8c22018-05-03 14:51:49 -0700477 u32_t order_key;
478
Andy Ross2724fd12018-01-29 14:55:20 -0800479#ifdef CONFIG_SMP
480 /* True for the per-CPU idle threads */
481 u8_t is_idle;
482
Andy Ross2724fd12018-01-29 14:55:20 -0800483 /* CPU index on which thread was last run */
484 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700485
486 /* Recursive count of irq_lock() calls */
487 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800488
489#endif
490
491#ifdef CONFIG_SCHED_CPU_MASK
492 /* "May run on" bits for each CPU */
493 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800494#endif
495
Andrew Boie73abd322017-04-04 13:19:13 -0700496 /* data returned by APIs */
497 void *swap_data;
498
499#ifdef CONFIG_SYS_CLOCK_EXISTS
500 /* this thread's entry in a timeout queue */
501 struct _timeout timeout;
502#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700503};
504
505typedef struct _thread_base _thread_base_t;
506
507#if defined(CONFIG_THREAD_STACK_INFO)
508/* Contains the stack information of a thread */
509struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700510 /* Stack start - Represents the start address of the thread-writable
511 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700512 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400513 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700514
515 /* Stack Size - Thread writable stack buffer size. Represents
516 * the size of the actual area, starting from the start member,
517 * that should be writable by the thread
518 */
Andrew Boie528233e2019-12-11 14:54:15 -0800519 size_t size;
Andrew Boie73abd322017-04-04 13:19:13 -0700520};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700521
522typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700523#endif /* CONFIG_THREAD_STACK_INFO */
524
Chunlin Hane9c97022017-07-07 20:29:30 +0800525#if defined(CONFIG_USERSPACE)
526struct _mem_domain_info {
Anas Nashife71293e2019-12-04 20:00:14 -0500527 /** memory domain queue node */
Chunlin Hane9c97022017-07-07 20:29:30 +0800528 sys_dnode_t mem_domain_q_node;
Anas Nashife71293e2019-12-04 20:00:14 -0500529 /** memory domain of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800530 struct k_mem_domain *mem_domain;
531};
532
533#endif /* CONFIG_USERSPACE */
534
Daniel Leungfc182432018-08-16 15:42:28 -0700535#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
536struct _thread_userspace_local_data {
537 int errno_var;
538};
539#endif
540
Anas Nashifce78d162018-05-24 12:43:11 -0500541/**
542 * @ingroup thread_apis
543 * Thread Structure
544 */
Andrew Boie73abd322017-04-04 13:19:13 -0700545struct k_thread {
546
547 struct _thread_base base;
548
Anas Nashifce78d162018-05-24 12:43:11 -0500549 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700550 struct _callee_saved callee_saved;
551
Anas Nashifce78d162018-05-24 12:43:11 -0500552 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700553 void *init_data;
554
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /**
556 * abort function
557 * @req K-THREAD-002
558 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700559 void (*fn_abort)(void);
560
561#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700563 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700564
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700566 struct k_thread *next_thread;
567#endif
568
Anas Nashif57554052018-03-03 02:31:05 -0600569#if defined(CONFIG_THREAD_NAME)
Anas Nashife71293e2019-12-04 20:00:14 -0500570 /** Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700571 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600572#endif
573
Andrew Boie73abd322017-04-04 13:19:13 -0700574#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500575 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700576 void *custom_data;
577#endif
578
Daniel Leungfc182432018-08-16 15:42:28 -0700579#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
580 struct _thread_userspace_local_data *userspace_local_data;
581#endif
582
Andrew Boie73abd322017-04-04 13:19:13 -0700583#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700584#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700586 int errno_var;
587#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700588#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700589
590#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500591 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700592 struct _thread_stack_info stack_info;
593#endif /* CONFIG_THREAD_STACK_INFO */
594
Chunlin Hane9c97022017-07-07 20:29:30 +0800595#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500596 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800597 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500598 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700599 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800600#endif /* CONFIG_USERSPACE */
601
Andy Ross042d8ec2017-12-09 08:37:20 -0800602#if defined(CONFIG_USE_SWITCH)
603 /* When using __switch() a few previously arch-specific items
604 * become part of the core OS
605 */
606
Patrik Flykt4344e272019-03-08 14:19:05 -0700607 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800608 int swap_retval;
609
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800610 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800611 void *switch_handle;
612#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500613 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700614 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800615
Anas Nashifce78d162018-05-24 12:43:11 -0500616 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700617 struct _thread_arch arch;
618};
619
620typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400621typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400623enum execution_context_types {
624 K_ISR = 0,
625 K_COOP_THREAD,
626 K_PREEMPT_THREAD,
627};
628
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400629/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500630 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100631 * @{
632 */
Anas Nashife71293e2019-12-04 20:00:14 -0500633
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530634typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
635 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100636
637/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530638 * @brief Iterate over all the threads in the system.
639 *
640 * This routine iterates over all the threads in the system and
641 * calls the user_cb function for each thread.
642 *
643 * @param user_cb Pointer to the user callback function.
644 * @param user_data Pointer to user data.
645 *
646 * @note CONFIG_THREAD_MONITOR must be set for this function
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100647 * to be effective.
648 * @note This API uses @ref k_spin_lock to protect the _kernel.threads
649 * list which means creation of new threads and terminations of existing
650 * threads are blocked until this API returns.
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530651 *
652 * @return N/A
653 */
654extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
655
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100656/**
657 * @brief Iterate over all the threads in the system without locking.
658 *
659 * This routine works exactly the same like @ref k_thread_foreach
660 * but unlocks interrupts when user_cb is executed.
661 *
662 * @param user_cb Pointer to the user callback function.
663 * @param user_data Pointer to user data.
664 *
665 * @note CONFIG_THREAD_MONITOR must be set for this function
666 * to be effective.
667 * @note This API uses @ref k_spin_lock only when accessing the _kernel.threads
668 * queue elements. It unlocks it during user callback function processing.
669 * If a new task is created when this @c foreach function is in progress,
670 * the added new task would not be included in the enumeration.
671 * If a task is aborted during this enumeration, there would be a race here
672 * and there is a possibility that this aborted task would be included in the
673 * enumeration.
674 * @note If the task is aborted and the memory occupied by its @c k_thread
675 * structure is reused when this @c k_thread_foreach_unlocked is in progress
676 * it might even lead to the system behave unstable.
677 * This function may never return, as it would follow some @c next task
678 * pointers treating given pointer as a pointer to the k_thread structure
679 * while it is something different right now.
680 * Do not reuse the memory that was occupied by k_thread structure of aborted
681 * task if it was aborted after this function was called in any context.
682 */
683extern void k_thread_foreach_unlocked(
684 k_thread_user_cb_t user_cb, void *user_data);
685
Anas Nashif166f5192018-02-25 08:02:36 -0600686/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100687
688/**
Allan Stephensc98da842016-11-11 15:45:03 -0500689 * @defgroup thread_apis Thread APIs
690 * @ingroup kernel_apis
691 * @{
692 */
693
Benjamin Walshed240f22017-01-22 13:05:08 -0500694#endif /* !_ASMLANGUAGE */
695
696
697/*
698 * Thread user options. May be needed by assembly code. Common part uses low
699 * bits, arch-specific use high bits.
700 */
701
Anas Nashifa541e932018-05-24 11:19:16 -0500702/**
703 * @brief system thread that must not abort
704 * @req K-THREAD-000
705 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700706#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500707
708#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500709/**
710 * @brief thread uses floating point registers
711 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700712#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500713#endif
714
Anas Nashifa541e932018-05-24 11:19:16 -0500715/**
716 * @brief user mode thread
717 *
718 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700719 * has additional restrictions
720 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700721#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700722
Anas Nashifa541e932018-05-24 11:19:16 -0500723/**
724 * @brief Inherit Permissions
725 *
726 * @details
727 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700728 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
729 * is not enabled.
730 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700731#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700732
Benjamin Walshed240f22017-01-22 13:05:08 -0500733#ifdef CONFIG_X86
734/* x86 Bitmask definitions for threads user options */
735
736#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
737/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700738#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500739#endif
740#endif
741
742/* end - thread options */
743
744#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400745/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700746 * @brief Create a thread.
747 *
748 * This routine initializes a thread, then schedules it for execution.
749 *
750 * The new thread may be scheduled for immediate execution or a delayed start.
751 * If the newly spawned thread does not have a delayed start the kernel
752 * scheduler may preempt the current thread to allow the new thread to
753 * execute.
754 *
755 * Thread options are architecture-specific, and can include K_ESSENTIAL,
756 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
757 * them using "|" (the logical OR operator).
758 *
759 * Historically, users often would use the beginning of the stack memory region
760 * to store the struct k_thread data, although corruption will occur if the
761 * stack overflows this region and stack protection features may not detect this
762 * situation.
763 *
764 * @param new_thread Pointer to uninitialized struct k_thread
765 * @param stack Pointer to the stack space.
766 * @param stack_size Stack size in bytes.
767 * @param entry Thread entry function.
768 * @param p1 1st entry point parameter.
769 * @param p2 2nd entry point parameter.
770 * @param p3 3rd entry point parameter.
771 * @param prio Thread priority.
772 * @param options Thread options.
773 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
774 *
775 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400776 *
777 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700778 */
Andrew Boie662c3452017-10-02 10:51:18 -0700779__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700780 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700781 size_t stack_size,
782 k_thread_entry_t entry,
783 void *p1, void *p2, void *p3,
784 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700785
Andrew Boie3f091b52017-08-30 14:34:14 -0700786/**
787 * @brief Drop a thread's privileges permanently to user mode
788 *
789 * @param entry Function to start executing from
790 * @param p1 1st entry point parameter
791 * @param p2 2nd entry point parameter
792 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400793 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700794 */
795extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
796 void *p1, void *p2,
797 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700798
Andrew Boied26cf2d2017-03-30 13:07:02 -0700799/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530800 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700801 *
802 * This is a convenience function. For the provided thread, grant access to
803 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700804 *
805 * The thread object must be initialized (i.e. running). The objects don't
806 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530807 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700808 *
809 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530810 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400811 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700812 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530813#define k_thread_access_grant(thread, ...) \
814 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700815
816/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700817 * @brief Assign a resource memory pool to a thread
818 *
819 * By default, threads have no resource pool assigned unless their parent
820 * thread has a resource pool, in which case it is inherited. Multiple
821 * threads may be assigned to the same memory pool.
822 *
823 * Changing a thread's resource pool will not migrate allocations from the
824 * previous pool.
825 *
826 * @param thread Target thread to assign a memory pool for resource requests,
827 * or NULL if the thread should no longer have a memory pool.
828 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400829 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700830 */
831static inline void k_thread_resource_pool_assign(struct k_thread *thread,
832 struct k_mem_pool *pool)
833{
834 thread->resource_pool = pool;
835}
836
837#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
838/**
839 * @brief Assign the system heap as a thread's resource pool
840 *
841 * Similar to k_thread_resource_pool_assign(), but the thread will use
842 * the kernel heap to draw memory.
843 *
844 * Use with caution, as a malicious thread could perform DoS attacks on the
845 * kernel heap.
846 *
847 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400848 *
849 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700850 */
851void k_thread_system_pool_assign(struct k_thread *thread);
852#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
853
854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700857 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700859 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200861 * @return Zero if the requested time has elapsed or the number of milliseconds
862 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700864__syscall s32_t k_sleep(s32_t ms);
865
866/**
867 * @brief Put the current thread to sleep with microsecond resolution.
868 *
869 * This function is unlikely to work as expected without kernel tuning.
870 * In particular, because the lower bound on the duration of a sleep is
871 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
872 * to achieve the resolution desired. The implications of doing this must
873 * be understood before attempting to use k_usleep(). Use with caution.
874 *
875 * @param us Number of microseconds to sleep.
876 *
877 * @return Zero if the requested time has elapsed or the number of microseconds
878 * left to sleep, if thread was woken up by \ref k_wakeup call.
879 */
880__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881
882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
885 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 * @return N/A
889 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800890__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891
892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400898 *
899 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400900 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 */
Andrew Boie468190a2017-09-29 14:00:48 -0700902__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903
904/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500907 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * If @a thread is not currently sleeping, the routine has no effect.
910 *
911 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400912 *
913 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400914 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 */
Andrew Boie468190a2017-09-29 14:00:48 -0700916__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917
918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500919 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400922 *
923 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700925__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400926
927/**
Allan Stephensc98da842016-11-11 15:45:03 -0500928 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500930 * This routine permanently stops execution of @a thread. The thread is taken
931 * off all kernel queues it is part of (i.e. the ready queue, the timeout
932 * queue, or a kernel object wait queue). However, any kernel resources the
933 * thread might currently own (such as mutexes or memory blocks) are not
934 * released. It is the responsibility of the caller of this routine to ensure
935 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 *
939 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400940 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400941 */
Andrew Boie468190a2017-09-29 14:00:48 -0700942__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400943
Andrew Boie7d627c52017-08-30 11:01:56 -0700944
945/**
946 * @brief Start an inactive thread
947 *
948 * If a thread was created with K_FOREVER in the delay parameter, it will
949 * not be added to the scheduling queue until this function is called
950 * on it.
951 *
952 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400953 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700954 */
Andrew Boie468190a2017-09-29 14:00:48 -0700955__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700956
Allan Stephensc98da842016-11-11 15:45:03 -0500957/**
958 * @cond INTERNAL_HIDDEN
959 */
960
Benjamin Walshd211a522016-12-06 11:44:01 -0500961/* timeout has timed out and is not on _timeout_q anymore */
962#define _EXPIRED (-2)
963
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400964struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700965 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700966 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400967 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700968 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500969 void *init_p1;
970 void *init_p2;
971 void *init_p3;
972 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500973 u32_t init_options;
974 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500975 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600976 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977};
978
Andrew Boied26cf2d2017-03-30 13:07:02 -0700979#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400980 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600981 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500982 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700983 .init_thread = (thread), \
984 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500985 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700986 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400987 .init_p1 = (void *)p1, \
988 .init_p2 = (void *)p2, \
989 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500990 .init_prio = (prio), \
991 .init_options = (options), \
992 .init_delay = (delay), \
993 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600994 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400995 }
996
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400997/**
Allan Stephensc98da842016-11-11 15:45:03 -0500998 * INTERNAL_HIDDEN @endcond
999 */
1000
1001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * @brief Statically define and initialize a thread.
1003 *
1004 * The thread may be scheduled for immediate execution or a delayed start.
1005 *
1006 * Thread options are architecture-specific, and can include K_ESSENTIAL,
1007 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
1008 * them using "|" (the logical OR operator).
1009 *
1010 * The ID of the thread can be accessed using:
1011 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001012 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 *
1014 * @param name Name of the thread.
1015 * @param stack_size Stack size in bytes.
1016 * @param entry Thread entry function.
1017 * @param p1 1st entry point parameter.
1018 * @param p2 2nd entry point parameter.
1019 * @param p3 3rd entry point parameter.
1020 * @param prio Thread priority.
1021 * @param options Thread options.
1022 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001023 *
Anas Nashif47420d02018-05-24 14:20:56 -04001024 * @req K-THREAD-010
1025 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001026 * @internal It has been observed that the x86 compiler by default aligns
1027 * these _static_thread_data structures to 32-byte boundaries, thereby
1028 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -04001029 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001030 */
Allan Stephens6cfe1322016-10-26 10:16:51 -05001031#define K_THREAD_DEFINE(name, stack_size, \
1032 entry, p1, p2, p3, \
1033 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -07001034 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001035 struct k_thread _k_thread_obj_##name; \
1036 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -07001037 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
1038 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001039 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -06001040 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001041 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001042
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * @param thread ID of thread whose priority is needed.
1049 *
1050 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001051 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001053__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 *
1060 * Rescheduling can occur immediately depending on the priority @a thread is
1061 * set to:
1062 *
1063 * - If its priority is raised above the priority of the caller of this
1064 * function, and the caller is preemptible, @a thread will be scheduled in.
1065 *
1066 * - If the caller operates on itself, it lowers its priority below that of
1067 * other threads in the system, and the caller is preemptible, the thread of
1068 * highest priority will be scheduled in.
1069 *
1070 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1071 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1072 * highest priority.
1073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 * @param prio New priority.
1076 *
1077 * @warning Changing the priority of a thread currently involved in mutex
1078 * priority inheritance may result in undefined behavior.
1079 *
1080 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001081 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 */
Andrew Boie468190a2017-09-29 14:00:48 -07001083__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001084
Andy Ross4a2e50f2018-05-15 11:06:25 -07001085
1086#ifdef CONFIG_SCHED_DEADLINE
1087/**
1088 * @brief Set deadline expiration time for scheduler
1089 *
1090 * This sets the "deadline" expiration as a time delta from the
1091 * current time, in the same units used by k_cycle_get_32(). The
1092 * scheduler (when deadline scheduling is enabled) will choose the
1093 * next expiring thread when selecting between threads at the same
1094 * static priority. Threads at different priorities will be scheduled
1095 * according to their static priority.
1096 *
1097 * @note Deadlines that are negative (i.e. in the past) are still seen
1098 * as higher priority than others, even if the thread has "finished"
1099 * its work. If you don't want it scheduled anymore, you have to
1100 * reset the deadline into the future, block/pend the thread, or
1101 * modify its priority with k_thread_priority_set().
1102 *
1103 * @note Despite the API naming, the scheduler makes no guarantees the
1104 * the thread WILL be scheduled within that deadline, nor does it take
1105 * extra metadata (like e.g. the "runtime" and "period" parameters in
1106 * Linux sched_setattr()) that allows the kernel to validate the
1107 * scheduling for achievability. Such features could be implemented
1108 * above this call, which is simply input to the priority selection
1109 * logic.
1110 *
Anas Nashif240c5162019-06-10 12:25:50 -04001111 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001112 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001113 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1114 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001115 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001116 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001117 * @param thread A thread on which to set the deadline
1118 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001119 *
1120 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001121 */
1122__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1123#endif
1124
Andy Rossab46b1b2019-01-30 15:00:42 -08001125#ifdef CONFIG_SCHED_CPU_MASK
1126/**
1127 * @brief Sets all CPU enable masks to zero
1128 *
1129 * After this returns, the thread will no longer be schedulable on any
1130 * CPUs. The thread must not be currently runnable.
1131 *
Anas Nashif240c5162019-06-10 12:25:50 -04001132 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001133 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001134 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1135 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001136 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001137 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001138 * @param thread Thread to operate upon
1139 * @return Zero on success, otherwise error code
1140 */
1141int k_thread_cpu_mask_clear(k_tid_t thread);
1142
1143/**
1144 * @brief Sets all CPU enable masks to one
1145 *
1146 * After this returns, the thread will be schedulable on any CPU. The
1147 * thread must not be currently runnable.
1148 *
Anas Nashif240c5162019-06-10 12:25:50 -04001149 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001150 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001151 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1152 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001153 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001154 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001155 * @param thread Thread to operate upon
1156 * @return Zero on success, otherwise error code
1157 */
1158int k_thread_cpu_mask_enable_all(k_tid_t thread);
1159
1160/**
1161 * @brief Enable thread to run on specified CPU
1162 *
1163 * The thread must not be currently runnable.
1164 *
Anas Nashif240c5162019-06-10 12:25:50 -04001165 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001166 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001167 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1168 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001169 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001170 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001171 * @param thread Thread to operate upon
1172 * @param cpu CPU index
1173 * @return Zero on success, otherwise error code
1174 */
1175int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1176
1177/**
1178 * @brief Prevent thread to run on specified CPU
1179 *
1180 * The thread must not be currently runnable.
1181 *
Anas Nashif240c5162019-06-10 12:25:50 -04001182 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001183 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001184 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1185 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001186 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001187 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001188 * @param thread Thread to operate upon
1189 * @param cpu CPU index
1190 * @return Zero on success, otherwise error code
1191 */
1192int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1193#endif
1194
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001196 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 *
Andy Ross50d09422019-11-19 11:20:07 -08001198 * This routine prevents the kernel scheduler from making @a thread
1199 * the current thread. All other internal operations on @a thread are
1200 * still performed; for example, kernel objects it is waiting on are
1201 * still handed to it. Note that any existing timeouts
1202 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1203 * will be canceled. On resume, the thread will begin running
1204 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * If @a thread is already suspended, the routine has no effect.
1207 *
1208 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 *
1210 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001211 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001212 */
Andrew Boie468190a2017-09-29 14:00:48 -07001213__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214
1215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001216 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001218 * This routine allows the kernel scheduler to make @a thread the current
1219 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * If @a thread is not currently suspended, the routine has no effect.
1222 *
1223 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
1225 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001226 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 */
Andrew Boie468190a2017-09-29 14:00:48 -07001228__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * This routine specifies how the scheduler will perform time slicing of
1234 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * To enable time slicing, @a slice must be non-zero. The scheduler
1237 * ensures that no thread runs for more than the specified time limit
1238 * before other threads of that priority are given a chance to execute.
1239 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001240 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001243 * execute. Once the scheduler selects a thread for execution, there is no
1244 * minimum guaranteed time the thread will execute before threads of greater or
1245 * equal priority are scheduled.
1246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001248 * for execution, this routine has no effect; the thread is immediately
1249 * rescheduled after the slice period expires.
1250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * To disable timeslicing, set both @a slice and @a prio to zero.
1252 *
1253 * @param slice Maximum time slice length (in milliseconds).
1254 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001255 *
1256 * @return N/A
1257 */
Kumar Galacc334c72017-04-21 10:55:34 -05001258extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001259
Anas Nashif166f5192018-02-25 08:02:36 -06001260/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001261
1262/**
1263 * @addtogroup isr_apis
1264 * @{
1265 */
1266
1267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001268 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001269 *
Allan Stephensc98da842016-11-11 15:45:03 -05001270 * This routine allows the caller to customize its actions, depending on
1271 * whether it is a thread or an ISR.
1272 *
1273 * @note Can be called by ISRs.
1274 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001275 * @return false if invoked by a thread.
1276 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001277 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001278extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001279
Benjamin Walsh445830d2016-11-10 15:54:27 -05001280/**
1281 * @brief Determine if code is running in a preemptible thread.
1282 *
Allan Stephensc98da842016-11-11 15:45:03 -05001283 * This routine allows the caller to customize its actions, depending on
1284 * whether it can be preempted by another thread. The routine returns a 'true'
1285 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001286 *
Allan Stephensc98da842016-11-11 15:45:03 -05001287 * - The code is running in a thread, not at ISR.
1288 * - The thread's priority is in the preemptible range.
1289 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001290 *
Allan Stephensc98da842016-11-11 15:45:03 -05001291 * @note Can be called by ISRs.
1292 *
1293 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001294 * @return Non-zero if invoked by a preemptible thread.
1295 */
Andrew Boie468190a2017-09-29 14:00:48 -07001296__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001297
Allan Stephensc98da842016-11-11 15:45:03 -05001298/**
Peter Bigot74ef3952019-12-23 11:48:43 -06001299 * @brief Test whether startup is in the before-main-task phase.
1300 *
1301 * This routine allows the caller to customize its actions, depending on
1302 * whether it being invoked before the kernel is fully active.
1303 *
1304 * @note Can be called by ISRs.
1305 *
1306 * @return true if invoked before post-kernel initialization
1307 * @return false if invoked during/after post-kernel initialization
1308 */
1309static inline bool k_is_pre_kernel(void)
1310{
1311 extern bool z_sys_post_kernel; /* in init.c */
1312
1313 return !z_sys_post_kernel;
1314}
1315
1316/**
Anas Nashif166f5192018-02-25 08:02:36 -06001317 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001318 */
1319
1320/**
1321 * @addtogroup thread_apis
1322 * @{
1323 */
1324
1325/**
1326 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001327 *
Allan Stephensc98da842016-11-11 15:45:03 -05001328 * This routine prevents the current thread from being preempted by another
1329 * thread by instructing the scheduler to treat it as a cooperative thread.
1330 * If the thread subsequently performs an operation that makes it unready,
1331 * it will be context switched out in the normal manner. When the thread
1332 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001333 *
Allan Stephensc98da842016-11-11 15:45:03 -05001334 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001335 *
Allan Stephensc98da842016-11-11 15:45:03 -05001336 * @note k_sched_lock() and k_sched_unlock() should normally be used
1337 * when the operation being performed can be safely interrupted by ISRs.
1338 * However, if the amount of processing involved is very small, better
1339 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001340 *
1341 * @return N/A
1342 */
1343extern void k_sched_lock(void);
1344
Allan Stephensc98da842016-11-11 15:45:03 -05001345/**
1346 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001347 *
Allan Stephensc98da842016-11-11 15:45:03 -05001348 * This routine reverses the effect of a previous call to k_sched_lock().
1349 * A thread must call the routine once for each time it called k_sched_lock()
1350 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001351 *
1352 * @return N/A
1353 */
1354extern void k_sched_unlock(void);
1355
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001357 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * Custom data is not used by the kernel itself, and is freely available
1362 * for a thread to use as it sees fit. It can be used as a framework
1363 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 *
1367 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001368 *
1369 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001370 */
Andrew Boie468190a2017-09-29 14:00:48 -07001371__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001372
1373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001374 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001379 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001380 */
Andrew Boie468190a2017-09-29 14:00:48 -07001381__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001382
1383/**
Anas Nashif57554052018-03-03 02:31:05 -06001384 * @brief Set current thread name
1385 *
1386 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1387 * tracing and debugging.
1388 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001389 * @param thread_id Thread to set name, or NULL to set the current thread
1390 * @param value Name string
1391 * @retval 0 on success
1392 * @retval -EFAULT Memory access error with supplied string
1393 * @retval -ENOSYS Thread name configuration option not enabled
1394 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001395 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001396__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001397
1398/**
1399 * @brief Get thread name
1400 *
1401 * Get the name of a thread
1402 *
1403 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001404 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001405 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001406const char *k_thread_name_get(k_tid_t thread_id);
1407
1408/**
1409 * @brief Copy the thread name into a supplied buffer
1410 *
1411 * @param thread_id Thread to obtain name information
1412 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001413 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001414 * @retval -ENOSPC Destination buffer too small
1415 * @retval -EFAULT Memory access error
1416 * @retval -ENOSYS Thread name feature not enabled
1417 * @retval 0 Success
1418 */
1419__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1420 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001421
1422/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001423 * @brief Get thread state string
1424 *
1425 * Get the human friendly thread state string
1426 *
1427 * @param thread_id Thread ID
1428 * @retval Thread state string, empty if no state flag is set
1429 */
1430const char *k_thread_state_str(k_tid_t thread_id);
1431
1432/**
Andy Rosscfe62032018-09-29 07:34:55 -07001433 * @}
1434 */
1435
1436/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001437 * @addtogroup clock_apis
1438 * @{
1439 */
1440
1441/**
1442 * @brief Generate null timeout delay.
1443 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001444 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001445 * not to wait if the requested operation cannot be performed immediately.
1446 *
1447 * @return Timeout delay value.
1448 */
1449#define K_NO_WAIT 0
1450
1451/**
1452 * @brief Generate timeout delay from milliseconds.
1453 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001454 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001455 * to wait up to @a ms milliseconds to perform the requested operation.
1456 *
1457 * @param ms Duration in milliseconds.
1458 *
1459 * @return Timeout delay value.
1460 */
Johan Hedberg14471692016-11-13 10:52:15 +02001461#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001462
1463/**
1464 * @brief Generate timeout delay from seconds.
1465 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001466 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001467 * to wait up to @a s seconds to perform the requested operation.
1468 *
1469 * @param s Duration in seconds.
1470 *
1471 * @return Timeout delay value.
1472 */
Johan Hedberg14471692016-11-13 10:52:15 +02001473#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001474
1475/**
1476 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001477
1478 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001479 * to wait up to @a m minutes to perform the requested operation.
1480 *
1481 * @param m Duration in minutes.
1482 *
1483 * @return Timeout delay value.
1484 */
Johan Hedberg14471692016-11-13 10:52:15 +02001485#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001486
1487/**
1488 * @brief Generate timeout delay from hours.
1489 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001490 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001491 * to wait up to @a h hours to perform the requested operation.
1492 *
1493 * @param h Duration in hours.
1494 *
1495 * @return Timeout delay value.
1496 */
Johan Hedberg14471692016-11-13 10:52:15 +02001497#define K_HOURS(h) K_MINUTES((h) * 60)
1498
Allan Stephensc98da842016-11-11 15:45:03 -05001499/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001500 * @brief Generate infinite timeout delay.
1501 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001502 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001503 * to wait as long as necessary to perform the requested operation.
1504 *
1505 * @return Timeout delay value.
1506 */
1507#define K_FOREVER (-1)
1508
1509/**
Anas Nashif166f5192018-02-25 08:02:36 -06001510 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001511 */
1512
1513/**
Allan Stephensc98da842016-11-11 15:45:03 -05001514 * @cond INTERNAL_HIDDEN
1515 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001516
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001517struct k_timer {
1518 /*
1519 * _timeout structure must be first here if we want to use
1520 * dynamic timer allocation. timeout.node is used in the double-linked
1521 * list of free timers
1522 */
1523 struct _timeout timeout;
1524
Allan Stephens45bfa372016-10-12 12:39:42 -05001525 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001526 _wait_q_t wait_q;
1527
1528 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001529 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530
1531 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001532 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001533
1534 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001535 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536
Allan Stephens45bfa372016-10-12 12:39:42 -05001537 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001538 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001539
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001540 /* user-specific data, also used to support legacy features */
1541 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001543 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001544 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545};
1546
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001547#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001548 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001549 .timeout = { \
1550 .node = {},\
1551 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001552 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001553 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001554 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001555 .expiry_fn = expiry, \
1556 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001557 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001558 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001559 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001560 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001561 }
1562
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001563#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001564
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001565/**
Allan Stephensc98da842016-11-11 15:45:03 -05001566 * INTERNAL_HIDDEN @endcond
1567 */
1568
1569/**
1570 * @defgroup timer_apis Timer APIs
1571 * @ingroup kernel_apis
1572 * @{
1573 */
1574
1575/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001576 * @typedef k_timer_expiry_t
1577 * @brief Timer expiry function type.
1578 *
1579 * A timer's expiry function is executed by the system clock interrupt handler
1580 * each time the timer expires. The expiry function is optional, and is only
1581 * invoked if the timer has been initialized with one.
1582 *
1583 * @param timer Address of timer.
1584 *
1585 * @return N/A
1586 */
1587typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1588
1589/**
1590 * @typedef k_timer_stop_t
1591 * @brief Timer stop function type.
1592 *
1593 * A timer's stop function is executed if the timer is stopped prematurely.
1594 * The function runs in the context of the thread that stops the timer.
1595 * The stop function is optional, and is only invoked if the timer has been
1596 * initialized with one.
1597 *
1598 * @param timer Address of timer.
1599 *
1600 * @return N/A
1601 */
1602typedef void (*k_timer_stop_t)(struct k_timer *timer);
1603
1604/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001608 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001609 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001610 *
1611 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @param expiry_fn Function to invoke each time the timer expires.
1613 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001614 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001615#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001616 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001617 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001618
Allan Stephens45bfa372016-10-12 12:39:42 -05001619/**
1620 * @brief Initialize a timer.
1621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001623 *
1624 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @param expiry_fn Function to invoke each time the timer expires.
1626 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001627 *
1628 * @return N/A
1629 */
1630extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001631 k_timer_expiry_t expiry_fn,
1632 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001633
Allan Stephens45bfa372016-10-12 12:39:42 -05001634/**
1635 * @brief Start a timer.
1636 *
1637 * This routine starts a timer, and resets its status to zero. The timer
1638 * begins counting down using the specified duration and period values.
1639 *
1640 * Attempting to start a timer that is already running is permitted.
1641 * The timer's status is reset to zero and the timer begins counting down
1642 * using the new duration and period values.
1643 *
1644 * @param timer Address of timer.
1645 * @param duration Initial timer duration (in milliseconds).
1646 * @param period Timer period (in milliseconds).
1647 *
1648 * @return N/A
1649 */
Andrew Boiea354d492017-09-29 16:22:28 -07001650__syscall void k_timer_start(struct k_timer *timer,
1651 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001652
1653/**
1654 * @brief Stop a timer.
1655 *
1656 * This routine stops a running timer prematurely. The timer's stop function,
1657 * if one exists, is invoked by the caller.
1658 *
1659 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001660 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001661 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001662 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1663 * if @a k_timer_stop is to be called from ISRs.
1664 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001665 * @param timer Address of timer.
1666 *
1667 * @return N/A
1668 */
Andrew Boiea354d492017-09-29 16:22:28 -07001669__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001670
1671/**
1672 * @brief Read timer status.
1673 *
1674 * This routine reads the timer's status, which indicates the number of times
1675 * it has expired since its status was last read.
1676 *
1677 * Calling this routine resets the timer's status to zero.
1678 *
1679 * @param timer Address of timer.
1680 *
1681 * @return Timer status.
1682 */
Andrew Boiea354d492017-09-29 16:22:28 -07001683__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001684
1685/**
1686 * @brief Synchronize thread to timer expiration.
1687 *
1688 * This routine blocks the calling thread until the timer's status is non-zero
1689 * (indicating that it has expired at least once since it was last examined)
1690 * or the timer is stopped. If the timer status is already non-zero,
1691 * or the timer is already stopped, the caller continues without waiting.
1692 *
1693 * Calling this routine resets the timer's status to zero.
1694 *
1695 * This routine must not be used by interrupt handlers, since they are not
1696 * allowed to block.
1697 *
1698 * @param timer Address of timer.
1699 *
1700 * @return Timer status.
1701 */
Andrew Boiea354d492017-09-29 16:22:28 -07001702__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001703
Andy Ross52e444b2018-09-28 09:06:37 -07001704extern s32_t z_timeout_remaining(struct _timeout *timeout);
1705
Allan Stephens45bfa372016-10-12 12:39:42 -05001706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001707 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001708 *
1709 * This routine computes the (approximate) time remaining before a running
1710 * timer next expires. If the timer is not running, it returns zero.
1711 *
1712 * @param timer Address of timer.
1713 *
1714 * @return Remaining time (in milliseconds).
1715 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001716__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001717
Patrik Flykt4344e272019-03-08 14:19:05 -07001718static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001719{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001720 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001721 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001722}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001723
Allan Stephensc98da842016-11-11 15:45:03 -05001724/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001725 * @brief Associate user-specific data with a timer.
1726 *
1727 * This routine records the @a user_data with the @a timer, to be retrieved
1728 * later.
1729 *
1730 * It can be used e.g. in a timer handler shared across multiple subsystems to
1731 * retrieve data specific to the subsystem this timer is associated with.
1732 *
1733 * @param timer Address of timer.
1734 * @param user_data User data to associate with the timer.
1735 *
1736 * @return N/A
1737 */
Andrew Boiea354d492017-09-29 16:22:28 -07001738__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1739
Anas Nashif954d5502018-02-25 08:37:28 -06001740/**
1741 * @internal
1742 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001743static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001744 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001745{
1746 timer->user_data = user_data;
1747}
1748
1749/**
1750 * @brief Retrieve the user-specific data from a timer.
1751 *
1752 * @param timer Address of timer.
1753 *
1754 * @return The user data.
1755 */
Andrew Boiea354d492017-09-29 16:22:28 -07001756__syscall void *k_timer_user_data_get(struct k_timer *timer);
1757
Patrik Flykt4344e272019-03-08 14:19:05 -07001758static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001759{
1760 return timer->user_data;
1761}
1762
Anas Nashif166f5192018-02-25 08:02:36 -06001763/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001764
Allan Stephensc98da842016-11-11 15:45:03 -05001765/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001766 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001767 * @{
1768 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001769
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * This routine returns the elapsed time since the system booted,
1774 * in milliseconds.
1775 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001776 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001777 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001778 * While this function returns time in milliseconds, it does
1779 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001780 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001781 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001782 *
1783 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001784 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001785__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001786
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001787/**
1788 * @brief Enable clock always on in tickless kernel
1789 *
Andy Ross1db9f182019-06-25 10:09:45 -07001790 * Deprecated. This does nothing (it was always just a hint). This
1791 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1792 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001793 *
1794 * @retval prev_status Previous status of always on flag
1795 */
Andy Ross1db9f182019-06-25 10:09:45 -07001796/* LCOV_EXCL_START */
1797__deprecated static inline int k_enable_sys_clock_always_on(void)
1798{
1799 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1800 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1801
1802 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1803}
1804/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001805
1806/**
1807 * @brief Disable clock always on in tickless kernel
1808 *
Andy Ross1db9f182019-06-25 10:09:45 -07001809 * Deprecated. This does nothing (it was always just a hint). This
1810 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1811 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001812 */
Andy Ross1db9f182019-06-25 10:09:45 -07001813/* LCOV_EXCL_START */
1814__deprecated static inline void k_disable_sys_clock_always_on(void)
1815{
1816 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1817 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1818}
1819/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001820
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001823 *
Peter Bigota6067a32019-08-28 08:19:26 -05001824 * This routine returns the lower 32 bits of the system uptime in
1825 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001826 *
Peter Bigota6067a32019-08-28 08:19:26 -05001827 * Because correct conversion requires full precision of the system
1828 * clock there is no benefit to using this over k_uptime_get() unless
1829 * you know the application will never run long enough for the system
1830 * clock to approach 2^32 ticks. Calls to this function may involve
1831 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001832 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001833 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001834 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001835 * While this function returns time in milliseconds, it does
1836 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001837 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001838 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001839 *
Peter Bigota6067a32019-08-28 08:19:26 -05001840 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001841 */
Peter Bigota6067a32019-08-28 08:19:26 -05001842static inline u32_t k_uptime_get_32(void)
1843{
1844 return (u32_t)k_uptime_get();
1845}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001846
1847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001850 * This routine computes the elapsed time between the current system uptime
1851 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001853 * @param reftime Pointer to a reference time, which is updated to the current
1854 * uptime upon return.
1855 *
1856 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001857 */
Andy Ross987c0e52018-09-27 16:50:00 -07001858static inline s64_t k_uptime_delta(s64_t *reftime)
1859{
1860 s64_t uptime, delta;
1861
1862 uptime = k_uptime_get();
1863 delta = uptime - *reftime;
1864 *reftime = uptime;
1865
1866 return delta;
1867}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001868
1869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001870 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001872 * This routine computes the elapsed time between the current system uptime
1873 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1876 * need for interrupt locking and 64-bit math. However, the 32-bit result
1877 * cannot hold an elapsed time larger than approximately 50 days, so the
1878 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001880 * @param reftime Pointer to a reference time, which is updated to the current
1881 * uptime upon return.
1882 *
1883 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001884 */
Andy Ross987c0e52018-09-27 16:50:00 -07001885static inline u32_t k_uptime_delta_32(s64_t *reftime)
1886{
1887 return (u32_t)k_uptime_delta(reftime);
1888}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001889
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001890/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001891 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001893 * This routine returns the current time, as measured by the system's hardware
1894 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001896 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001897 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001898static inline u32_t k_cycle_get_32(void)
1899{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001900 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001901}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001902
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001903/**
Anas Nashif166f5192018-02-25 08:02:36 -06001904 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001905 */
1906
Allan Stephensc98da842016-11-11 15:45:03 -05001907/**
1908 * @cond INTERNAL_HIDDEN
1909 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001910
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001911struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001912 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001913 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001914 union {
1915 _wait_q_t wait_q;
1916
1917 _POLL_EVENT;
1918 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001919
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001920 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001921 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001922};
1923
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001924#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001925 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001926 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001927 .lock = { }, \
1928 { \
1929 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1930 _POLL_EVENT_OBJ_INIT(obj) \
1931 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001932 _OBJECT_TRACING_INIT \
1933 }
1934
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001935#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001936
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001937extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1938
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001939/**
1940 * INTERNAL_HIDDEN @endcond
1941 */
1942
1943/**
1944 * @defgroup queue_apis Queue APIs
1945 * @ingroup kernel_apis
1946 * @{
1947 */
1948
1949/**
1950 * @brief Initialize a queue.
1951 *
1952 * This routine initializes a queue object, prior to its first use.
1953 *
1954 * @param queue Address of the queue.
1955 *
1956 * @return N/A
1957 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001958__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001959
1960/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001961 * @brief Cancel waiting on a queue.
1962 *
1963 * This routine causes first thread pending on @a queue, if any, to
1964 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001965 * If the queue is being waited on by k_poll(), it will return with
1966 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1967 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001968 *
1969 * @note Can be called by ISRs.
1970 *
1971 * @param queue Address of the queue.
1972 *
1973 * @return N/A
1974 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001975__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001976
1977/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978 * @brief Append an element to the end of a queue.
1979 *
1980 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001981 * aligned on a word boundary, and the first word of the item is reserved
1982 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001983 *
1984 * @note Can be called by ISRs.
1985 *
1986 * @param queue Address of the queue.
1987 * @param data Address of the data item.
1988 *
1989 * @return N/A
1990 */
1991extern void k_queue_append(struct k_queue *queue, void *data);
1992
1993/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001994 * @brief Append an element to a queue.
1995 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001996 * This routine appends a data item to @a queue. There is an implicit memory
1997 * allocation to create an additional temporary bookkeeping data structure from
1998 * the calling thread's resource pool, which is automatically freed when the
1999 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002000 *
2001 * @note Can be called by ISRs.
2002 *
2003 * @param queue Address of the queue.
2004 * @param data Address of the data item.
2005 *
2006 * @retval 0 on success
2007 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2008 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302009__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002010
2011/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002012 * @brief Prepend an element to a queue.
2013 *
2014 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002015 * aligned on a word boundary, and the first word of the item is reserved
2016 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002017 *
2018 * @note Can be called by ISRs.
2019 *
2020 * @param queue Address of the queue.
2021 * @param data Address of the data item.
2022 *
2023 * @return N/A
2024 */
2025extern void k_queue_prepend(struct k_queue *queue, void *data);
2026
2027/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002028 * @brief Prepend an element to a queue.
2029 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002030 * This routine prepends a data item to @a queue. There is an implicit memory
2031 * allocation to create an additional temporary bookkeeping data structure from
2032 * the calling thread's resource pool, which is automatically freed when the
2033 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002034 *
2035 * @note Can be called by ISRs.
2036 *
2037 * @param queue Address of the queue.
2038 * @param data Address of the data item.
2039 *
2040 * @retval 0 on success
2041 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2042 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302043__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002044
2045/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002046 * @brief Inserts an element to a queue.
2047 *
2048 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002049 * data item must be aligned on a word boundary, and the first word of
2050 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002051 *
2052 * @note Can be called by ISRs.
2053 *
2054 * @param queue Address of the queue.
2055 * @param prev Address of the previous data item.
2056 * @param data Address of the data item.
2057 *
2058 * @return N/A
2059 */
2060extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2061
2062/**
2063 * @brief Atomically append a list of elements to a queue.
2064 *
2065 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002066 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002067 * in each data item pointing to the next data item; the list must be
2068 * NULL-terminated.
2069 *
2070 * @note Can be called by ISRs.
2071 *
2072 * @param queue Address of the queue.
2073 * @param head Pointer to first node in singly-linked list.
2074 * @param tail Pointer to last node in singly-linked list.
2075 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002076 * @retval 0 on success
2077 * @retval -EINVAL on invalid supplied data
2078 *
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002079 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002080extern int k_queue_append_list(struct k_queue *queue, void *head, void *tail);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002081
2082/**
2083 * @brief Atomically add a list of elements to a queue.
2084 *
2085 * This routine adds a list of data items to @a queue in one operation.
2086 * The data items must be in a singly-linked list implemented using a
2087 * sys_slist_t object. Upon completion, the original list is empty.
2088 *
2089 * @note Can be called by ISRs.
2090 *
2091 * @param queue Address of the queue.
2092 * @param list Pointer to sys_slist_t object.
2093 *
Anas Nashif756d8b02019-06-16 09:53:55 -04002094 * @retval 0 on success
2095 * @retval -EINVAL on invalid data
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002096 */
Anas Nashif756d8b02019-06-16 09:53:55 -04002097extern int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002098
2099/**
2100 * @brief Get an element from a queue.
2101 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002102 * This routine removes first data item from @a queue. The first word of the
2103 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002104 *
2105 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2106 *
2107 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002108 * @param timeout Non-negative waiting period to obtain a data item (in
2109 * milliseconds), or one of the special values K_NO_WAIT and
2110 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002111 *
2112 * @return Address of the data item if successful; NULL if returned
2113 * without waiting, or waiting period timed out.
2114 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002115__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002116
2117/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002118 * @brief Remove an element from a queue.
2119 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002120 * This routine removes data item from @a queue. The first word of the
2121 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002122 * rely on sys_slist_find_and_remove which is not a constant time operation.
2123 *
2124 * @note Can be called by ISRs
2125 *
2126 * @param queue Address of the queue.
2127 * @param data Address of the data item.
2128 *
2129 * @return true if data item was removed
2130 */
2131static inline bool k_queue_remove(struct k_queue *queue, void *data)
2132{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002133 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002134}
2135
2136/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002137 * @brief Append an element to a queue only if it's not present already.
2138 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002139 * This routine appends data item to @a queue. The first word of the data
2140 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002141 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2142 *
2143 * @note Can be called by ISRs
2144 *
2145 * @param queue Address of the queue.
2146 * @param data Address of the data item.
2147 *
2148 * @return true if data item was added, false if not
2149 */
2150static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2151{
2152 sys_sfnode_t *test;
2153
2154 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2155 if (test == (sys_sfnode_t *) data) {
2156 return false;
2157 }
2158 }
2159
2160 k_queue_append(queue, data);
2161 return true;
2162}
2163
2164/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002165 * @brief Query a queue to see if it has data available.
2166 *
2167 * Note that the data might be already gone by the time this function returns
2168 * if other threads are also trying to read from the queue.
2169 *
2170 * @note Can be called by ISRs.
2171 *
2172 * @param queue Address of the queue.
2173 *
2174 * @return Non-zero if the queue is empty.
2175 * @return 0 if data is available.
2176 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002177__syscall int k_queue_is_empty(struct k_queue *queue);
2178
Patrik Flykt4344e272019-03-08 14:19:05 -07002179static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002180{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002181 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002182}
2183
2184/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002185 * @brief Peek element at the head of queue.
2186 *
2187 * Return element from the head of queue without removing it.
2188 *
2189 * @param queue Address of the queue.
2190 *
2191 * @return Head element, or NULL if queue is empty.
2192 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002193__syscall void *k_queue_peek_head(struct k_queue *queue);
2194
Patrik Flykt4344e272019-03-08 14:19:05 -07002195static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002196{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002197 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002198}
2199
2200/**
2201 * @brief Peek element at the tail of queue.
2202 *
2203 * Return element from the tail of queue without removing it.
2204 *
2205 * @param queue Address of the queue.
2206 *
2207 * @return Tail element, or NULL if queue is empty.
2208 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002209__syscall void *k_queue_peek_tail(struct k_queue *queue);
2210
Patrik Flykt4344e272019-03-08 14:19:05 -07002211static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002212{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002213 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002214}
2215
2216/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002217 * @brief Statically define and initialize a queue.
2218 *
2219 * The queue can be accessed outside the module where it is defined using:
2220 *
2221 * @code extern struct k_queue <name>; @endcode
2222 *
2223 * @param name Name of the queue.
2224 */
2225#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002226 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002227 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002228
Anas Nashif166f5192018-02-25 08:02:36 -06002229/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002230
Wentong Wu5611e922019-06-20 23:51:27 +08002231#ifdef CONFIG_USERSPACE
2232/**
2233 * @brief futex structure
2234 *
2235 * A k_futex is a lightweight mutual exclusion primitive designed
2236 * to minimize kernel involvement. Uncontended operation relies
2237 * only on atomic access to shared memory. k_futex are tracked as
2238 * kernel objects and can live in user memory so any access bypass
2239 * the kernel object permission management mechanism.
2240 */
2241struct k_futex {
2242 atomic_t val;
2243};
2244
2245/**
2246 * @brief futex kernel data structure
2247 *
2248 * z_futex_data are the helper data structure for k_futex to complete
2249 * futex contended operation on kernel side, structure z_futex_data
2250 * of every futex object is invisible in user mode.
2251 */
2252struct z_futex_data {
2253 _wait_q_t wait_q;
2254 struct k_spinlock lock;
2255};
2256
2257#define Z_FUTEX_DATA_INITIALIZER(obj) \
2258 { \
2259 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2260 }
2261
2262/**
2263 * @defgroup futex_apis FUTEX APIs
2264 * @ingroup kernel_apis
2265 * @{
2266 */
2267
2268/**
Wentong Wu5611e922019-06-20 23:51:27 +08002269 * @brief Pend the current thread on a futex
2270 *
2271 * Tests that the supplied futex contains the expected value, and if so,
2272 * goes to sleep until some other thread calls k_futex_wake() on it.
2273 *
2274 * @param futex Address of the futex.
2275 * @param expected Expected value of the futex, if it is different the caller
2276 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002277 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2278 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002279 * @retval -EACCES Caller does not have read access to futex address.
2280 * @retval -EAGAIN If the futex value did not match the expected parameter.
2281 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2282 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2283 * @retval 0 if the caller went to sleep and was woken up. The caller
2284 * should check the futex's value on wakeup to determine if it needs
2285 * to block again.
2286 */
2287__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2288
2289/**
2290 * @brief Wake one/all threads pending on a futex
2291 *
2292 * Wake up the highest priority thread pending on the supplied futex, or
2293 * wakeup all the threads pending on the supplied futex, and the behavior
2294 * depends on wake_all.
2295 *
2296 * @param futex Futex to wake up pending threads.
2297 * @param wake_all If true, wake up all pending threads; If false,
2298 * wakeup the highest priority thread.
2299 * @retval -EACCES Caller does not have access to the futex address.
2300 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2301 * @retval Number of threads that were woken up.
2302 */
2303__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2304
2305/** @} */
2306#endif
2307
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002309 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310};
2311
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002312/**
2313 * @cond INTERNAL_HIDDEN
2314 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002315#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002316 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002317 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002318 }
2319
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002320#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002321
Allan Stephensc98da842016-11-11 15:45:03 -05002322/**
2323 * INTERNAL_HIDDEN @endcond
2324 */
2325
2326/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002327 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002328 * @ingroup kernel_apis
2329 * @{
2330 */
2331
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002333 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002335 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002337 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 *
2339 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002340 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002342#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002343 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002344
2345/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002346 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002347 *
2348 * This routine causes first thread pending on @a fifo, if any, to
2349 * return from k_fifo_get() call with NULL value (as if timeout
2350 * expired).
2351 *
2352 * @note Can be called by ISRs.
2353 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002355 *
2356 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002357 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002358 */
2359#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002360 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002361
2362/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002363 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002365 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002366 * aligned on a word boundary, and the first word of the item is reserved
2367 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * @note Can be called by ISRs.
2370 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002371 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373 *
2374 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002375 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002376 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002377#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002378 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379
2380/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002381 * @brief Add an element to a FIFO queue.
2382 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002383 * This routine adds a data item to @a fifo. There is an implicit memory
2384 * allocation to create an additional temporary bookkeeping data structure from
2385 * the calling thread's resource pool, which is automatically freed when the
2386 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002387 *
2388 * @note Can be called by ISRs.
2389 *
2390 * @param fifo Address of the FIFO.
2391 * @param data Address of the data item.
2392 *
2393 * @retval 0 on success
2394 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002395 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002396 */
2397#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002398 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002399
2400/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002401 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002404 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * each data item pointing to the next data item; the list must be
2406 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002409 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002410 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411 * @param head Pointer to first node in singly-linked list.
2412 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002413 *
2414 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002415 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002417#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002418 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419
2420/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002421 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 * This routine adds a list of data items to @a fifo in one operation.
2424 * The data items must be in a singly-linked list implemented using a
2425 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002426 * and must be re-initialized via sys_slist_init().
2427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 * @note Can be called by ISRs.
2429 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002430 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 *
2433 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002434 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002436#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002437 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438
2439/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002440 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002442 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002443 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2446 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002447 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002448 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002449 * or one of the special values K_NO_WAIT and K_FOREVER.
2450 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002451 * @return Address of the data item if successful; NULL if returned
2452 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002453 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002455#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002456 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002459 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002460 *
2461 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002462 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002463 *
2464 * @note Can be called by ISRs.
2465 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002466 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002467 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002468 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002469 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002470 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002471 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002472#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002473 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002474
2475/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002476 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002477 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002478 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302479 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002480 * on each iteration of processing, a head container will be peeked,
2481 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002482 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002483 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002484 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002485 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002486 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002487 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002488 */
2489#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002490 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002491
2492/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002493 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002494 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002495 * Return element from the tail of FIFO queue (without removing it). A usecase
2496 * for this is if elements of the FIFO queue are themselves containers. Then
2497 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002498 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002499 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002500 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002501 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002502 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002503 */
2504#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002505 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002506
2507/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002508 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002510 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002511 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002512 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002514 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002515 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002518 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002519 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520
Anas Nashif166f5192018-02-25 08:02:36 -06002521/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002522
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002524 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525};
2526
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002527/**
2528 * @cond INTERNAL_HIDDEN
2529 */
2530
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002531#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002532 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002533 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002534 }
2535
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002536#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002537
Allan Stephensc98da842016-11-11 15:45:03 -05002538/**
2539 * INTERNAL_HIDDEN @endcond
2540 */
2541
2542/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002543 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002544 * @ingroup kernel_apis
2545 * @{
2546 */
2547
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002548/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002549 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002550 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002551 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002552 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002553 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002554 *
2555 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002556 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002557 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002558#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002559 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002560
2561/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002562 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002563 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002564 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002565 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002566 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * @note Can be called by ISRs.
2569 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002570 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002572 *
2573 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002574 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002576#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002577 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578
2579/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002580 * @brief Add an element to a LIFO queue.
2581 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002582 * This routine adds a data item to @a lifo. There is an implicit memory
2583 * allocation to create an additional temporary bookkeeping data structure from
2584 * the calling thread's resource pool, which is automatically freed when the
2585 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002586 *
2587 * @note Can be called by ISRs.
2588 *
2589 * @param lifo Address of the LIFO.
2590 * @param data Address of the data item.
2591 *
2592 * @retval 0 on success
2593 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002594 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002595 */
2596#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002597 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002598
2599/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002600 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002602 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002603 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2606 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002607 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002609 * or one of the special values K_NO_WAIT and K_FOREVER.
2610 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002611 * @return Address of the data item if successful; NULL if returned
2612 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002613 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002614 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002615#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002616 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002618/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002619 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002621 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002622 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002623 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002625 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002626 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002627 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002629 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002630 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631
Anas Nashif166f5192018-02-25 08:02:36 -06002632/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002633
2634/**
2635 * @cond INTERNAL_HIDDEN
2636 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302637#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002638
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002639typedef uintptr_t stack_data_t;
2640
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641struct k_stack {
2642 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002643 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002644 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002646 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002647 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002648 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649};
2650
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002651#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002652 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002653 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002654 .base = stack_buffer, \
2655 .next = stack_buffer, \
2656 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002657 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002658 }
2659
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002660#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002661
Allan Stephensc98da842016-11-11 15:45:03 -05002662/**
2663 * INTERNAL_HIDDEN @endcond
2664 */
2665
2666/**
2667 * @defgroup stack_apis Stack APIs
2668 * @ingroup kernel_apis
2669 * @{
2670 */
2671
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672/**
2673 * @brief Initialize a stack.
2674 *
2675 * This routine initializes a stack object, prior to its first use.
2676 *
2677 * @param stack Address of the stack.
2678 * @param buffer Address of array used to hold stacked values.
2679 * @param num_entries Maximum number of values that can be stacked.
2680 *
2681 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002682 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002683 */
Andrew Boief3bee952018-05-02 17:44:39 -07002684void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002685 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002686
2687
2688/**
2689 * @brief Initialize a stack.
2690 *
2691 * This routine initializes a stack object, prior to its first use. Internal
2692 * buffers will be allocated from the calling thread's resource pool.
2693 * This memory will be released if k_stack_cleanup() is called, or
2694 * userspace is enabled and the stack object loses all references to it.
2695 *
2696 * @param stack Address of the stack.
2697 * @param num_entries Maximum number of values that can be stacked.
2698 *
2699 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002700 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002701 */
2702
Adithya Baglody28080d32018-10-15 11:48:51 +05302703__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2704 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002705
2706/**
2707 * @brief Release a stack's allocated buffer
2708 *
2709 * If a stack object was given a dynamically allocated buffer via
2710 * k_stack_alloc_init(), this will free it. This function does nothing
2711 * if the buffer wasn't dynamically allocated.
2712 *
2713 * @param stack Address of the stack.
Anas Nashif1ed67d12019-06-16 08:58:10 -04002714 * @retval 0 on success
2715 * @retval -EAGAIN when object is still in use
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002716 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002717 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002718int k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002719
2720/**
2721 * @brief Push an element onto a stack.
2722 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002723 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 *
2725 * @note Can be called by ISRs.
2726 *
2727 * @param stack Address of the stack.
2728 * @param data Value to push onto the stack.
2729 *
Anas Nashif1ed67d12019-06-16 08:58:10 -04002730 * @retval 0 on success
2731 * @retval -ENOMEM if stack is full
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002732 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002734__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735
2736/**
2737 * @brief Pop an element from a stack.
2738 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002739 * This routine removes a stack_data_t value from @a stack in a "last in,
2740 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 *
2742 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2743 *
2744 * @param stack Address of the stack.
2745 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002746 * @param timeout Non-negative waiting period to obtain a value (in
2747 * milliseconds), or one of the special values K_NO_WAIT and
2748 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002749 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002750 * @retval 0 Element popped from stack.
2751 * @retval -EBUSY Returned without waiting.
2752 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002753 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002754 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002755__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2756 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002762 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002763 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @param name Name of the stack.
2766 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002767 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002768 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002769#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002770 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002771 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002772 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002773 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002774 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775
Anas Nashif166f5192018-02-25 08:02:36 -06002776/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002777
Allan Stephens6bba9b02016-11-16 14:56:54 -05002778struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002779struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002780
Piotr Zięcik19d83492019-09-27 09:16:25 +02002781/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002782typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2783struct _poller {
2784 volatile bool is_polling;
2785 struct k_thread *thread;
2786 _poller_cb_t cb;
2787};
2788
Allan Stephensc98da842016-11-11 15:45:03 -05002789/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002790 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002791 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792 */
2793
Allan Stephens6bba9b02016-11-16 14:56:54 -05002794/**
2795 * @typedef k_work_handler_t
2796 * @brief Work item handler function type.
2797 *
2798 * A work item's handler function is executed by a workqueue's thread
2799 * when the work item is processed by the workqueue.
2800 *
2801 * @param work Address of the work item.
2802 *
2803 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002804 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002805 */
2806typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807
2808/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002809 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002810 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002811
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002813 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002814 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815};
2816
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002818 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819};
2820
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002822 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823 k_work_handler_t handler;
2824 atomic_t flags[1];
2825};
2826
Allan Stephens6bba9b02016-11-16 14:56:54 -05002827struct k_delayed_work {
2828 struct k_work work;
2829 struct _timeout timeout;
2830 struct k_work_q *work_q;
2831};
2832
Piotr Zięcik19d83492019-09-27 09:16:25 +02002833struct k_work_poll {
2834 struct k_work work;
2835 struct _poller poller;
2836 struct k_poll_event *events;
2837 int num_events;
2838 k_work_handler_t real_handler;
2839 struct _timeout timeout;
2840 int poll_result;
2841};
2842
Allan Stephens6bba9b02016-11-16 14:56:54 -05002843extern struct k_work_q k_sys_work_q;
2844
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002846 * INTERNAL_HIDDEN @endcond
2847 */
2848
Patrik Flykt4344e272019-03-08 14:19:05 -07002849#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002850 { \
2851 ._reserved = NULL, \
2852 .handler = work_handler, \
2853 .flags = { 0 } \
2854 }
2855
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002856#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002857
Allan Stephens6bba9b02016-11-16 14:56:54 -05002858/**
2859 * @brief Initialize a statically-defined work item.
2860 *
2861 * This macro can be used to initialize a statically-defined workqueue work
2862 * item, prior to its first use. For example,
2863 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002864 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002865 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002866 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002867 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002868 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002869 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002870#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002871 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872
2873/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002874 * @brief Initialize a work item.
2875 *
2876 * This routine initializes a workqueue work item, prior to its first use.
2877 *
2878 * @param work Address of work item.
2879 * @param handler Function to invoke each time work item is processed.
2880 *
2881 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002882 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883 */
2884static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2885{
Patrik Flykt4344e272019-03-08 14:19:05 -07002886 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002887}
2888
2889/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002890 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002891 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002892 * This routine submits work item @a work to be processed by workqueue
2893 * @a work_q. If the work item is already pending in the workqueue's queue
2894 * as a result of an earlier submission, this routine has no effect on the
2895 * work item. If the work item has already been processed, or is currently
2896 * being processed, its work is considered complete and the work item can be
2897 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002898 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002899 * @warning
2900 * A submitted work item must not be modified until it has been processed
2901 * by the workqueue.
2902 *
2903 * @note Can be called by ISRs.
2904 *
2905 * @param work_q Address of workqueue.
2906 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002907 *
2908 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002909 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910 */
2911static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2912 struct k_work *work)
2913{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002914 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002915 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916 }
2917}
2918
2919/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002920 * @brief Submit a work item to a user mode workqueue
2921 *
David B. Kinder06d78352018-12-17 14:32:40 -08002922 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002923 * memory allocation is made from the caller's resource pool which is freed
2924 * once the worker thread consumes the k_work item. The workqueue
2925 * thread must have memory access to the k_work item being submitted. The caller
2926 * must have permission granted on the work_q parameter's queue object.
2927 *
2928 * Otherwise this works the same as k_work_submit_to_queue().
2929 *
2930 * @note Can be called by ISRs.
2931 *
2932 * @param work_q Address of workqueue.
2933 * @param work Address of work item.
2934 *
2935 * @retval -EBUSY if the work item was already in some workqueue
2936 * @retval -ENOMEM if no memory for thread resource pool allocation
2937 * @retval 0 Success
2938 * @req K-WORK-001
2939 */
2940static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2941 struct k_work *work)
2942{
2943 int ret = -EBUSY;
2944
2945 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2946 ret = k_queue_alloc_append(&work_q->queue, work);
2947
2948 /* Couldn't insert into the queue. Clear the pending bit
2949 * so the work item can be submitted again
2950 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002951 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002952 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2953 }
2954 }
2955
2956 return ret;
2957}
2958
2959/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002960 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002961 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002962 * This routine indicates if work item @a work is pending in a workqueue's
2963 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002964 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002965 * @note Can be called by ISRs.
2966 *
2967 * @param work Address of work item.
2968 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002969 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002970 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002971 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002972static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002973{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002974 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002975}
2976
2977/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002978 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002979 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002980 * This routine starts workqueue @a work_q. The workqueue spawns its work
2981 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002983 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002984 * @param stack Pointer to work queue thread's stack space, as defined by
2985 * K_THREAD_STACK_DEFINE()
2986 * @param stack_size Size of the work queue thread's stack (in bytes), which
2987 * should either be the same constant passed to
2988 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002989 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002990 *
2991 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002992 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002993 */
Andrew Boie507852a2017-07-25 18:47:07 -07002994extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002995 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002996 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002998/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002999 * @brief Start a workqueue in user mode
3000 *
3001 * This works identically to k_work_q_start() except it is callable from user
3002 * mode, and the worker thread created will run in user mode.
3003 * The caller must have permissions granted on both the work_q parameter's
3004 * thread and queue objects, and the same restrictions on priority apply as
3005 * k_thread_create().
3006 *
3007 * @param work_q Address of workqueue.
3008 * @param stack Pointer to work queue thread's stack space, as defined by
3009 * K_THREAD_STACK_DEFINE()
3010 * @param stack_size Size of the work queue thread's stack (in bytes), which
3011 * should either be the same constant passed to
3012 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
3013 * @param prio Priority of the work queue's thread.
3014 *
3015 * @return N/A
3016 * @req K-WORK-001
3017 */
3018extern void k_work_q_user_start(struct k_work_q *work_q,
3019 k_thread_stack_t *stack,
3020 size_t stack_size, int prio);
3021
3022/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003023 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003024 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003025 * This routine initializes a workqueue delayed work item, prior to
3026 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003027 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003028 * @param work Address of delayed work item.
3029 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003030 *
3031 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003032 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003034extern void k_delayed_work_init(struct k_delayed_work *work,
3035 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036
3037/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003038 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003040 * This routine schedules work item @a work to be processed by workqueue
3041 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003042 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003043 * Only when the countdown completes is the work item actually submitted to
3044 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003046 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08003047 * counting down cancels the existing submission and restarts the
3048 * countdown using the new delay. Note that this behavior is
3049 * inherently subject to race conditions with the pre-existing
3050 * timeouts and work queue, so care must be taken to synchronize such
3051 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003052 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003053 * @warning
3054 * A delayed work item must not be modified until it has been processed
3055 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003056 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003057 * @note Can be called by ISRs.
3058 *
3059 * @param work_q Address of workqueue.
3060 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003061 * @param delay Non-negative delay before submitting the work item (in
3062 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003063 *
3064 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003065 * @retval -EINVAL Work item is being processed or has completed its work.
3066 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003067 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003069extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3070 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003071 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072
3073/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003074 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003076 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003077 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003078 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003080 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003082 * @note The result of calling this on a k_delayed_work item that has
3083 * not been submitted (i.e. before the return of the
3084 * k_delayed_work_submit_to_queue() call) is undefined.
3085 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003086 * @param work Address of delayed work item.
3087 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003088 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003089 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003090 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003092extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003094/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003095 * @brief Submit a work item to the system workqueue.
3096 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003097 * This routine submits work item @a work to be processed by the system
3098 * workqueue. If the work item is already pending in the workqueue's queue
3099 * as a result of an earlier submission, this routine has no effect on the
3100 * work item. If the work item has already been processed, or is currently
3101 * being processed, its work is considered complete and the work item can be
3102 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003104 * @warning
3105 * Work items submitted to the system workqueue should avoid using handlers
3106 * that block or yield since this may prevent the system workqueue from
3107 * processing other work items in a timely manner.
3108 *
3109 * @note Can be called by ISRs.
3110 *
3111 * @param work Address of work item.
3112 *
3113 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003114 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115 */
3116static inline void k_work_submit(struct k_work *work)
3117{
3118 k_work_submit_to_queue(&k_sys_work_q, work);
3119}
3120
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122 * @brief Submit a delayed work item to the system workqueue.
3123 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003124 * This routine schedules work item @a work to be processed by the system
3125 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003126 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003127 * Only when the countdown completes is the work item actually submitted to
3128 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003130 * Submitting a previously submitted delayed work item that is still
3131 * counting down cancels the existing submission and restarts the countdown
3132 * using the new delay. If the work item is currently pending on the
3133 * workqueue's queue because the countdown has completed it is too late to
3134 * resubmit the item, and resubmission fails without impacting the work item.
3135 * If the work item has already been processed, or is currently being processed,
3136 * its work is considered complete and the work item can be resubmitted.
3137 *
3138 * @warning
3139 * Work items submitted to the system workqueue should avoid using handlers
3140 * that block or yield since this may prevent the system workqueue from
3141 * processing other work items in a timely manner.
3142 *
3143 * @note Can be called by ISRs.
3144 *
3145 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003146 * @param delay Non-negative delay before submitting the work item (in
3147 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003148 *
3149 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003150 * @retval -EINVAL Work item is being processed or has completed its work.
3151 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003152 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003153 */
3154static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003155 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003156{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003157 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003158}
3159
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003160/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003161 * @brief Get time remaining before a delayed work gets scheduled.
3162 *
3163 * This routine computes the (approximate) time remaining before a
3164 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003165 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003166 *
3167 * @param work Delayed work item.
3168 *
3169 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003170 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003171 */
Kumar Galacc334c72017-04-21 10:55:34 -05003172static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003173{
Andy Ross88924062019-10-03 11:43:10 -07003174 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003175}
3176
Piotr Zięcik19d83492019-09-27 09:16:25 +02003177/**
3178 * @brief Initialize a triggered work item.
3179 *
3180 * This routine initializes a workqueue triggered work item, prior to
3181 * its first use.
3182 *
3183 * @param work Address of triggered work item.
3184 * @param handler Function to invoke each time work item is processed.
3185 *
3186 * @return N/A
3187 */
3188extern void k_work_poll_init(struct k_work_poll *work,
3189 k_work_handler_t handler);
3190
3191/**
3192 * @brief Submit a triggered work item.
3193 *
3194 * This routine schedules work item @a work to be processed by workqueue
3195 * @a work_q when one of the given @a events is signaled. The routine
3196 * initiates internal poller for the work item and then returns to the caller.
3197 * Only when one of the watched events happen the work item is actually
3198 * submitted to the workqueue and becomes pending.
3199 *
3200 * Submitting a previously submitted triggered work item that is still
3201 * waiting for the event cancels the existing submission and reschedules it
3202 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003203 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003204 * so care must be taken to synchronize such resubmissions externally.
3205 *
3206 * @note Can be called by ISRs.
3207 *
3208 * @warning
3209 * Provided array of events as well as a triggered work item must be placed
3210 * in persistent memory (valid until work handler execution or work
3211 * cancellation) and cannot be modified after submission.
3212 *
3213 * @param work_q Address of workqueue.
3214 * @param work Address of delayed work item.
3215 * @param events An array of pointers to events which trigger the work.
3216 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003217 * @param timeout Non-negative timeout after which the work will be scheduled
3218 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003219 *
3220 *
3221 * @retval 0 Work item started watching for events.
3222 * @retval -EINVAL Work item is being processed or has completed its work.
3223 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3224 */
3225extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3226 struct k_work_poll *work,
3227 struct k_poll_event *events,
3228 int num_events,
3229 s32_t timeout);
3230
3231/**
3232 * @brief Submit a triggered work item to the system workqueue.
3233 *
3234 * This routine schedules work item @a work to be processed by system
3235 * workqueue when one of the given @a events is signaled. The routine
3236 * initiates internal poller for the work item and then returns to the caller.
3237 * Only when one of the watched events happen the work item is actually
3238 * submitted to the workqueue and becomes pending.
3239 *
3240 * Submitting a previously submitted triggered work item that is still
3241 * waiting for the event cancels the existing submission and reschedules it
3242 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003243 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003244 * so care must be taken to synchronize such resubmissions externally.
3245 *
3246 * @note Can be called by ISRs.
3247 *
3248 * @warning
3249 * Provided array of events as well as a triggered work item must not be
3250 * modified until the item has been processed by the workqueue.
3251 *
3252 * @param work Address of delayed work item.
3253 * @param events An array of pointers to events which trigger the work.
3254 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003255 * @param timeout Non-negative timeout after which the work will be scheduled
3256 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003257 *
3258 * @retval 0 Work item started watching for events.
3259 * @retval -EINVAL Work item is being processed or has completed its work.
3260 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3261 */
3262static inline int k_work_poll_submit(struct k_work_poll *work,
3263 struct k_poll_event *events,
3264 int num_events,
3265 s32_t timeout)
3266{
3267 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3268 events, num_events, timeout);
3269}
3270
3271/**
3272 * @brief Cancel a triggered work item.
3273 *
3274 * This routine cancels the submission of triggered work item @a work.
3275 * A triggered work item can only be canceled if no event triggered work
3276 * submission.
3277 *
3278 * @note Can be called by ISRs.
3279 *
3280 * @param work Address of delayed work item.
3281 *
David B. Kinder73896c02019-10-28 16:27:57 -07003282 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003283 * @retval -EINVAL Work item is being processed or has completed its work.
3284 */
3285extern int k_work_poll_cancel(struct k_work_poll *work);
3286
Anas Nashif166f5192018-02-25 08:02:36 -06003287/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003288/**
Anas Nashifce78d162018-05-24 12:43:11 -05003289 * @defgroup mutex_apis Mutex APIs
3290 * @ingroup kernel_apis
3291 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003292 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293
Anas Nashifce78d162018-05-24 12:43:11 -05003294/**
3295 * Mutex Structure
3296 * @ingroup mutex_apis
3297 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05003299 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003301 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003302 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05003303
3304 /** Current lock count */
Kumar Galacc334c72017-04-21 10:55:34 -05003305 u32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05003306
3307 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003310 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003311 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312};
3313
Anas Nashifce78d162018-05-24 12:43:11 -05003314/**
3315 * @cond INTERNAL_HIDDEN
3316 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003317#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003319 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003320 .owner = NULL, \
3321 .lock_count = 0, \
3322 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003323 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324 }
3325
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003326#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003327
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003328/**
Allan Stephensc98da842016-11-11 15:45:03 -05003329 * INTERNAL_HIDDEN @endcond
3330 */
3331
3332/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003333 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003336 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003337 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003340 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003341 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003343 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003344 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003346/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * Upon completion, the mutex is available and does not have an owner.
3352 *
3353 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003354 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003355 * @retval 0 Mutex object created
3356 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003357 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003358 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003359__syscall int k_mutex_init(struct k_mutex *mutex);
3360
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003361
3362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * This routine locks @a mutex. If the mutex is locked by another thread,
3366 * the calling thread waits until the mutex becomes available or until
3367 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * A thread is permitted to lock a mutex it has already locked. The operation
3370 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003373 * @param timeout Non-negative waiting period to lock the mutex (in
3374 * milliseconds), or one of the special values K_NO_WAIT and
3375 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003376 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003377 * @retval 0 Mutex locked.
3378 * @retval -EBUSY Returned without waiting.
3379 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003380 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003381 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003382__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003383
3384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * This routine unlocks @a mutex. The mutex must already be locked by the
3388 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003389 *
3390 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003392 * thread.
3393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003395 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04003396 * @retval 0 Mutex unlocked.
3397 * @retval -EPERM The current thread does not own the mutex
3398 * @retval -EINVAL The mutex is not locked
3399 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003400 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003401 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04003402__syscall int k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003403
Allan Stephensc98da842016-11-11 15:45:03 -05003404/**
Anas Nashif166f5192018-02-25 08:02:36 -06003405 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003406 */
3407
3408/**
3409 * @cond INTERNAL_HIDDEN
3410 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411
3412struct k_sem {
3413 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303414 u32_t count;
3415 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003416 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003418 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003419 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420};
3421
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003422#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003423 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003424 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003425 .count = initial_count, \
3426 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003427 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003428 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003429 }
3430
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003431#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003432
Allan Stephensc98da842016-11-11 15:45:03 -05003433/**
3434 * INTERNAL_HIDDEN @endcond
3435 */
3436
3437/**
3438 * @defgroup semaphore_apis Semaphore APIs
3439 * @ingroup kernel_apis
3440 * @{
3441 */
3442
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * @param sem Address of the semaphore.
3449 * @param initial_count Initial semaphore count.
3450 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003451 *
Anas Nashif928af3c2019-05-04 10:36:14 -04003452 * @retval 0 Semaphore created successfully
3453 * @retval -EINVAL Invalid values
3454 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003455 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003456 */
Anas Nashif928af3c2019-05-04 10:36:14 -04003457__syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count,
Andrew Boie99280232017-09-29 14:17:47 -07003458 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003459
3460/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003463 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003465 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3466 *
3467 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003468 * @param timeout Non-negative waiting period to take the semaphore (in
3469 * milliseconds), or one of the special values K_NO_WAIT and
3470 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003471 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003472 * @retval 0 Semaphore taken.
3473 * @retval -EBUSY Returned without waiting.
3474 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003475 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003476 */
Andrew Boie99280232017-09-29 14:17:47 -07003477__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003478
3479/**
3480 * @brief Give a semaphore.
3481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * This routine gives @a sem, unless the semaphore is already at its maximum
3483 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003485 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003487 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003488 *
3489 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003490 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003491 */
Andrew Boie99280232017-09-29 14:17:47 -07003492__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003494/**
3495 * @brief Reset a semaphore's count to zero.
3496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003500 *
3501 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003502 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003503 */
Andrew Boie990bf162017-10-03 12:36:49 -07003504__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003505
Anas Nashif954d5502018-02-25 08:37:28 -06003506/**
3507 * @internal
3508 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003509static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510{
Patrik Flykt24d71432019-03-26 19:57:45 -06003511 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512}
3513
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003514/**
3515 * @brief Get a semaphore's count.
3516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003517 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003522 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003523 */
Andrew Boie990bf162017-10-03 12:36:49 -07003524__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003525
Anas Nashif954d5502018-02-25 08:37:28 -06003526/**
3527 * @internal
3528 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003529static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003530{
3531 return sem->count;
3532}
3533
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003534/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003538 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003539 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * @param name Name of the semaphore.
3542 * @param initial_count Initial semaphore count.
3543 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003544 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003545 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003547 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003548 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303549 BUILD_ASSERT(((count_limit) != 0) && \
3550 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551
Anas Nashif166f5192018-02-25 08:02:36 -06003552/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003553
3554/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003555 * @defgroup msgq_apis Message Queue APIs
3556 * @ingroup kernel_apis
3557 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003558 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003559
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003560/**
3561 * @brief Message Queue Structure
3562 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003563struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05003564 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003565 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05003566 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07003567 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05003568 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003569 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003570 /** Maximal number of messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003571 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003572 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003573 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05003574 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003575 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05003576 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003577 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003578 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003580 /** Number of used messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003581 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003582
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003583 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003584 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05003585
3586 /** Message queue */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003587 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003588};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003589/**
3590 * @cond INTERNAL_HIDDEN
3591 */
3592
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003594#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003595 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003596 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003597 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003598 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003599 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003600 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003601 .read_ptr = q_buffer, \
3602 .write_ptr = q_buffer, \
3603 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003604 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003605 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003606#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003607/**
3608 * INTERNAL_HIDDEN @endcond
3609 */
3610
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003611
Andrew Boie0fe789f2018-04-12 18:35:56 -07003612#define K_MSGQ_FLAG_ALLOC BIT(0)
3613
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003614/**
3615 * @brief Message Queue Attributes
3616 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303617struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05003618 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303619 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003620 /** Maximal number of messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303621 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003622 /** Used messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303623 u32_t used_msgs;
3624};
3625
Allan Stephensc98da842016-11-11 15:45:03 -05003626
3627/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003628 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003629 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003630 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3631 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003632 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3633 * message is similarly aligned to this boundary, @a q_msg_size must also be
3634 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * The message queue can be accessed outside the module where it is defined
3637 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003638 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003639 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003641 * @param q_name Name of the message queue.
3642 * @param q_msg_size Message size (in bytes).
3643 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003644 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003645 *
3646 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003647 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003648#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3649 static char __noinit __aligned(q_align) \
3650 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3651 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3652 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003653 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003654
Peter Mitsisd7a37502016-10-13 11:37:40 -04003655/**
3656 * @brief Initialize a message queue.
3657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * This routine initializes a message queue object, prior to its first use.
3659 *
Allan Stephensda827222016-11-09 14:23:58 -06003660 * The message queue's ring buffer must contain space for @a max_msgs messages,
3661 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3662 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3663 * that each message is similarly aligned to this boundary, @a q_msg_size
3664 * must also be a multiple of N.
3665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * @param q Address of the message queue.
3667 * @param buffer Pointer to ring buffer that holds queued messages.
3668 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003669 * @param max_msgs Maximum number of messages that can be queued.
3670 *
3671 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003672 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003673 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003674void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3675 u32_t max_msgs);
3676
3677/**
3678 * @brief Initialize a message queue.
3679 *
3680 * This routine initializes a message queue object, prior to its first use,
3681 * allocating its internal ring buffer from the calling thread's resource
3682 * pool.
3683 *
3684 * Memory allocated for the ring buffer can be released by calling
3685 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3686 * all of its references.
3687 *
Anas Nashif4b386592019-11-25 09:30:47 -05003688 * @param msgq Address of the message queue.
Andrew Boie0fe789f2018-04-12 18:35:56 -07003689 * @param msg_size Message size (in bytes).
3690 * @param max_msgs Maximum number of messages that can be queued.
3691 *
3692 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3693 * thread's resource pool, or -EINVAL if the size parameters cause
3694 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003695 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003696 */
Anas Nashif4b386592019-11-25 09:30:47 -05003697__syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size,
Andrew Boie0fe789f2018-04-12 18:35:56 -07003698 u32_t max_msgs);
3699
Anas Nashife71293e2019-12-04 20:00:14 -05003700/**
Anas Nashif4b386592019-11-25 09:30:47 -05003701 * @brief Release allocated buffer for a queue
Anas Nashife71293e2019-12-04 20:00:14 -05003702 *
3703 * Releases memory allocated for the ring buffer.
Anas Nashif4b386592019-11-25 09:30:47 -05003704 *
3705 * @param msgq message queue to cleanup
3706 *
Anas Nashif11b93652019-06-16 08:43:48 -04003707 * @retval 0 on success
3708 * @retval -EBUSY Queue not empty
Anas Nashife71293e2019-12-04 20:00:14 -05003709 */
Anas Nashif11b93652019-06-16 08:43:48 -04003710int k_msgq_cleanup(struct k_msgq *msgq);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003711
3712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003713 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003715 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003716 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003717 * @note Can be called by ISRs.
3718 *
Anas Nashif4b386592019-11-25 09:30:47 -05003719 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003720 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003721 * @param timeout Non-negative waiting period to add the message (in
3722 * milliseconds), or one of the special values K_NO_WAIT and
3723 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003724 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003725 * @retval 0 Message sent.
3726 * @retval -ENOMSG Returned without waiting or queue purged.
3727 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003728 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003729 */
Anas Nashif4b386592019-11-25 09:30:47 -05003730__syscall int k_msgq_put(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003731
3732/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003733 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003735 * This routine receives a message from message queue @a q in a "first in,
3736 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003737 *
Allan Stephensc98da842016-11-11 15:45:03 -05003738 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003739 *
Anas Nashif4b386592019-11-25 09:30:47 -05003740 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003742 * @param timeout Non-negative waiting period to receive the message (in
3743 * milliseconds), or one of the special values K_NO_WAIT and
3744 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003745 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003746 * @retval 0 Message received.
3747 * @retval -ENOMSG Returned without waiting.
3748 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003749 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003750 */
Anas Nashif4b386592019-11-25 09:30:47 -05003751__syscall int k_msgq_get(struct k_msgq *msgq, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003752
3753/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003754 * @brief Peek/read a message from a message queue.
3755 *
3756 * This routine reads a message from message queue @a q in a "first in,
3757 * first out" manner and leaves the message in the queue.
3758 *
3759 * @note Can be called by ISRs.
3760 *
Anas Nashif4b386592019-11-25 09:30:47 -05003761 * @param msgq Address of the message queue.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003762 * @param data Address of area to hold the message read from the queue.
3763 *
3764 * @retval 0 Message read.
3765 * @retval -ENOMSG Returned when the queue has no message.
3766 * @req K-MSGQ-002
3767 */
Anas Nashif4b386592019-11-25 09:30:47 -05003768__syscall int k_msgq_peek(struct k_msgq *msgq, void *data);
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003769
3770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003771 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003773 * This routine discards all unreceived messages in a message queue's ring
3774 * buffer. Any threads that are blocked waiting to send a message to the
3775 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003776 *
Anas Nashif4b386592019-11-25 09:30:47 -05003777 * @param msgq Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003778 *
3779 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003780 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003781 */
Anas Nashif4b386592019-11-25 09:30:47 -05003782__syscall void k_msgq_purge(struct k_msgq *msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003783
Peter Mitsis67be2492016-10-07 11:44:34 -04003784/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * This routine returns the number of unused entries in a message queue's
3788 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003789 *
Anas Nashif4b386592019-11-25 09:30:47 -05003790 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 *
3792 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003793 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003794 */
Anas Nashif4b386592019-11-25 09:30:47 -05003795__syscall u32_t k_msgq_num_free_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003796
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303797/**
3798 * @brief Get basic attributes of a message queue.
3799 *
3800 * This routine fetches basic attributes of message queue into attr argument.
3801 *
Anas Nashif4b386592019-11-25 09:30:47 -05003802 * @param msgq Address of the message queue.
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303803 * @param attrs pointer to message queue attribute structure.
3804 *
3805 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003806 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303807 */
Anas Nashif4b386592019-11-25 09:30:47 -05003808__syscall void k_msgq_get_attrs(struct k_msgq *msgq,
3809 struct k_msgq_attrs *attrs);
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303810
3811
Anas Nashif4b386592019-11-25 09:30:47 -05003812static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq)
Peter Mitsis67be2492016-10-07 11:44:34 -04003813{
Anas Nashif4b386592019-11-25 09:30:47 -05003814 return msgq->max_msgs - msgq->used_msgs;
Peter Mitsis67be2492016-10-07 11:44:34 -04003815}
3816
Peter Mitsisd7a37502016-10-13 11:37:40 -04003817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003818 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003820 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003821 *
Anas Nashif4b386592019-11-25 09:30:47 -05003822 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003823 *
3824 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003825 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003826 */
Anas Nashif4b386592019-11-25 09:30:47 -05003827__syscall u32_t k_msgq_num_used_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07003828
Anas Nashif4b386592019-11-25 09:30:47 -05003829static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003830{
Anas Nashif4b386592019-11-25 09:30:47 -05003831 return msgq->used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003832}
3833
Anas Nashif166f5192018-02-25 08:02:36 -06003834/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003835
3836/**
3837 * @defgroup mem_pool_apis Memory Pool APIs
3838 * @ingroup kernel_apis
3839 * @{
3840 */
3841
Andy Ross73cb9582017-05-09 10:42:39 -07003842/* Note on sizing: the use of a 20 bit field for block means that,
3843 * assuming a reasonable minimum block size of 16 bytes, we're limited
3844 * to 16M of memory managed by a single pool. Long term it would be
3845 * good to move to a variable bit size based on configuration.
3846 */
3847struct k_mem_block_id {
3848 u32_t pool : 8;
3849 u32_t level : 4;
3850 u32_t block : 20;
3851};
3852
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003853struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003854 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003855 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856};
3857
Anas Nashif166f5192018-02-25 08:02:36 -06003858/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003859
3860/**
3861 * @defgroup mailbox_apis Mailbox APIs
3862 * @ingroup kernel_apis
3863 * @{
3864 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003865
Anas Nashife71293e2019-12-04 20:00:14 -05003866/**
3867 * @brief Mailbox Message Structure
3868 *
3869 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870struct k_mbox_msg {
3871 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003872 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003874 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003876 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003877 /** sender's message data buffer */
3878 void *tx_data;
3879 /** internal use only - needed for legacy API support */
3880 void *_rx_data;
3881 /** message data block descriptor */
3882 struct k_mem_block tx_block;
3883 /** source thread id */
3884 k_tid_t rx_source_thread;
3885 /** target thread id */
3886 k_tid_t tx_target_thread;
3887 /** internal use only - thread waiting on send (may be a dummy) */
3888 k_tid_t _syncing_thread;
3889#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3890 /** internal use only - semaphore used during asynchronous send */
3891 struct k_sem *_async_sem;
3892#endif
3893};
Anas Nashife71293e2019-12-04 20:00:14 -05003894/**
3895 * @brief Mailbox Structure
3896 *
3897 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05003899 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003900 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05003901 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003902 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003903 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003904
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003905 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003906 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003907};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003908/**
3909 * @cond INTERNAL_HIDDEN
3910 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003911
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003912#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003913 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003914 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3915 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003916 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003917 }
3918
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003919#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003920
Peter Mitsis12092702016-10-14 12:57:23 -04003921/**
Allan Stephensc98da842016-11-11 15:45:03 -05003922 * INTERNAL_HIDDEN @endcond
3923 */
3924
3925/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003926 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003928 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003929 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003930 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003933 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003934 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003935#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003936 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003937 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938
Peter Mitsis12092702016-10-14 12:57:23 -04003939/**
3940 * @brief Initialize a mailbox.
3941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003942 * This routine initializes a mailbox object, prior to its first use.
3943 *
3944 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003945 *
3946 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003947 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003948 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003949extern void k_mbox_init(struct k_mbox *mbox);
3950
Peter Mitsis12092702016-10-14 12:57:23 -04003951/**
3952 * @brief Send a mailbox message in a synchronous manner.
3953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003954 * This routine sends a message to @a mbox and waits for a receiver to both
3955 * receive and process it. The message data may be in a buffer, in a memory
3956 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003958 * @param mbox Address of the mailbox.
3959 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003960 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003961 * milliseconds), or one of the special values K_NO_WAIT
3962 * and K_FOREVER. Once the message has been received,
3963 * this routine waits as long as necessary for the message
3964 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003965 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003966 * @retval 0 Message sent.
3967 * @retval -ENOMSG Returned without waiting.
3968 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003969 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003970 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003971extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003972 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003973
Peter Mitsis12092702016-10-14 12:57:23 -04003974/**
3975 * @brief Send a mailbox message in an asynchronous manner.
3976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003977 * This routine sends a message to @a mbox without waiting for a receiver
3978 * to process it. The message data may be in a buffer, in a memory pool block,
3979 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3980 * will be given when the message has been both received and completely
3981 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * @param mbox Address of the mailbox.
3984 * @param tx_msg Address of the transmit message descriptor.
3985 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003986 *
3987 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003988 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003989 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003990extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003991 struct k_sem *sem);
3992
Peter Mitsis12092702016-10-14 12:57:23 -04003993/**
3994 * @brief Receive a mailbox message.
3995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * This routine receives a message from @a mbox, then optionally retrieves
3997 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * @param mbox Address of the mailbox.
4000 * @param rx_msg Address of the receive message descriptor.
4001 * @param buffer Address of the buffer to receive data, or NULL to defer data
4002 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004003 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004004 * milliseconds), or one of the special values K_NO_WAIT
4005 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004006 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004007 * @retval 0 Message received.
4008 * @retval -ENOMSG Returned without waiting.
4009 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004010 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004011 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004012extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05004013 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004014
4015/**
4016 * @brief Retrieve mailbox message data into a buffer.
4017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004018 * This routine completes the processing of a received message by retrieving
4019 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004020 *
4021 * Alternatively, this routine can be used to dispose of a received message
4022 * without retrieving its data.
4023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * @param rx_msg Address of the receive message descriptor.
4025 * @param buffer Address of the buffer to receive data, or NULL to discard
4026 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04004027 *
4028 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004029 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004030 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004031extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04004032
4033/**
4034 * @brief Retrieve mailbox message data into a memory pool block.
4035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004036 * This routine completes the processing of a received message by retrieving
4037 * its data into a memory pool block, then disposing of the message.
4038 * The memory pool block that results from successful retrieval must be
4039 * returned to the pool once the data has been processed, even in cases
4040 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04004041 *
4042 * Alternatively, this routine can be used to dispose of a received message
4043 * without retrieving its data. In this case there is no need to return a
4044 * memory pool block to the pool.
4045 *
4046 * This routine allocates a new memory pool block for the data only if the
4047 * data is not already in one. If a new block cannot be allocated, the routine
4048 * returns a failure code and the received message is left unchanged. This
4049 * permits the caller to reattempt data retrieval at a later time or to dispose
4050 * of the received message without retrieving its data.
4051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004052 * @param rx_msg Address of a receive message descriptor.
4053 * @param pool Address of memory pool, or NULL to discard data.
4054 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004055 * @param timeout Non-negative waiting period to wait for a memory pool block
4056 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004057 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004058 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004059 * @retval 0 Data retrieved.
4060 * @retval -ENOMEM Returned without waiting.
4061 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004062 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004063 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004064extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04004065 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05004066 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004067
Anas Nashif166f5192018-02-25 08:02:36 -06004068/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004069
4070/**
Anas Nashifce78d162018-05-24 12:43:11 -05004071 * @defgroup pipe_apis Pipe APIs
4072 * @ingroup kernel_apis
4073 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004074 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004075
Anas Nashifce78d162018-05-24 12:43:11 -05004076/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004077struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004078 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4079 size_t size; /**< Buffer size */
4080 size_t bytes_used; /**< # bytes used in buffer */
4081 size_t read_index; /**< Where in buffer to read from */
4082 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004083 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004084
4085 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004086 _wait_q_t readers; /**< Reader wait queue */
4087 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004088 } wait_q;
4089
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004090 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004091 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05004092 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004093};
4094
Anas Nashifce78d162018-05-24 12:43:11 -05004095/**
4096 * @cond INTERNAL_HIDDEN
4097 */
4098#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4099
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004100#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
4101 { \
4102 .buffer = pipe_buffer, \
4103 .size = pipe_buffer_size, \
4104 .bytes_used = 0, \
4105 .read_index = 0, \
4106 .write_index = 0, \
4107 .lock = {}, \
4108 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004109 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4110 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004111 }, \
4112 _OBJECT_TRACING_INIT \
4113 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004114 }
4115
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004116#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004117
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004118/**
Allan Stephensc98da842016-11-11 15:45:03 -05004119 * INTERNAL_HIDDEN @endcond
4120 */
4121
4122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004123 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004125 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004126 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004127 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004129 * @param name Name of the pipe.
4130 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4131 * or zero if no ring buffer is used.
4132 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004133 *
4134 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004135 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004136#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004137 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004138 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004139 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004140 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004141
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004142/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004143 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004145 * This routine initializes a pipe object, prior to its first use.
4146 *
4147 * @param pipe Address of the pipe.
4148 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4149 * is used.
4150 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4151 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004152 *
4153 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004154 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004155 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004156void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4157
4158/**
4159 * @brief Release a pipe's allocated buffer
4160 *
4161 * If a pipe object was given a dynamically allocated buffer via
4162 * k_pipe_alloc_init(), this will free it. This function does nothing
4163 * if the buffer wasn't dynamically allocated.
4164 *
4165 * @param pipe Address of the pipe.
Anas Nashif361a84d2019-06-16 08:22:08 -04004166 * @retval 0 on success
4167 * @retval -EAGAIN nothing to cleanup
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004168 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004169 */
Anas Nashif361a84d2019-06-16 08:22:08 -04004170int k_pipe_cleanup(struct k_pipe *pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07004171
4172/**
4173 * @brief Initialize a pipe and allocate a buffer for it
4174 *
4175 * Storage for the buffer region will be allocated from the calling thread's
4176 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4177 * or userspace is enabled and the pipe object loses all references to it.
4178 *
4179 * This function should only be called on uninitialized pipe objects.
4180 *
4181 * @param pipe Address of the pipe.
4182 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4183 * buffer is used.
4184 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004185 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004186 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004187 */
4188__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004189
4190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004191 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004193 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004195 * @param pipe Address of the pipe.
4196 * @param data Address of data to write.
4197 * @param bytes_to_write Size of data (in bytes).
4198 * @param bytes_written Address of area to hold the number of bytes written.
4199 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004200 * @param timeout Non-negative waiting period to wait for the data to be written
4201 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004202 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004203 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004204 * @retval 0 At least @a min_xfer bytes of data were written.
4205 * @retval -EIO Returned without waiting; zero data bytes were written.
4206 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004207 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004208 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004209 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004210__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4211 size_t bytes_to_write, size_t *bytes_written,
4212 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004213
4214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004215 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004217 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004219 * @param pipe Address of the pipe.
4220 * @param data Address to place the data read from pipe.
4221 * @param bytes_to_read Maximum number of data bytes to read.
4222 * @param bytes_read Address of area to hold the number of bytes read.
4223 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004224 * @param timeout Non-negative waiting period to wait for the data to be read
4225 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004226 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004227 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004228 * @retval 0 At least @a min_xfer bytes of data were read.
Anas Nashif361a84d2019-06-16 08:22:08 -04004229 * @retval -EINVAL invalid parameters supplied
Allan Stephens9ef50f42016-11-16 15:33:31 -05004230 * @retval -EIO Returned without waiting; zero data bytes were read.
4231 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004232 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004233 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004234 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004235__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4236 size_t bytes_to_read, size_t *bytes_read,
4237 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004238
4239/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004240 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004242 * This routine writes the data contained in a memory block to @a pipe.
4243 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004244 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004246 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004247 * @param block Memory block containing data to send
4248 * @param size Number of data bytes in memory block to send
4249 * @param sem Semaphore to signal upon completion (else NULL)
4250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004251 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004252 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004253 */
4254extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4255 size_t size, struct k_sem *sem);
4256
Anas Nashif166f5192018-02-25 08:02:36 -06004257/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004258
Allan Stephensc98da842016-11-11 15:45:03 -05004259/**
4260 * @cond INTERNAL_HIDDEN
4261 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004262
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004263struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004264 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004265 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004266 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004267 char *buffer;
4268 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004269 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004270
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004271 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004272 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004273};
4274
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004275#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004276 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004277 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004278 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004279 .num_blocks = slab_num_blocks, \
4280 .block_size = slab_block_size, \
4281 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004282 .free_list = NULL, \
4283 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004284 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004285 }
4286
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004287#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004288
4289
Peter Mitsis578f9112016-10-07 13:50:31 -04004290/**
Allan Stephensc98da842016-11-11 15:45:03 -05004291 * INTERNAL_HIDDEN @endcond
4292 */
4293
4294/**
4295 * @defgroup mem_slab_apis Memory Slab APIs
4296 * @ingroup kernel_apis
4297 * @{
4298 */
4299
4300/**
Allan Stephensda827222016-11-09 14:23:58 -06004301 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004302 *
Allan Stephensda827222016-11-09 14:23:58 -06004303 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004304 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004305 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4306 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004307 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004308 *
Allan Stephensda827222016-11-09 14:23:58 -06004309 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004310 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004311 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004312 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004314 * @param name Name of the memory slab.
4315 * @param slab_block_size Size of each memory block (in bytes).
4316 * @param slab_num_blocks Number memory blocks.
4317 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004318 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004319 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004320#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004321 char __noinit __aligned(WB_UP(slab_align)) \
4322 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004323 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004324 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004325 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004326
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004327/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004328 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004330 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004331 *
Allan Stephensda827222016-11-09 14:23:58 -06004332 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4333 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004334 * N-byte boundary matching a word boundary, where N is a power of 2
4335 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004336 * To ensure that each memory block is similarly aligned to this boundary,
4337 * @a slab_block_size must also be a multiple of N.
4338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004339 * @param slab Address of the memory slab.
4340 * @param buffer Pointer to buffer used for the memory blocks.
4341 * @param block_size Size of each memory block (in bytes).
4342 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004343 *
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004344 * @retval 0 on success
4345 * @retval -EINVAL invalid data supplied
4346 *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004347 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004348 */
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004349extern int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004350 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004351
4352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004353 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004355 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004357 * @param slab Address of the memory slab.
4358 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004359 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004360 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4361 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004362 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004363 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004364 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004365 * @retval -ENOMEM Returned without waiting.
4366 * @retval -EAGAIN Waiting period timed out.
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004367 * @retval -EINVAL Invalid data supplied
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004368 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004369 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004370extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004371 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004372
4373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004374 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004376 * This routine releases a previously allocated memory block back to its
4377 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004379 * @param slab Address of the memory slab.
4380 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004381 *
4382 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004383 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004384 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004385extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004386
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004388 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004390 * This routine gets the number of memory blocks that are currently
4391 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004393 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004395 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004396 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004397 */
Kumar Galacc334c72017-04-21 10:55:34 -05004398static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004399{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004400 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004401}
4402
Peter Mitsisc001aa82016-10-13 13:53:37 -04004403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004404 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004406 * This routine gets the number of memory blocks that are currently
4407 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004409 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004411 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004412 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004413 */
Kumar Galacc334c72017-04-21 10:55:34 -05004414static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004415{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004416 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004417}
4418
Anas Nashif166f5192018-02-25 08:02:36 -06004419/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004420
4421/**
4422 * @cond INTERNAL_HIDDEN
4423 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004424
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004425struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004426 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004427 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004428};
4429
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004430/**
Allan Stephensc98da842016-11-11 15:45:03 -05004431 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004432 */
4433
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004434/**
Allan Stephensc98da842016-11-11 15:45:03 -05004435 * @addtogroup mem_pool_apis
4436 * @{
4437 */
4438
4439/**
4440 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004442 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4443 * long. The memory pool allows blocks to be repeatedly partitioned into
4444 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004445 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004446 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004447 * If the pool is to be accessed outside the module where it is defined, it
4448 * can be declared via
4449 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004450 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004452 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004453 * @param minsz Size of the smallest blocks in the pool (in bytes).
4454 * @param maxsz Size of the largest blocks in the pool (in bytes).
4455 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004456 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004457 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004458 */
Andy Ross73cb9582017-05-09 10:42:39 -07004459#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004460 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004461 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004462 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004463 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004464 .base = { \
4465 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004466 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004467 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004468 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004469 .levels = _mpool_lvls_##name, \
4470 .flags = SYS_MEM_POOL_KERNEL \
4471 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004472 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004473 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004474
Peter Mitsis937042c2016-10-13 13:18:26 -04004475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004476 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004478 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004480 * @param pool Address of the memory pool.
4481 * @param block Pointer to block descriptor for the allocated memory.
4482 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004483 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004484 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4485 * or K_FOREVER to wait as long as necessary.
4486 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004487 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004488 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004489 * @retval -ENOMEM Returned without waiting.
4490 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004491 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004492 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004493extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004494 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004495
4496/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004497 * @brief Allocate memory from a memory pool with malloc() semantics
4498 *
4499 * Such memory must be released using k_free().
4500 *
4501 * @param pool Address of the memory pool.
4502 * @param size Amount of memory to allocate (in bytes).
4503 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004504 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004505 */
4506extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4507
4508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004509 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004511 * This routine releases a previously allocated memory block back to its
4512 * memory pool.
4513 *
4514 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004515 *
4516 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004517 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004518 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004519extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004520
4521/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004522 * @brief Free memory allocated from a memory pool.
4523 *
4524 * This routine releases a previously allocated memory block back to its
4525 * memory pool
4526 *
4527 * @param id Memory block identifier.
4528 *
4529 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004530 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004531 */
4532extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4533
4534/**
Anas Nashif166f5192018-02-25 08:02:36 -06004535 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004536 */
4537
4538/**
4539 * @defgroup heap_apis Heap Memory Pool APIs
4540 * @ingroup kernel_apis
4541 * @{
4542 */
4543
4544/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004545 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004547 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004548 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004550 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004552 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004553 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004554 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004555extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004556
4557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004558 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004559 *
4560 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004561 * returned must have been allocated from the heap memory pool or
4562 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004563 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004564 * If @a ptr is NULL, no operation is performed.
4565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004566 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004567 *
4568 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004569 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004570 */
4571extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004572
Allan Stephensc98da842016-11-11 15:45:03 -05004573/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004574 * @brief Allocate memory from heap, array style
4575 *
4576 * This routine provides traditional calloc() semantics. Memory is
4577 * allocated from the heap memory pool and zeroed.
4578 *
4579 * @param nmemb Number of elements in the requested array
4580 * @param size Size of each array element (in bytes).
4581 *
4582 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004583 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004584 */
4585extern void *k_calloc(size_t nmemb, size_t size);
4586
Anas Nashif166f5192018-02-25 08:02:36 -06004587/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004588
Benjamin Walshacc68c12017-01-29 18:57:45 -05004589/* polling API - PRIVATE */
4590
Benjamin Walshb0179862017-02-02 16:39:57 -05004591#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004592#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004593#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004594#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004595#endif
4596
Benjamin Walshacc68c12017-01-29 18:57:45 -05004597/* private - types bit positions */
4598enum _poll_types_bits {
4599 /* can be used to ignore an event */
4600 _POLL_TYPE_IGNORE,
4601
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004602 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004603 _POLL_TYPE_SIGNAL,
4604
4605 /* semaphore availability */
4606 _POLL_TYPE_SEM_AVAILABLE,
4607
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004608 /* queue/fifo/lifo data availability */
4609 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004610
4611 _POLL_NUM_TYPES
4612};
4613
Patrik Flykt4344e272019-03-08 14:19:05 -07004614#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004615
4616/* private - states bit positions */
4617enum _poll_states_bits {
4618 /* default state when creating event */
4619 _POLL_STATE_NOT_READY,
4620
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004621 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004622 _POLL_STATE_SIGNALED,
4623
4624 /* semaphore is available */
4625 _POLL_STATE_SEM_AVAILABLE,
4626
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004627 /* data is available to read on queue/fifo/lifo */
4628 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004629
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004630 /* queue/fifo/lifo wait was cancelled */
4631 _POLL_STATE_CANCELLED,
4632
Benjamin Walshacc68c12017-01-29 18:57:45 -05004633 _POLL_NUM_STATES
4634};
4635
Patrik Flykt4344e272019-03-08 14:19:05 -07004636#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004637
4638#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004639 (32 - (0 \
4640 + 8 /* tag */ \
4641 + _POLL_NUM_TYPES \
4642 + _POLL_NUM_STATES \
4643 + 1 /* modes */ \
4644 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004645
Benjamin Walshacc68c12017-01-29 18:57:45 -05004646/* end of polling API - PRIVATE */
4647
4648
4649/**
4650 * @defgroup poll_apis Async polling APIs
4651 * @ingroup kernel_apis
4652 * @{
4653 */
4654
4655/* Public polling API */
4656
4657/* public - values for k_poll_event.type bitfield */
4658#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004659#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4660#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4661#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004662#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004663
4664/* public - polling modes */
4665enum k_poll_modes {
4666 /* polling thread does not take ownership of objects when available */
4667 K_POLL_MODE_NOTIFY_ONLY = 0,
4668
4669 K_POLL_NUM_MODES
4670};
4671
4672/* public - values for k_poll_event.state bitfield */
4673#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004674#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4675#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4676#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004677#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004678#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004679
4680/* public - poll signal object */
4681struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05004682 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004683 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004684
Anas Nashife71293e2019-12-04 20:00:14 -05004685 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004686 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4687 * user resets it to 0.
4688 */
4689 unsigned int signaled;
4690
Anas Nashife71293e2019-12-04 20:00:14 -05004691 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004692 int result;
4693};
4694
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004695#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004696 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004697 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004698 .signaled = 0, \
4699 .result = 0, \
4700 }
Anas Nashife71293e2019-12-04 20:00:14 -05004701/**
4702 * @brief Poll Event
4703 *
4704 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004705struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05004706 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004707 sys_dnode_t _node;
4708
Anas Nashife71293e2019-12-04 20:00:14 -05004709 /** PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004710 struct _poller *poller;
4711
Anas Nashife71293e2019-12-04 20:00:14 -05004712 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004713 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004714
Anas Nashife71293e2019-12-04 20:00:14 -05004715 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004716 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004717
Anas Nashife71293e2019-12-04 20:00:14 -05004718 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004719 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004720
Anas Nashife71293e2019-12-04 20:00:14 -05004721 /** mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004722 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004723
Anas Nashife71293e2019-12-04 20:00:14 -05004724 /** unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004725 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004726
Anas Nashife71293e2019-12-04 20:00:14 -05004727 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004728 union {
4729 void *obj;
4730 struct k_poll_signal *signal;
4731 struct k_sem *sem;
4732 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004733 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004734 };
4735};
4736
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004737#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004738 { \
4739 .poller = NULL, \
4740 .type = event_type, \
4741 .state = K_POLL_STATE_NOT_READY, \
4742 .mode = event_mode, \
4743 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004744 { .obj = event_obj }, \
4745 }
4746
4747#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4748 event_tag) \
4749 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004750 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004751 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004752 .state = K_POLL_STATE_NOT_READY, \
4753 .mode = event_mode, \
4754 .unused = 0, \
4755 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004756 }
4757
4758/**
4759 * @brief Initialize one struct k_poll_event instance
4760 *
4761 * After this routine is called on a poll event, the event it ready to be
4762 * placed in an event array to be passed to k_poll().
4763 *
4764 * @param event The event to initialize.
4765 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4766 * values. Only values that apply to the same object being polled
4767 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4768 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004769 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004770 * @param obj Kernel object or poll signal.
4771 *
4772 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004773 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004774 */
4775
Kumar Galacc334c72017-04-21 10:55:34 -05004776extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004777 int mode, void *obj);
4778
4779/**
4780 * @brief Wait for one or many of multiple poll events to occur
4781 *
4782 * This routine allows a thread to wait concurrently for one or many of
4783 * multiple poll events to have occurred. Such events can be a kernel object
4784 * being available, like a semaphore, or a poll signal event.
4785 *
4786 * When an event notifies that a kernel object is available, the kernel object
4787 * is not "given" to the thread calling k_poll(): it merely signals the fact
4788 * that the object was available when the k_poll() call was in effect. Also,
4789 * all threads trying to acquire an object the regular way, i.e. by pending on
4790 * the object, have precedence over the thread polling on the object. This
4791 * means that the polling thread will never get the poll event on an object
4792 * until the object becomes available and its pend queue is empty. For this
4793 * reason, the k_poll() call is more effective when the objects being polled
4794 * only have one thread, the polling thread, trying to acquire them.
4795 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004796 * When k_poll() returns 0, the caller should loop on all the events that were
4797 * passed to k_poll() and check the state field for the values that were
4798 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004799 *
4800 * Before being reused for another call to k_poll(), the user has to reset the
4801 * state field to K_POLL_STATE_NOT_READY.
4802 *
Andrew Boie3772f772018-05-07 16:52:57 -07004803 * When called from user mode, a temporary memory allocation is required from
4804 * the caller's resource pool.
4805 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004806 * @param events An array of pointers to events to be polled for.
4807 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004808 * @param timeout Non-negative waiting period for an event to be ready (in
4809 * milliseconds), or one of the special values K_NO_WAIT and
4810 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004811 *
4812 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004813 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004814 * @retval -EINTR Polling has been interrupted, e.g. with
4815 * k_queue_cancel_wait(). All output events are still set and valid,
4816 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4817 * words, -EINTR status means that at least one of output events is
4818 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004819 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4820 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004821 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004822 */
4823
Andrew Boie3772f772018-05-07 16:52:57 -07004824__syscall int k_poll(struct k_poll_event *events, int num_events,
4825 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004826
4827/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004828 * @brief Initialize a poll signal object.
4829 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004830 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004831 *
4832 * @param signal A poll signal.
4833 *
4834 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004835 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004836 */
4837
Andrew Boie3772f772018-05-07 16:52:57 -07004838__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4839
4840/*
4841 * @brief Reset a poll signal object's state to unsignaled.
4842 *
4843 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004844 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004845 */
4846__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4847
Patrik Flykt4344e272019-03-08 14:19:05 -07004848static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004849{
Patrik Flykt24d71432019-03-26 19:57:45 -06004850 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004851}
4852
4853/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004854 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004855 *
4856 * @param signal A poll signal object
4857 * @param signaled An integer buffer which will be written nonzero if the
4858 * object was signaled
4859 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004860 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004861 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004862 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004863 */
4864__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4865 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004866
4867/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004868 * @brief Signal a poll signal object.
4869 *
4870 * This routine makes ready a poll signal, which is basically a poll event of
4871 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4872 * made ready to run. A @a result value can be specified.
4873 *
4874 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004875 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004876 * k_poll_signal_reset(). It thus has to be reset by the user before being
4877 * passed again to k_poll() or k_poll() will consider it being signaled, and
4878 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004879 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004880 * @note The result is stored and the 'signaled' field is set even if
4881 * this function returns an error indicating that an expiring poll was
4882 * not notified. The next k_poll() will detect the missed raise.
4883 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004884 * @param signal A poll signal.
4885 * @param result The value to store in the result field of the signal.
4886 *
4887 * @retval 0 The signal was delivered successfully.
4888 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004889 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004890 */
4891
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004892__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004893
Anas Nashif954d5502018-02-25 08:37:28 -06004894/**
4895 * @internal
4896 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004897extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004898
Anas Nashif166f5192018-02-25 08:02:36 -06004899/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004900
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004901/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004902 * @defgroup cpu_idle_apis CPU Idling APIs
4903 * @ingroup kernel_apis
4904 * @{
4905 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004906/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004907 * @brief Make the CPU idle.
4908 *
4909 * This function makes the CPU idle until an event wakes it up.
4910 *
4911 * In a regular system, the idle thread should be the only thread responsible
4912 * for making the CPU idle and triggering any type of power management.
4913 * However, in some more constrained systems, such as a single-threaded system,
4914 * the only thread would be responsible for this if needed.
4915 *
4916 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004917 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004918 */
Andrew Boie07525a32019-09-21 16:17:23 -07004919static inline void k_cpu_idle(void)
4920{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004921 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004922}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004923
4924/**
4925 * @brief Make the CPU idle in an atomic fashion.
4926 *
4927 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4928 * must be done atomically before making the CPU idle.
4929 *
4930 * @param key Interrupt locking key obtained from irq_lock().
4931 *
4932 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004933 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004934 */
Andrew Boie07525a32019-09-21 16:17:23 -07004935static inline void k_cpu_atomic_idle(unsigned int key)
4936{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004937 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004938}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004939
Anas Nashif30c3cff2019-01-22 08:18:13 -05004940/**
4941 * @}
4942 */
Anas Nashif954d5502018-02-25 08:37:28 -06004943
4944/**
4945 * @internal
4946 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004947extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004948
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004949#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004950/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004951#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004952#else
4953
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004954#if !defined(CONFIG_ASSERT_NO_FILE_INFO)
4955#define __EXCEPT_LOC() __ASSERT_PRINT("@ %s:%d\n", __FILE__, __LINE__)
4956#else
4957#define __EXCEPT_LOC()
4958#endif
4959
Andrew Boiecdb94d62017-04-18 15:22:05 -07004960/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004961 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004962 *
4963 * We won't have a real exception frame to determine the PC value when
4964 * the oops occurred, so print file and line number before we jump into
4965 * the fatal error handler.
4966 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004967#define z_except_reason(reason) do { \
Joakim Anderssone04e4c22019-12-20 15:42:38 +01004968 __EXCEPT_LOC(); \
Andrew Boie56236372019-07-15 15:22:29 -07004969 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004970 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004971
4972#endif /* _ARCH__EXCEPT */
4973
4974/**
4975 * @brief Fatally terminate a thread
4976 *
4977 * This should be called when a thread has encountered an unrecoverable
4978 * runtime condition and needs to terminate. What this ultimately
4979 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004980 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004981 *
4982 * If this is called from ISR context, the default system fatal error handler
4983 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004984 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004985 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004986#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004987
4988/**
4989 * @brief Fatally terminate the system
4990 *
4991 * This should be called when the Zephyr kernel has encountered an
4992 * unrecoverable runtime condition and needs to terminate. What this ultimately
4993 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004994 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004995 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004996 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004997#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004998
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004999/*
5000 * private APIs that are utilized by one or more public APIs
5001 */
5002
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09005003/**
5004 * @internal
5005 */
5006extern void z_init_thread_base(struct _thread_base *thread_base,
5007 int priority, u32_t initial_state,
5008 unsigned int options);
5009
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005010#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06005011/**
5012 * @internal
5013 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005014extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005015#else
Anas Nashif954d5502018-02-25 08:37:28 -06005016/**
5017 * @internal
5018 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005019#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005020#endif
5021
Anas Nashif954d5502018-02-25 08:37:28 -06005022/**
5023 * @internal
5024 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005025extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06005026/**
5027 * @internal
5028 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005029extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005030
Andrew Boiedc5d9352017-06-02 12:56:47 -07005031/* arch/cpu.h may declare an architecture or platform-specific macro
5032 * for properly declaring stacks, compatible with MMU/MPU constraints if
5033 * enabled
5034 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07005035
5036/**
5037 * @brief Obtain an extern reference to a stack
5038 *
5039 * This macro properly brings the symbol of a thread stack declared
5040 * elsewhere into scope.
5041 *
5042 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005043 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07005044 */
5045#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
5046
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005047#ifdef ARCH_THREAD_STACK_DEFINE
5048#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07005049#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005050 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
5051#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
5052#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
5053#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boieb5c68102019-11-21 17:38:17 -08005054#define K_THREAD_STACK_RESERVED ((size_t)ARCH_THREAD_STACK_RESERVED)
Andrew Boie4e5c0932019-04-04 12:05:28 -07005055static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005056{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005057 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07005058}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005059#else
5060/**
5061 * @brief Declare a toplevel thread stack memory region
5062 *
5063 * This declares a region of memory suitable for use as a thread's stack.
5064 *
5065 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5066 * 'noinit' section so that it isn't zeroed at boot
5067 *
Andrew Boie507852a2017-07-25 18:47:07 -07005068 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04005069 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07005070 * inside needs to be examined, examine thread->stack_info for the associated
5071 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005072 *
5073 * It is legal to precede this definition with the 'static' keyword.
5074 *
5075 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
5076 * parameter of k_thread_create(), it may not be the same as the
5077 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
5078 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005079 * Some arches may round the size of the usable stack region up to satisfy
5080 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
5081 * size.
5082 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07005083 * @param sym Thread stack symbol name
5084 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005085 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005086 */
5087#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005088 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005089
5090/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305091 * @brief Calculate size of stacks to be allocated in a stack array
5092 *
5093 * This macro calculates the size to be allocated for the stacks
5094 * inside a stack array. It accepts the indicated "size" as a parameter
5095 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
5096 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
5097 *
5098 * @param size Size of the stack memory region
5099 * @req K-TSTACK-001
5100 */
5101#define K_THREAD_STACK_LEN(size) (size)
5102
5103/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07005104 * @brief Declare a toplevel array of thread stack memory regions
5105 *
5106 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
5107 * definition for additional details and constraints.
5108 *
5109 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5110 * 'noinit' section so that it isn't zeroed at boot
5111 *
5112 * @param sym Thread stack symbol name
5113 * @param nmemb Number of stacks to declare
5114 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005115 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005116 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07005117#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005118 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305119 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005120
5121/**
5122 * @brief Declare an embedded stack memory region
5123 *
5124 * Used for stacks embedded within other data structures. Use is highly
5125 * discouraged but in some cases necessary. For memory protection scenarios,
5126 * it is very important that any RAM preceding this member not be writable
5127 * by threads else a stack overflow will lead to silent corruption. In other
5128 * words, the containing data structure should live in RAM owned by the kernel.
5129 *
5130 * @param sym Thread stack symbol name
5131 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005132 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005133 */
5134#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005135 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005136
5137/**
5138 * @brief Return the size in bytes of a stack memory region
5139 *
5140 * Convenience macro for passing the desired stack size to k_thread_create()
5141 * since the underlying implementation may actually create something larger
5142 * (for instance a guard area).
5143 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005144 * The value returned here is not guaranteed to match the 'size' parameter
5145 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005146 *
5147 * @param sym Stack memory symbol
5148 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005149 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005150 */
5151#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5152
Andrew Boie575abc02019-03-19 10:42:24 -07005153
5154/**
5155 * @brief Indicate how much additional memory is reserved for stack objects
5156 *
5157 * Any given stack declaration may have additional memory in it for guard
5158 * areas or supervisor mode stacks. This macro indicates how much space
5159 * is reserved for this. The memory reserved may not be contiguous within
5160 * the stack object, and does not account for additional space used due to
5161 * enforce alignment.
5162 */
Andrew Boieb5c68102019-11-21 17:38:17 -08005163#define K_THREAD_STACK_RESERVED ((size_t)0U)
Andrew Boie575abc02019-03-19 10:42:24 -07005164
Andrew Boiedc5d9352017-06-02 12:56:47 -07005165/**
5166 * @brief Get a pointer to the physical stack buffer
5167 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005168 * This macro is deprecated. If a stack buffer needs to be examined, the
5169 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005170 *
5171 * @param sym Declared stack symbol name
5172 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005173 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005174 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005175static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005176{
5177 return (char *)sym;
5178}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005179
5180#endif /* _ARCH_DECLARE_STACK */
5181
Chunlin Hane9c97022017-07-07 20:29:30 +08005182/**
5183 * @defgroup mem_domain_apis Memory domain APIs
5184 * @ingroup kernel_apis
5185 * @{
5186 */
5187
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005188/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005189 * @def K_MEM_PARTITION_DEFINE
5190 * @brief Used to declare a memory partition
5191 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005192 */
5193#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5194#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5195 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005196 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005197 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005198#else
5199#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005200 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005201 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005202#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5203
Chunlin Hane9c97022017-07-07 20:29:30 +08005204/* memory partition */
5205struct k_mem_partition {
Anas Nashife71293e2019-12-04 20:00:14 -05005206 /** start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005207 uintptr_t start;
Anas Nashife71293e2019-12-04 20:00:14 -05005208 /** size of memory partition */
Andrew Boiea8248212019-11-13 12:10:56 -08005209 size_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005210#if defined(CONFIG_MEMORY_PROTECTION)
Anas Nashife71293e2019-12-04 20:00:14 -05005211 /** attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305212 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005213#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005214};
5215
Anas Nashife71293e2019-12-04 20:00:14 -05005216/**
5217 * @brief Memory Domain
5218 *
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305219 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005220struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305221#ifdef CONFIG_USERSPACE
Anas Nashife71293e2019-12-04 20:00:14 -05005222 /** partitions in the domain */
Chunlin Hane9c97022017-07-07 20:29:30 +08005223 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305224#endif /* CONFIG_USERSPACE */
Anas Nashife71293e2019-12-04 20:00:14 -05005225 /** domain q */
Chunlin Hane9c97022017-07-07 20:29:30 +08005226 sys_dlist_t mem_domain_q;
Anas Nashife71293e2019-12-04 20:00:14 -05005227 /** number of partitions in the domain */
Leandro Pereira08de6582018-02-28 14:22:57 -08005228 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005229};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305230
Chunlin Hane9c97022017-07-07 20:29:30 +08005231
5232/**
5233 * @brief Initialize a memory domain.
5234 *
5235 * Initialize a memory domain with given name and memory partitions.
5236 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005237 * See documentation for k_mem_domain_add_partition() for details about
5238 * partition constraints.
5239 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005240 * @param domain The memory domain to be initialized.
5241 * @param num_parts The number of array items of "parts" parameter.
5242 * @param parts An array of pointers to the memory partitions. Can be NULL
5243 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005244 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005245 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005246extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005247 struct k_mem_partition *parts[]);
5248/**
5249 * @brief Destroy a memory domain.
5250 *
5251 * Destroy a memory domain.
5252 *
5253 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005254 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005255 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005256extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5257
5258/**
5259 * @brief Add a memory partition into a memory domain.
5260 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005261 * Add a memory partition into a memory domain. Partitions must conform to
5262 * the following constraints:
5263 *
5264 * - Partition bounds must be within system RAM boundaries on MMU-based
5265 * systems.
5266 * - Partitions in the same memory domain may not overlap each other.
5267 * - Partitions must not be defined which expose private kernel
5268 * data structures or kernel objects.
5269 * - The starting address alignment, and the partition size must conform to
5270 * the constraints of the underlying memory management hardware, which
5271 * varies per architecture.
5272 * - Memory domain partitions are only intended to control access to memory
5273 * from user mode threads.
5274 *
5275 * Violating these constraints may lead to CPU exceptions or undefined
5276 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005277 *
5278 * @param domain The memory domain to be added a memory partition.
5279 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005280 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005281 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005282extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5283 struct k_mem_partition *part);
5284
5285/**
5286 * @brief Remove a memory partition from a memory domain.
5287 *
5288 * Remove a memory partition from a memory domain.
5289 *
5290 * @param domain The memory domain to be removed a memory partition.
5291 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005292 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005293 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005294extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5295 struct k_mem_partition *part);
5296
5297/**
5298 * @brief Add a thread into a memory domain.
5299 *
5300 * Add a thread into a memory domain.
5301 *
5302 * @param domain The memory domain that the thread is going to be added into.
5303 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005304 *
5305 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005306 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005307extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5308 k_tid_t thread);
5309
5310/**
5311 * @brief Remove a thread from its memory domain.
5312 *
5313 * Remove a thread from its memory domain.
5314 *
5315 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005316 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005317 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005318extern void k_mem_domain_remove_thread(k_tid_t thread);
5319
Anas Nashif166f5192018-02-25 08:02:36 -06005320/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005321
Andrew Boied76ae462020-01-02 11:57:43 -08005322#ifdef CONFIG_PRINTK
Andrew Boie756f9072017-10-10 16:01:49 -07005323/**
5324 * @brief Emit a character buffer to the console device
5325 *
5326 * @param c String of characters to print
5327 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005328 *
5329 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005330 */
5331__syscall void k_str_out(char *c, size_t n);
Andrew Boied76ae462020-01-02 11:57:43 -08005332#endif
Andrew Boie756f9072017-10-10 16:01:49 -07005333
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005334/**
5335 * @brief Disable preservation of floating point context information.
5336 *
5337 * This routine informs the kernel that the specified thread
5338 * will no longer be using the floating point registers.
5339 *
5340 * @warning
5341 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005342 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005343 *
5344 * @warning
5345 * This routine should only be used to disable floating point support for
5346 * a thread that currently has such support enabled.
5347 *
5348 * @param thread ID of thread.
5349 *
5350 * @retval 0 On success.
5351 * @retval -ENOSYS If the floating point disabling is not implemented.
5352 * -EINVAL If the floating point disabling could not be performed.
5353 */
5354__syscall int k_float_disable(struct k_thread *thread);
5355
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005356#ifdef __cplusplus
5357}
5358#endif
5359
Anas Nashif73008b42020-02-06 09:14:51 -05005360#include <tracing/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005361#include <syscalls/kernel.h>
5362
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005363#endif /* !_ASMLANGUAGE */
5364
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005365#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */