blob: 978c08cbe916daae23505cd1f16d2b2f7cb411db [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/**
Anas Nashif166f5192018-02-25 08:02:36 -06001299 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001300 */
1301
1302/**
1303 * @addtogroup thread_apis
1304 * @{
1305 */
1306
1307/**
1308 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001309 *
Allan Stephensc98da842016-11-11 15:45:03 -05001310 * This routine prevents the current thread from being preempted by another
1311 * thread by instructing the scheduler to treat it as a cooperative thread.
1312 * If the thread subsequently performs an operation that makes it unready,
1313 * it will be context switched out in the normal manner. When the thread
1314 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001315 *
Allan Stephensc98da842016-11-11 15:45:03 -05001316 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001317 *
Allan Stephensc98da842016-11-11 15:45:03 -05001318 * @note k_sched_lock() and k_sched_unlock() should normally be used
1319 * when the operation being performed can be safely interrupted by ISRs.
1320 * However, if the amount of processing involved is very small, better
1321 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001322 *
1323 * @return N/A
1324 */
1325extern void k_sched_lock(void);
1326
Allan Stephensc98da842016-11-11 15:45:03 -05001327/**
1328 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001329 *
Allan Stephensc98da842016-11-11 15:45:03 -05001330 * This routine reverses the effect of a previous call to k_sched_lock().
1331 * A thread must call the routine once for each time it called k_sched_lock()
1332 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001333 *
1334 * @return N/A
1335 */
1336extern void k_sched_unlock(void);
1337
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001338/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001339 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001341 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001343 * Custom data is not used by the kernel itself, and is freely available
1344 * for a thread to use as it sees fit. It can be used as a framework
1345 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001347 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001348 *
1349 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001350 *
1351 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001352 */
Andrew Boie468190a2017-09-29 14:00:48 -07001353__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001354
1355/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001356 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001360 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001361 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001362 */
Andrew Boie468190a2017-09-29 14:00:48 -07001363__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364
1365/**
Anas Nashif57554052018-03-03 02:31:05 -06001366 * @brief Set current thread name
1367 *
1368 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1369 * tracing and debugging.
1370 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001371 * @param thread_id Thread to set name, or NULL to set the current thread
1372 * @param value Name string
1373 * @retval 0 on success
1374 * @retval -EFAULT Memory access error with supplied string
1375 * @retval -ENOSYS Thread name configuration option not enabled
1376 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001377 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001378__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001379
1380/**
1381 * @brief Get thread name
1382 *
1383 * Get the name of a thread
1384 *
1385 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001386 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001387 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001388const char *k_thread_name_get(k_tid_t thread_id);
1389
1390/**
1391 * @brief Copy the thread name into a supplied buffer
1392 *
1393 * @param thread_id Thread to obtain name information
1394 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001395 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001396 * @retval -ENOSPC Destination buffer too small
1397 * @retval -EFAULT Memory access error
1398 * @retval -ENOSYS Thread name feature not enabled
1399 * @retval 0 Success
1400 */
1401__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1402 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001403
1404/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001405 * @brief Get thread state string
1406 *
1407 * Get the human friendly thread state string
1408 *
1409 * @param thread_id Thread ID
1410 * @retval Thread state string, empty if no state flag is set
1411 */
1412const char *k_thread_state_str(k_tid_t thread_id);
1413
1414/**
Andy Rosscfe62032018-09-29 07:34:55 -07001415 * @}
1416 */
1417
1418/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001419 * @addtogroup clock_apis
1420 * @{
1421 */
1422
1423/**
1424 * @brief Generate null timeout delay.
1425 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001426 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001427 * not to wait if the requested operation cannot be performed immediately.
1428 *
1429 * @return Timeout delay value.
1430 */
1431#define K_NO_WAIT 0
1432
1433/**
1434 * @brief Generate timeout delay from milliseconds.
1435 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001436 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001437 * to wait up to @a ms milliseconds to perform the requested operation.
1438 *
1439 * @param ms Duration in milliseconds.
1440 *
1441 * @return Timeout delay value.
1442 */
Johan Hedberg14471692016-11-13 10:52:15 +02001443#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001444
1445/**
1446 * @brief Generate timeout delay from seconds.
1447 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001448 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001449 * to wait up to @a s seconds to perform the requested operation.
1450 *
1451 * @param s Duration in seconds.
1452 *
1453 * @return Timeout delay value.
1454 */
Johan Hedberg14471692016-11-13 10:52:15 +02001455#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001456
1457/**
1458 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001459
1460 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001461 * to wait up to @a m minutes to perform the requested operation.
1462 *
1463 * @param m Duration in minutes.
1464 *
1465 * @return Timeout delay value.
1466 */
Johan Hedberg14471692016-11-13 10:52:15 +02001467#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001468
1469/**
1470 * @brief Generate timeout delay from hours.
1471 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001472 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001473 * to wait up to @a h hours to perform the requested operation.
1474 *
1475 * @param h Duration in hours.
1476 *
1477 * @return Timeout delay value.
1478 */
Johan Hedberg14471692016-11-13 10:52:15 +02001479#define K_HOURS(h) K_MINUTES((h) * 60)
1480
Allan Stephensc98da842016-11-11 15:45:03 -05001481/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001482 * @brief Generate infinite timeout delay.
1483 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001484 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001485 * to wait as long as necessary to perform the requested operation.
1486 *
1487 * @return Timeout delay value.
1488 */
1489#define K_FOREVER (-1)
1490
1491/**
Anas Nashif166f5192018-02-25 08:02:36 -06001492 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001493 */
1494
1495/**
Allan Stephensc98da842016-11-11 15:45:03 -05001496 * @cond INTERNAL_HIDDEN
1497 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001498
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499struct k_timer {
1500 /*
1501 * _timeout structure must be first here if we want to use
1502 * dynamic timer allocation. timeout.node is used in the double-linked
1503 * list of free timers
1504 */
1505 struct _timeout timeout;
1506
Allan Stephens45bfa372016-10-12 12:39:42 -05001507 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001508 _wait_q_t wait_q;
1509
1510 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001511 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001512
1513 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001514 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001515
1516 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001517 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001518
Allan Stephens45bfa372016-10-12 12:39:42 -05001519 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001520 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001521
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001522 /* user-specific data, also used to support legacy features */
1523 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001524
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001525 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001526 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001527};
1528
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001529#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001530 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001531 .timeout = { \
1532 .node = {},\
1533 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001534 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001535 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001536 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001537 .expiry_fn = expiry, \
1538 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001539 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001540 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001541 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001542 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001543 }
1544
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001545#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001546
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001547/**
Allan Stephensc98da842016-11-11 15:45:03 -05001548 * INTERNAL_HIDDEN @endcond
1549 */
1550
1551/**
1552 * @defgroup timer_apis Timer APIs
1553 * @ingroup kernel_apis
1554 * @{
1555 */
1556
1557/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001558 * @typedef k_timer_expiry_t
1559 * @brief Timer expiry function type.
1560 *
1561 * A timer's expiry function is executed by the system clock interrupt handler
1562 * each time the timer expires. The expiry function is optional, and is only
1563 * invoked if the timer has been initialized with one.
1564 *
1565 * @param timer Address of timer.
1566 *
1567 * @return N/A
1568 */
1569typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1570
1571/**
1572 * @typedef k_timer_stop_t
1573 * @brief Timer stop function type.
1574 *
1575 * A timer's stop function is executed if the timer is stopped prematurely.
1576 * The function runs in the context of the thread that stops the timer.
1577 * The stop function is optional, and is only invoked if the timer has been
1578 * initialized with one.
1579 *
1580 * @param timer Address of timer.
1581 *
1582 * @return N/A
1583 */
1584typedef void (*k_timer_stop_t)(struct k_timer *timer);
1585
1586/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001587 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001590 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001591 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001592 *
1593 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * @param expiry_fn Function to invoke each time the timer expires.
1595 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001596 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001597#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001598 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001599 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001600
Allan Stephens45bfa372016-10-12 12:39:42 -05001601/**
1602 * @brief Initialize a timer.
1603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001604 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001605 *
1606 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * @param expiry_fn Function to invoke each time the timer expires.
1608 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001609 *
1610 * @return N/A
1611 */
1612extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001613 k_timer_expiry_t expiry_fn,
1614 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001615
Allan Stephens45bfa372016-10-12 12:39:42 -05001616/**
1617 * @brief Start a timer.
1618 *
1619 * This routine starts a timer, and resets its status to zero. The timer
1620 * begins counting down using the specified duration and period values.
1621 *
1622 * Attempting to start a timer that is already running is permitted.
1623 * The timer's status is reset to zero and the timer begins counting down
1624 * using the new duration and period values.
1625 *
1626 * @param timer Address of timer.
1627 * @param duration Initial timer duration (in milliseconds).
1628 * @param period Timer period (in milliseconds).
1629 *
1630 * @return N/A
1631 */
Andrew Boiea354d492017-09-29 16:22:28 -07001632__syscall void k_timer_start(struct k_timer *timer,
1633 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001634
1635/**
1636 * @brief Stop a timer.
1637 *
1638 * This routine stops a running timer prematurely. The timer's stop function,
1639 * if one exists, is invoked by the caller.
1640 *
1641 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001642 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001643 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001644 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1645 * if @a k_timer_stop is to be called from ISRs.
1646 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001647 * @param timer Address of timer.
1648 *
1649 * @return N/A
1650 */
Andrew Boiea354d492017-09-29 16:22:28 -07001651__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001652
1653/**
1654 * @brief Read timer status.
1655 *
1656 * This routine reads the timer's status, which indicates the number of times
1657 * it has expired since its status was last read.
1658 *
1659 * Calling this routine resets the timer's status to zero.
1660 *
1661 * @param timer Address of timer.
1662 *
1663 * @return Timer status.
1664 */
Andrew Boiea354d492017-09-29 16:22:28 -07001665__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001666
1667/**
1668 * @brief Synchronize thread to timer expiration.
1669 *
1670 * This routine blocks the calling thread until the timer's status is non-zero
1671 * (indicating that it has expired at least once since it was last examined)
1672 * or the timer is stopped. If the timer status is already non-zero,
1673 * or the timer is already stopped, the caller continues without waiting.
1674 *
1675 * Calling this routine resets the timer's status to zero.
1676 *
1677 * This routine must not be used by interrupt handlers, since they are not
1678 * allowed to block.
1679 *
1680 * @param timer Address of timer.
1681 *
1682 * @return Timer status.
1683 */
Andrew Boiea354d492017-09-29 16:22:28 -07001684__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001685
Andy Ross52e444b2018-09-28 09:06:37 -07001686extern s32_t z_timeout_remaining(struct _timeout *timeout);
1687
Allan Stephens45bfa372016-10-12 12:39:42 -05001688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001690 *
1691 * This routine computes the (approximate) time remaining before a running
1692 * timer next expires. If the timer is not running, it returns zero.
1693 *
1694 * @param timer Address of timer.
1695 *
1696 * @return Remaining time (in milliseconds).
1697 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001698__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001699
Patrik Flykt4344e272019-03-08 14:19:05 -07001700static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001701{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001702 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001703 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001704}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705
Allan Stephensc98da842016-11-11 15:45:03 -05001706/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001707 * @brief Associate user-specific data with a timer.
1708 *
1709 * This routine records the @a user_data with the @a timer, to be retrieved
1710 * later.
1711 *
1712 * It can be used e.g. in a timer handler shared across multiple subsystems to
1713 * retrieve data specific to the subsystem this timer is associated with.
1714 *
1715 * @param timer Address of timer.
1716 * @param user_data User data to associate with the timer.
1717 *
1718 * @return N/A
1719 */
Andrew Boiea354d492017-09-29 16:22:28 -07001720__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1721
Anas Nashif954d5502018-02-25 08:37:28 -06001722/**
1723 * @internal
1724 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001725static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001726 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001727{
1728 timer->user_data = user_data;
1729}
1730
1731/**
1732 * @brief Retrieve the user-specific data from a timer.
1733 *
1734 * @param timer Address of timer.
1735 *
1736 * @return The user data.
1737 */
Andrew Boiea354d492017-09-29 16:22:28 -07001738__syscall void *k_timer_user_data_get(struct k_timer *timer);
1739
Patrik Flykt4344e272019-03-08 14:19:05 -07001740static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001741{
1742 return timer->user_data;
1743}
1744
Anas Nashif166f5192018-02-25 08:02:36 -06001745/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001746
Allan Stephensc98da842016-11-11 15:45:03 -05001747/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001748 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001749 * @{
1750 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001751
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001753 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * This routine returns the elapsed time since the system booted,
1756 * in milliseconds.
1757 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001758 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001759 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001760 * While this function returns time in milliseconds, it does
1761 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001762 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001763 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001764 *
1765 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001766 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001767__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001768
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001769/**
1770 * @brief Enable clock always on in tickless kernel
1771 *
Andy Ross1db9f182019-06-25 10:09:45 -07001772 * Deprecated. This does nothing (it was always just a hint). This
1773 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1774 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001775 *
1776 * @retval prev_status Previous status of always on flag
1777 */
Andy Ross1db9f182019-06-25 10:09:45 -07001778/* LCOV_EXCL_START */
1779__deprecated static inline int k_enable_sys_clock_always_on(void)
1780{
1781 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1782 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1783
1784 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1785}
1786/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001787
1788/**
1789 * @brief Disable clock always on in tickless kernel
1790 *
Andy Ross1db9f182019-06-25 10:09:45 -07001791 * Deprecated. This does nothing (it was always just a hint). This
1792 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1793 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001794 */
Andy Ross1db9f182019-06-25 10:09:45 -07001795/* LCOV_EXCL_START */
1796__deprecated static inline void k_disable_sys_clock_always_on(void)
1797{
1798 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1799 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1800}
1801/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001802
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001803/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001805 *
Peter Bigota6067a32019-08-28 08:19:26 -05001806 * This routine returns the lower 32 bits of the system uptime in
1807 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001808 *
Peter Bigota6067a32019-08-28 08:19:26 -05001809 * Because correct conversion requires full precision of the system
1810 * clock there is no benefit to using this over k_uptime_get() unless
1811 * you know the application will never run long enough for the system
1812 * clock to approach 2^32 ticks. Calls to this function may involve
1813 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001814 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001815 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001816 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001817 * While this function returns time in milliseconds, it does
1818 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001819 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001820 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001821 *
Peter Bigota6067a32019-08-28 08:19:26 -05001822 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001823 */
Peter Bigota6067a32019-08-28 08:19:26 -05001824static inline u32_t k_uptime_get_32(void)
1825{
1826 return (u32_t)k_uptime_get();
1827}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001828
1829/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001830 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001832 * This routine computes the elapsed time between the current system uptime
1833 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @param reftime Pointer to a reference time, which is updated to the current
1836 * uptime upon return.
1837 *
1838 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001839 */
Andy Ross987c0e52018-09-27 16:50:00 -07001840static inline s64_t k_uptime_delta(s64_t *reftime)
1841{
1842 s64_t uptime, delta;
1843
1844 uptime = k_uptime_get();
1845 delta = uptime - *reftime;
1846 *reftime = uptime;
1847
1848 return delta;
1849}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001850
1851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * This routine computes the elapsed time between the current system uptime
1855 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001857 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1858 * need for interrupt locking and 64-bit math. However, the 32-bit result
1859 * cannot hold an elapsed time larger than approximately 50 days, so the
1860 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001862 * @param reftime Pointer to a reference time, which is updated to the current
1863 * uptime upon return.
1864 *
1865 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001866 */
Andy Ross987c0e52018-09-27 16:50:00 -07001867static inline u32_t k_uptime_delta_32(s64_t *reftime)
1868{
1869 return (u32_t)k_uptime_delta(reftime);
1870}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001871
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001872/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001873 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001875 * This routine returns the current time, as measured by the system's hardware
1876 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001878 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001879 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001880static inline u32_t k_cycle_get_32(void)
1881{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001882 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001883}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001884
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001885/**
Anas Nashif166f5192018-02-25 08:02:36 -06001886 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001887 */
1888
Allan Stephensc98da842016-11-11 15:45:03 -05001889/**
1890 * @cond INTERNAL_HIDDEN
1891 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001892
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001893struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001894 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001895 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001896 union {
1897 _wait_q_t wait_q;
1898
1899 _POLL_EVENT;
1900 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001901
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001902 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001903 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001904};
1905
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001906#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001907 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001908 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001909 .lock = { }, \
1910 { \
1911 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1912 _POLL_EVENT_OBJ_INIT(obj) \
1913 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001914 _OBJECT_TRACING_INIT \
1915 }
1916
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001917#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001918
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001919extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1920
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001921/**
1922 * INTERNAL_HIDDEN @endcond
1923 */
1924
1925/**
1926 * @defgroup queue_apis Queue APIs
1927 * @ingroup kernel_apis
1928 * @{
1929 */
1930
1931/**
1932 * @brief Initialize a queue.
1933 *
1934 * This routine initializes a queue object, prior to its first use.
1935 *
1936 * @param queue Address of the queue.
1937 *
1938 * @return N/A
1939 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001940__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001941
1942/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001943 * @brief Cancel waiting on a queue.
1944 *
1945 * This routine causes first thread pending on @a queue, if any, to
1946 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001947 * If the queue is being waited on by k_poll(), it will return with
1948 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1949 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001950 *
1951 * @note Can be called by ISRs.
1952 *
1953 * @param queue Address of the queue.
1954 *
1955 * @return N/A
1956 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001957__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001958
1959/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001960 * @brief Append an element to the end of a queue.
1961 *
1962 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001963 * aligned on a word boundary, and the first word of the item is reserved
1964 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001965 *
1966 * @note Can be called by ISRs.
1967 *
1968 * @param queue Address of the queue.
1969 * @param data Address of the data item.
1970 *
1971 * @return N/A
1972 */
1973extern void k_queue_append(struct k_queue *queue, void *data);
1974
1975/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001976 * @brief Append an element to a queue.
1977 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001978 * This routine appends a data item to @a queue. There is an implicit memory
1979 * allocation to create an additional temporary bookkeeping data structure from
1980 * the calling thread's resource pool, which is automatically freed when the
1981 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001982 *
1983 * @note Can be called by ISRs.
1984 *
1985 * @param queue Address of the queue.
1986 * @param data Address of the data item.
1987 *
1988 * @retval 0 on success
1989 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1990 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301991__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001992
1993/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001994 * @brief Prepend an element to a queue.
1995 *
1996 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001997 * aligned on a word boundary, and the first word of the item is reserved
1998 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001999 *
2000 * @note Can be called by ISRs.
2001 *
2002 * @param queue Address of the queue.
2003 * @param data Address of the data item.
2004 *
2005 * @return N/A
2006 */
2007extern void k_queue_prepend(struct k_queue *queue, void *data);
2008
2009/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002010 * @brief Prepend an element to a queue.
2011 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002012 * This routine prepends a data item to @a queue. There is an implicit memory
2013 * allocation to create an additional temporary bookkeeping data structure from
2014 * the calling thread's resource pool, which is automatically freed when the
2015 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002016 *
2017 * @note Can be called by ISRs.
2018 *
2019 * @param queue Address of the queue.
2020 * @param data Address of the data item.
2021 *
2022 * @retval 0 on success
2023 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2024 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05302025__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002026
2027/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002028 * @brief Inserts an element to a queue.
2029 *
2030 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002031 * data item must be aligned on a word boundary, and the first word of
2032 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002033 *
2034 * @note Can be called by ISRs.
2035 *
2036 * @param queue Address of the queue.
2037 * @param prev Address of the previous data item.
2038 * @param data Address of the data item.
2039 *
2040 * @return N/A
2041 */
2042extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2043
2044/**
2045 * @brief Atomically append a list of elements to a queue.
2046 *
2047 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002048 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002049 * in each data item pointing to the next data item; the list must be
2050 * NULL-terminated.
2051 *
2052 * @note Can be called by ISRs.
2053 *
2054 * @param queue Address of the queue.
2055 * @param head Pointer to first node in singly-linked list.
2056 * @param tail Pointer to last node in singly-linked list.
2057 *
2058 * @return N/A
2059 */
2060extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
2061
2062/**
2063 * @brief Atomically add a list of elements to a queue.
2064 *
2065 * This routine adds a list of data items to @a queue in one operation.
2066 * The data items must be in a singly-linked list implemented using a
2067 * sys_slist_t object. Upon completion, the original list is empty.
2068 *
2069 * @note Can be called by ISRs.
2070 *
2071 * @param queue Address of the queue.
2072 * @param list Pointer to sys_slist_t object.
2073 *
2074 * @return N/A
2075 */
2076extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2077
2078/**
2079 * @brief Get an element from a queue.
2080 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002081 * This routine removes first data item from @a queue. The first word of the
2082 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002083 *
2084 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2085 *
2086 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002087 * @param timeout Non-negative waiting period to obtain a data item (in
2088 * milliseconds), or one of the special values K_NO_WAIT and
2089 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002090 *
2091 * @return Address of the data item if successful; NULL if returned
2092 * without waiting, or waiting period timed out.
2093 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002094__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002095
2096/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002097 * @brief Remove an element from a queue.
2098 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002099 * This routine removes data item from @a queue. The first word of the
2100 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002101 * rely on sys_slist_find_and_remove which is not a constant time operation.
2102 *
2103 * @note Can be called by ISRs
2104 *
2105 * @param queue Address of the queue.
2106 * @param data Address of the data item.
2107 *
2108 * @return true if data item was removed
2109 */
2110static inline bool k_queue_remove(struct k_queue *queue, void *data)
2111{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002112 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002113}
2114
2115/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002116 * @brief Append an element to a queue only if it's not present already.
2117 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002118 * This routine appends data item to @a queue. The first word of the data
2119 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002120 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2121 *
2122 * @note Can be called by ISRs
2123 *
2124 * @param queue Address of the queue.
2125 * @param data Address of the data item.
2126 *
2127 * @return true if data item was added, false if not
2128 */
2129static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2130{
2131 sys_sfnode_t *test;
2132
2133 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2134 if (test == (sys_sfnode_t *) data) {
2135 return false;
2136 }
2137 }
2138
2139 k_queue_append(queue, data);
2140 return true;
2141}
2142
2143/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002144 * @brief Query a queue to see if it has data available.
2145 *
2146 * Note that the data might be already gone by the time this function returns
2147 * if other threads are also trying to read from the queue.
2148 *
2149 * @note Can be called by ISRs.
2150 *
2151 * @param queue Address of the queue.
2152 *
2153 * @return Non-zero if the queue is empty.
2154 * @return 0 if data is available.
2155 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002156__syscall int k_queue_is_empty(struct k_queue *queue);
2157
Patrik Flykt4344e272019-03-08 14:19:05 -07002158static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002159{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002160 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002161}
2162
2163/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002164 * @brief Peek element at the head of queue.
2165 *
2166 * Return element from the head of queue without removing it.
2167 *
2168 * @param queue Address of the queue.
2169 *
2170 * @return Head element, or NULL if queue is empty.
2171 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002172__syscall void *k_queue_peek_head(struct k_queue *queue);
2173
Patrik Flykt4344e272019-03-08 14:19:05 -07002174static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002175{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002176 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002177}
2178
2179/**
2180 * @brief Peek element at the tail of queue.
2181 *
2182 * Return element from the tail of queue without removing it.
2183 *
2184 * @param queue Address of the queue.
2185 *
2186 * @return Tail element, or NULL if queue is empty.
2187 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002188__syscall void *k_queue_peek_tail(struct k_queue *queue);
2189
Patrik Flykt4344e272019-03-08 14:19:05 -07002190static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002191{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002192 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002193}
2194
2195/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002196 * @brief Statically define and initialize a queue.
2197 *
2198 * The queue can be accessed outside the module where it is defined using:
2199 *
2200 * @code extern struct k_queue <name>; @endcode
2201 *
2202 * @param name Name of the queue.
2203 */
2204#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002205 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002206 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002207
Anas Nashif166f5192018-02-25 08:02:36 -06002208/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002209
Wentong Wu5611e922019-06-20 23:51:27 +08002210#ifdef CONFIG_USERSPACE
2211/**
2212 * @brief futex structure
2213 *
2214 * A k_futex is a lightweight mutual exclusion primitive designed
2215 * to minimize kernel involvement. Uncontended operation relies
2216 * only on atomic access to shared memory. k_futex are tracked as
2217 * kernel objects and can live in user memory so any access bypass
2218 * the kernel object permission management mechanism.
2219 */
2220struct k_futex {
2221 atomic_t val;
2222};
2223
2224/**
2225 * @brief futex kernel data structure
2226 *
2227 * z_futex_data are the helper data structure for k_futex to complete
2228 * futex contended operation on kernel side, structure z_futex_data
2229 * of every futex object is invisible in user mode.
2230 */
2231struct z_futex_data {
2232 _wait_q_t wait_q;
2233 struct k_spinlock lock;
2234};
2235
2236#define Z_FUTEX_DATA_INITIALIZER(obj) \
2237 { \
2238 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2239 }
2240
2241/**
2242 * @defgroup futex_apis FUTEX APIs
2243 * @ingroup kernel_apis
2244 * @{
2245 */
2246
2247/**
Wentong Wu5611e922019-06-20 23:51:27 +08002248 * @brief Pend the current thread on a futex
2249 *
2250 * Tests that the supplied futex contains the expected value, and if so,
2251 * goes to sleep until some other thread calls k_futex_wake() on it.
2252 *
2253 * @param futex Address of the futex.
2254 * @param expected Expected value of the futex, if it is different the caller
2255 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002256 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2257 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002258 * @retval -EACCES Caller does not have read access to futex address.
2259 * @retval -EAGAIN If the futex value did not match the expected parameter.
2260 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2261 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2262 * @retval 0 if the caller went to sleep and was woken up. The caller
2263 * should check the futex's value on wakeup to determine if it needs
2264 * to block again.
2265 */
2266__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2267
2268/**
2269 * @brief Wake one/all threads pending on a futex
2270 *
2271 * Wake up the highest priority thread pending on the supplied futex, or
2272 * wakeup all the threads pending on the supplied futex, and the behavior
2273 * depends on wake_all.
2274 *
2275 * @param futex Futex to wake up pending threads.
2276 * @param wake_all If true, wake up all pending threads; If false,
2277 * wakeup the highest priority thread.
2278 * @retval -EACCES Caller does not have access to the futex address.
2279 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2280 * @retval Number of threads that were woken up.
2281 */
2282__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2283
2284/** @} */
2285#endif
2286
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002287struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002288 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289};
2290
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002291/**
2292 * @cond INTERNAL_HIDDEN
2293 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002294#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002295 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002296 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002297 }
2298
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002299#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002300
Allan Stephensc98da842016-11-11 15:45:03 -05002301/**
2302 * INTERNAL_HIDDEN @endcond
2303 */
2304
2305/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002306 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002307 * @ingroup kernel_apis
2308 * @{
2309 */
2310
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002312 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002314 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002316 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
2318 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002319 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002321#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002322 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323
2324/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002325 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002326 *
2327 * This routine causes first thread pending on @a fifo, if any, to
2328 * return from k_fifo_get() call with NULL value (as if timeout
2329 * expired).
2330 *
2331 * @note Can be called by ISRs.
2332 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002333 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002334 *
2335 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002336 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002337 */
2338#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002339 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002340
2341/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002342 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002344 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002345 * aligned on a word boundary, and the first word of the item is reserved
2346 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * @note Can be called by ISRs.
2349 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002350 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002351 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
2353 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002354 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002356#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002357 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002358
2359/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002360 * @brief Add an element to a FIFO queue.
2361 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002362 * This routine adds a data item to @a fifo. There is an implicit memory
2363 * allocation to create an additional temporary bookkeeping data structure from
2364 * the calling thread's resource pool, which is automatically freed when the
2365 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002366 *
2367 * @note Can be called by ISRs.
2368 *
2369 * @param fifo Address of the FIFO.
2370 * @param data Address of the data item.
2371 *
2372 * @retval 0 on success
2373 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002374 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002375 */
2376#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002377 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002378
2379/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002380 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002383 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 * each data item pointing to the next data item; the list must be
2385 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002387 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002388 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002389 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002390 * @param head Pointer to first node in singly-linked list.
2391 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002392 *
2393 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002394 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002395 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002396#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002397 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398
2399/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002400 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 * This routine adds a list of data items to @a fifo in one operation.
2403 * The data items must be in a singly-linked list implemented using a
2404 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002405 * and must be re-initialized via sys_slist_init().
2406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002407 * @note Can be called by ISRs.
2408 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002409 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 *
2412 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002413 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002414 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002415#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002416 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417
2418/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002419 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002422 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002424 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2425 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002426 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002428 * or one of the special values K_NO_WAIT and K_FOREVER.
2429 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002430 * @return Address of the data item if successful; NULL if returned
2431 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002432 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002433 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002434#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002435 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002438 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002439 *
2440 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002441 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002442 *
2443 * @note Can be called by ISRs.
2444 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002445 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002446 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002447 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002448 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002449 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002450 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002451#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002452 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002453
2454/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002455 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002456 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002457 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302458 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002459 * on each iteration of processing, a head container will be peeked,
2460 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002461 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002462 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002463 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002464 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002465 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002466 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002467 */
2468#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002469 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002470
2471/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002472 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002473 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002474 * Return element from the tail of FIFO queue (without removing it). A usecase
2475 * for this is if elements of the FIFO queue are themselves containers. Then
2476 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002477 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002478 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002479 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002480 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002481 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002482 */
2483#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002484 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002485
2486/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002487 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002489 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002491 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002493 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002494 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002495 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002497 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002498 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499
Anas Nashif166f5192018-02-25 08:02:36 -06002500/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002501
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002503 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504};
2505
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002506/**
2507 * @cond INTERNAL_HIDDEN
2508 */
2509
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002510#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002511 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002512 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002513 }
2514
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002515#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002516
Allan Stephensc98da842016-11-11 15:45:03 -05002517/**
2518 * INTERNAL_HIDDEN @endcond
2519 */
2520
2521/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002522 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002523 * @ingroup kernel_apis
2524 * @{
2525 */
2526
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002528 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002530 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002532 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
2534 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002535 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002537#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002538 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539
2540/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002541 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002543 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002544 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002545 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002547 * @note Can be called by ISRs.
2548 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002549 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002550 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002551 *
2552 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002553 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002554 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002555#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002556 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002557
2558/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002559 * @brief Add an element to a LIFO queue.
2560 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002561 * This routine adds a data item to @a lifo. There is an implicit memory
2562 * allocation to create an additional temporary bookkeeping data structure from
2563 * the calling thread's resource pool, which is automatically freed when the
2564 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002565 *
2566 * @note Can be called by ISRs.
2567 *
2568 * @param lifo Address of the LIFO.
2569 * @param data Address of the data item.
2570 *
2571 * @retval 0 on success
2572 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002573 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002574 */
2575#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002576 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002577
2578/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002579 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002580 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002581 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002582 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2585 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002586 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002588 * or one of the special values K_NO_WAIT and K_FOREVER.
2589 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002590 * @return Address of the data item if successful; NULL if returned
2591 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002592 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002593 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002594#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002595 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002597/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002598 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002599 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002600 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002602 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002604 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002605 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002606 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002608 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002609 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610
Anas Nashif166f5192018-02-25 08:02:36 -06002611/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002612
2613/**
2614 * @cond INTERNAL_HIDDEN
2615 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302616#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002618typedef uintptr_t stack_data_t;
2619
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620struct k_stack {
2621 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002622 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002623 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002625 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002626 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002627 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628};
2629
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002630#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002631 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002632 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002633 .base = stack_buffer, \
2634 .next = stack_buffer, \
2635 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002636 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002637 }
2638
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002639#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002640
Allan Stephensc98da842016-11-11 15:45:03 -05002641/**
2642 * INTERNAL_HIDDEN @endcond
2643 */
2644
2645/**
2646 * @defgroup stack_apis Stack APIs
2647 * @ingroup kernel_apis
2648 * @{
2649 */
2650
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651/**
2652 * @brief Initialize a stack.
2653 *
2654 * This routine initializes a stack object, prior to its first use.
2655 *
2656 * @param stack Address of the stack.
2657 * @param buffer Address of array used to hold stacked values.
2658 * @param num_entries Maximum number of values that can be stacked.
2659 *
2660 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002661 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 */
Andrew Boief3bee952018-05-02 17:44:39 -07002663void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002664 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002665
2666
2667/**
2668 * @brief Initialize a stack.
2669 *
2670 * This routine initializes a stack object, prior to its first use. Internal
2671 * buffers will be allocated from the calling thread's resource pool.
2672 * This memory will be released if k_stack_cleanup() is called, or
2673 * userspace is enabled and the stack object loses all references to it.
2674 *
2675 * @param stack Address of the stack.
2676 * @param num_entries Maximum number of values that can be stacked.
2677 *
2678 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002679 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002680 */
2681
Adithya Baglody28080d32018-10-15 11:48:51 +05302682__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2683 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002684
2685/**
2686 * @brief Release a stack's allocated buffer
2687 *
2688 * If a stack object was given a dynamically allocated buffer via
2689 * k_stack_alloc_init(), this will free it. This function does nothing
2690 * if the buffer wasn't dynamically allocated.
2691 *
2692 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002693 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002694 */
2695void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002696
2697/**
2698 * @brief Push an element onto a stack.
2699 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002700 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002701 *
2702 * @note Can be called by ISRs.
2703 *
2704 * @param stack Address of the stack.
2705 * @param data Value to push onto the stack.
2706 *
2707 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002708 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002709 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002710__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711
2712/**
2713 * @brief Pop an element from a stack.
2714 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002715 * This routine removes a stack_data_t value from @a stack in a "last in,
2716 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002717 *
2718 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2719 *
2720 * @param stack Address of the stack.
2721 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002722 * @param timeout Non-negative waiting period to obtain a value (in
2723 * milliseconds), or one of the special values K_NO_WAIT and
2724 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002725 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002726 * @retval 0 Element popped from stack.
2727 * @retval -EBUSY Returned without waiting.
2728 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002729 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002730 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002731__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2732 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002738 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002739 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002741 * @param name Name of the stack.
2742 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002743 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002745#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002746 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002747 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002748 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002749 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002750 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751
Anas Nashif166f5192018-02-25 08:02:36 -06002752/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002753
Allan Stephens6bba9b02016-11-16 14:56:54 -05002754struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002755struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002756
Piotr Zięcik19d83492019-09-27 09:16:25 +02002757/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002758typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2759struct _poller {
2760 volatile bool is_polling;
2761 struct k_thread *thread;
2762 _poller_cb_t cb;
2763};
2764
Allan Stephensc98da842016-11-11 15:45:03 -05002765/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002766 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002767 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768 */
2769
Allan Stephens6bba9b02016-11-16 14:56:54 -05002770/**
2771 * @typedef k_work_handler_t
2772 * @brief Work item handler function type.
2773 *
2774 * A work item's handler function is executed by a workqueue's thread
2775 * when the work item is processed by the workqueue.
2776 *
2777 * @param work Address of the work item.
2778 *
2779 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002780 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002781 */
2782typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783
2784/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002785 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002787
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002789 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002790 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791};
2792
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002794 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795};
2796
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002798 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799 k_work_handler_t handler;
2800 atomic_t flags[1];
2801};
2802
Allan Stephens6bba9b02016-11-16 14:56:54 -05002803struct k_delayed_work {
2804 struct k_work work;
2805 struct _timeout timeout;
2806 struct k_work_q *work_q;
2807};
2808
Piotr Zięcik19d83492019-09-27 09:16:25 +02002809struct k_work_poll {
2810 struct k_work work;
2811 struct _poller poller;
2812 struct k_poll_event *events;
2813 int num_events;
2814 k_work_handler_t real_handler;
2815 struct _timeout timeout;
2816 int poll_result;
2817};
2818
Allan Stephens6bba9b02016-11-16 14:56:54 -05002819extern struct k_work_q k_sys_work_q;
2820
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002822 * INTERNAL_HIDDEN @endcond
2823 */
2824
Patrik Flykt4344e272019-03-08 14:19:05 -07002825#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002826 { \
2827 ._reserved = NULL, \
2828 .handler = work_handler, \
2829 .flags = { 0 } \
2830 }
2831
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002832#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002833
Allan Stephens6bba9b02016-11-16 14:56:54 -05002834/**
2835 * @brief Initialize a statically-defined work item.
2836 *
2837 * This macro can be used to initialize a statically-defined workqueue work
2838 * item, prior to its first use. For example,
2839 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002840 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002841 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002842 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002843 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002844 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002846#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002847 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848
2849/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002850 * @brief Initialize a work item.
2851 *
2852 * This routine initializes a workqueue work item, prior to its first use.
2853 *
2854 * @param work Address of work item.
2855 * @param handler Function to invoke each time work item is processed.
2856 *
2857 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002858 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859 */
2860static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2861{
Patrik Flykt4344e272019-03-08 14:19:05 -07002862 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863}
2864
2865/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002866 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002867 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002868 * This routine submits work item @a work to be processed by workqueue
2869 * @a work_q. If the work item is already pending in the workqueue's queue
2870 * as a result of an earlier submission, this routine has no effect on the
2871 * work item. If the work item has already been processed, or is currently
2872 * being processed, its work is considered complete and the work item can be
2873 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002874 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002875 * @warning
2876 * A submitted work item must not be modified until it has been processed
2877 * by the workqueue.
2878 *
2879 * @note Can be called by ISRs.
2880 *
2881 * @param work_q Address of workqueue.
2882 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002883 *
2884 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002885 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002886 */
2887static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2888 struct k_work *work)
2889{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002890 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002891 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002892 }
2893}
2894
2895/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002896 * @brief Submit a work item to a user mode workqueue
2897 *
David B. Kinder06d78352018-12-17 14:32:40 -08002898 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002899 * memory allocation is made from the caller's resource pool which is freed
2900 * once the worker thread consumes the k_work item. The workqueue
2901 * thread must have memory access to the k_work item being submitted. The caller
2902 * must have permission granted on the work_q parameter's queue object.
2903 *
2904 * Otherwise this works the same as k_work_submit_to_queue().
2905 *
2906 * @note Can be called by ISRs.
2907 *
2908 * @param work_q Address of workqueue.
2909 * @param work Address of work item.
2910 *
2911 * @retval -EBUSY if the work item was already in some workqueue
2912 * @retval -ENOMEM if no memory for thread resource pool allocation
2913 * @retval 0 Success
2914 * @req K-WORK-001
2915 */
2916static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2917 struct k_work *work)
2918{
2919 int ret = -EBUSY;
2920
2921 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2922 ret = k_queue_alloc_append(&work_q->queue, work);
2923
2924 /* Couldn't insert into the queue. Clear the pending bit
2925 * so the work item can be submitted again
2926 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002927 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002928 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2929 }
2930 }
2931
2932 return ret;
2933}
2934
2935/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002936 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002937 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002938 * This routine indicates if work item @a work is pending in a workqueue's
2939 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002940 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002941 * @note Can be called by ISRs.
2942 *
2943 * @param work Address of work item.
2944 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002945 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002946 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002947 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002948static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002949{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002950 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002951}
2952
2953/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002954 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002955 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002956 * This routine starts workqueue @a work_q. The workqueue spawns its work
2957 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002958 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002959 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002960 * @param stack Pointer to work queue thread's stack space, as defined by
2961 * K_THREAD_STACK_DEFINE()
2962 * @param stack_size Size of the work queue thread's stack (in bytes), which
2963 * should either be the same constant passed to
2964 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002965 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002966 *
2967 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002968 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969 */
Andrew Boie507852a2017-07-25 18:47:07 -07002970extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002971 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002972 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002973
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002974/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002975 * @brief Start a workqueue in user mode
2976 *
2977 * This works identically to k_work_q_start() except it is callable from user
2978 * mode, and the worker thread created will run in user mode.
2979 * The caller must have permissions granted on both the work_q parameter's
2980 * thread and queue objects, and the same restrictions on priority apply as
2981 * k_thread_create().
2982 *
2983 * @param work_q Address of workqueue.
2984 * @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().
2989 * @param prio Priority of the work queue's thread.
2990 *
2991 * @return N/A
2992 * @req K-WORK-001
2993 */
2994extern void k_work_q_user_start(struct k_work_q *work_q,
2995 k_thread_stack_t *stack,
2996 size_t stack_size, int prio);
2997
2998/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002999 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003000 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003001 * This routine initializes a workqueue delayed work item, prior to
3002 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003003 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003004 * @param work Address of delayed work item.
3005 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003006 *
3007 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003008 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003010extern void k_delayed_work_init(struct k_delayed_work *work,
3011 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012
3013/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003014 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003016 * This routine schedules work item @a work to be processed by workqueue
3017 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003018 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003019 * Only when the countdown completes is the work item actually submitted to
3020 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003022 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08003023 * counting down cancels the existing submission and restarts the
3024 * countdown using the new delay. Note that this behavior is
3025 * inherently subject to race conditions with the pre-existing
3026 * timeouts and work queue, so care must be taken to synchronize such
3027 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003029 * @warning
3030 * A delayed work item must not be modified until it has been processed
3031 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003032 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003033 * @note Can be called by ISRs.
3034 *
3035 * @param work_q Address of workqueue.
3036 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003037 * @param delay Non-negative delay before submitting the work item (in
3038 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003039 *
3040 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003041 * @retval -EINVAL Work item is being processed or has completed its work.
3042 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003043 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003045extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3046 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003047 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048
3049/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003050 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003051 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003052 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003053 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003054 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003055 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003056 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003057 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003058 * @note The result of calling this on a k_delayed_work item that has
3059 * not been submitted (i.e. before the return of the
3060 * k_delayed_work_submit_to_queue() call) is undefined.
3061 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003062 * @param work Address of delayed work item.
3063 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003064 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003065 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003066 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003068extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003070/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071 * @brief Submit a work item to the system workqueue.
3072 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003073 * This routine submits work item @a work to be processed by the system
3074 * workqueue. If the work item is already pending in the workqueue's queue
3075 * as a result of an earlier submission, this routine has no effect on the
3076 * work item. If the work item has already been processed, or is currently
3077 * being processed, its work is considered complete and the work item can be
3078 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003080 * @warning
3081 * Work items submitted to the system workqueue should avoid using handlers
3082 * that block or yield since this may prevent the system workqueue from
3083 * processing other work items in a timely manner.
3084 *
3085 * @note Can be called by ISRs.
3086 *
3087 * @param work Address of work item.
3088 *
3089 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003090 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091 */
3092static inline void k_work_submit(struct k_work *work)
3093{
3094 k_work_submit_to_queue(&k_sys_work_q, work);
3095}
3096
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003097/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098 * @brief Submit a delayed work item to the system workqueue.
3099 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003100 * This routine schedules work item @a work to be processed by the system
3101 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003102 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003103 * Only when the countdown completes is the work item actually submitted to
3104 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003106 * Submitting a previously submitted delayed work item that is still
3107 * counting down cancels the existing submission and restarts the countdown
3108 * using the new delay. If the work item is currently pending on the
3109 * workqueue's queue because the countdown has completed it is too late to
3110 * resubmit the item, and resubmission fails without impacting the work item.
3111 * If the work item has already been processed, or is currently being processed,
3112 * its work is considered complete and the work item can be resubmitted.
3113 *
3114 * @warning
3115 * Work items submitted to the system workqueue should avoid using handlers
3116 * that block or yield since this may prevent the system workqueue from
3117 * processing other work items in a timely manner.
3118 *
3119 * @note Can be called by ISRs.
3120 *
3121 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003122 * @param delay Non-negative delay before submitting the work item (in
3123 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003124 *
3125 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003126 * @retval -EINVAL Work item is being processed or has completed its work.
3127 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003128 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129 */
3130static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003131 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003133 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003134}
3135
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003137 * @brief Get time remaining before a delayed work gets scheduled.
3138 *
3139 * This routine computes the (approximate) time remaining before a
3140 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003141 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003142 *
3143 * @param work Delayed work item.
3144 *
3145 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003146 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003147 */
Kumar Galacc334c72017-04-21 10:55:34 -05003148static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003149{
Andy Ross88924062019-10-03 11:43:10 -07003150 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003151}
3152
Piotr Zięcik19d83492019-09-27 09:16:25 +02003153/**
3154 * @brief Initialize a triggered work item.
3155 *
3156 * This routine initializes a workqueue triggered work item, prior to
3157 * its first use.
3158 *
3159 * @param work Address of triggered work item.
3160 * @param handler Function to invoke each time work item is processed.
3161 *
3162 * @return N/A
3163 */
3164extern void k_work_poll_init(struct k_work_poll *work,
3165 k_work_handler_t handler);
3166
3167/**
3168 * @brief Submit a triggered work item.
3169 *
3170 * This routine schedules work item @a work to be processed by workqueue
3171 * @a work_q when one of the given @a events is signaled. The routine
3172 * initiates internal poller for the work item and then returns to the caller.
3173 * Only when one of the watched events happen the work item is actually
3174 * submitted to the workqueue and becomes pending.
3175 *
3176 * Submitting a previously submitted triggered work item that is still
3177 * waiting for the event cancels the existing submission and reschedules it
3178 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003179 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003180 * so care must be taken to synchronize such resubmissions externally.
3181 *
3182 * @note Can be called by ISRs.
3183 *
3184 * @warning
3185 * Provided array of events as well as a triggered work item must be placed
3186 * in persistent memory (valid until work handler execution or work
3187 * cancellation) and cannot be modified after submission.
3188 *
3189 * @param work_q Address of workqueue.
3190 * @param work Address of delayed work item.
3191 * @param events An array of pointers to events which trigger the work.
3192 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003193 * @param timeout Non-negative timeout after which the work will be scheduled
3194 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003195 *
3196 *
3197 * @retval 0 Work item started watching for events.
3198 * @retval -EINVAL Work item is being processed or has completed its work.
3199 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3200 */
3201extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3202 struct k_work_poll *work,
3203 struct k_poll_event *events,
3204 int num_events,
3205 s32_t timeout);
3206
3207/**
3208 * @brief Submit a triggered work item to the system workqueue.
3209 *
3210 * This routine schedules work item @a work to be processed by system
3211 * workqueue when one of the given @a events is signaled. The routine
3212 * initiates internal poller for the work item and then returns to the caller.
3213 * Only when one of the watched events happen the work item is actually
3214 * submitted to the workqueue and becomes pending.
3215 *
3216 * Submitting a previously submitted triggered work item that is still
3217 * waiting for the event cancels the existing submission and reschedules it
3218 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003219 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003220 * so care must be taken to synchronize such resubmissions externally.
3221 *
3222 * @note Can be called by ISRs.
3223 *
3224 * @warning
3225 * Provided array of events as well as a triggered work item must not be
3226 * modified until the item has been processed by the workqueue.
3227 *
3228 * @param work Address of delayed work item.
3229 * @param events An array of pointers to events which trigger the work.
3230 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003231 * @param timeout Non-negative timeout after which the work will be scheduled
3232 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003233 *
3234 * @retval 0 Work item started watching for events.
3235 * @retval -EINVAL Work item is being processed or has completed its work.
3236 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3237 */
3238static inline int k_work_poll_submit(struct k_work_poll *work,
3239 struct k_poll_event *events,
3240 int num_events,
3241 s32_t timeout)
3242{
3243 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3244 events, num_events, timeout);
3245}
3246
3247/**
3248 * @brief Cancel a triggered work item.
3249 *
3250 * This routine cancels the submission of triggered work item @a work.
3251 * A triggered work item can only be canceled if no event triggered work
3252 * submission.
3253 *
3254 * @note Can be called by ISRs.
3255 *
3256 * @param work Address of delayed work item.
3257 *
David B. Kinder73896c02019-10-28 16:27:57 -07003258 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003259 * @retval -EINVAL Work item is being processed or has completed its work.
3260 */
3261extern int k_work_poll_cancel(struct k_work_poll *work);
3262
Anas Nashif166f5192018-02-25 08:02:36 -06003263/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003264/**
Anas Nashifce78d162018-05-24 12:43:11 -05003265 * @defgroup mutex_apis Mutex APIs
3266 * @ingroup kernel_apis
3267 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003268 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269
Anas Nashifce78d162018-05-24 12:43:11 -05003270/**
3271 * Mutex Structure
3272 * @ingroup mutex_apis
3273 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05003275 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003277 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003278 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05003279
3280 /** Current lock count */
Kumar Galacc334c72017-04-21 10:55:34 -05003281 u32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05003282
3283 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003284 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003286 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003287 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288};
3289
Anas Nashifce78d162018-05-24 12:43:11 -05003290/**
3291 * @cond INTERNAL_HIDDEN
3292 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003293#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003295 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003296 .owner = NULL, \
3297 .lock_count = 0, \
3298 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003299 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300 }
3301
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003302#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003303
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003304/**
Allan Stephensc98da842016-11-11 15:45:03 -05003305 * INTERNAL_HIDDEN @endcond
3306 */
3307
3308/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003311 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003312 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003313 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003316 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003317 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003319 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003320 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003321
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003322/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003323 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003325 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * Upon completion, the mutex is available and does not have an owner.
3328 *
3329 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003330 *
3331 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003332 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003333 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003334__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003335
3336/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * This routine locks @a mutex. If the mutex is locked by another thread,
3340 * the calling thread waits until the mutex becomes available or until
3341 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * A thread is permitted to lock a mutex it has already locked. The operation
3344 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003347 * @param timeout Non-negative waiting period to lock the mutex (in
3348 * milliseconds), or one of the special values K_NO_WAIT and
3349 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003350 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003351 * @retval 0 Mutex locked.
3352 * @retval -EBUSY Returned without waiting.
3353 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003354 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003355 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003356__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003357
3358/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * This routine unlocks @a mutex. The mutex must already be locked by the
3362 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003363 *
3364 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003366 * thread.
3367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003368 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003369 *
3370 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003371 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003372 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003373__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374
Allan Stephensc98da842016-11-11 15:45:03 -05003375/**
Anas Nashif166f5192018-02-25 08:02:36 -06003376 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003377 */
3378
3379/**
3380 * @cond INTERNAL_HIDDEN
3381 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003382
3383struct k_sem {
3384 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303385 u32_t count;
3386 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003387 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003388
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003389 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003390 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003391};
3392
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003393#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003394 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003395 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003396 .count = initial_count, \
3397 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003398 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003399 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003400 }
3401
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003402#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003403
Allan Stephensc98da842016-11-11 15:45:03 -05003404/**
3405 * INTERNAL_HIDDEN @endcond
3406 */
3407
3408/**
3409 * @defgroup semaphore_apis Semaphore APIs
3410 * @ingroup kernel_apis
3411 * @{
3412 */
3413
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @param sem Address of the semaphore.
3420 * @param initial_count Initial semaphore count.
3421 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003422 *
3423 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003424 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003425 */
Andrew Boie99280232017-09-29 14:17:47 -07003426__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3427 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003428
3429/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3435 *
3436 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003437 * @param timeout Non-negative waiting period to take the semaphore (in
3438 * milliseconds), or one of the special values K_NO_WAIT and
3439 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003440 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003441 * @retval 0 Semaphore taken.
3442 * @retval -EBUSY Returned without waiting.
3443 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003444 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003445 */
Andrew Boie99280232017-09-29 14:17:47 -07003446__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003447
3448/**
3449 * @brief Give a semaphore.
3450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003451 * This routine gives @a sem, unless the semaphore is already at its maximum
3452 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003454 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003457 *
3458 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003459 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003460 */
Andrew Boie99280232017-09-29 14:17:47 -07003461__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003462
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003463/**
3464 * @brief Reset a semaphore's count to zero.
3465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003466 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003468 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003469 *
3470 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003471 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003472 */
Andrew Boie990bf162017-10-03 12:36:49 -07003473__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003474
Anas Nashif954d5502018-02-25 08:37:28 -06003475/**
3476 * @internal
3477 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003478static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003479{
Patrik Flykt24d71432019-03-26 19:57:45 -06003480 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481}
3482
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003483/**
3484 * @brief Get a semaphore's count.
3485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003488 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003491 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003492 */
Andrew Boie990bf162017-10-03 12:36:49 -07003493__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003494
Anas Nashif954d5502018-02-25 08:37:28 -06003495/**
3496 * @internal
3497 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003498static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499{
3500 return sem->count;
3501}
3502
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003504 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003507 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003508 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * @param name Name of the semaphore.
3511 * @param initial_count Initial semaphore count.
3512 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003513 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003515#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003516 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003517 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303518 BUILD_ASSERT(((count_limit) != 0) && \
3519 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003520
Anas Nashif166f5192018-02-25 08:02:36 -06003521/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003522
3523/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003524 * @defgroup msgq_apis Message Queue APIs
3525 * @ingroup kernel_apis
3526 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003527 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003528
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003529/**
3530 * @brief Message Queue Structure
3531 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003532struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05003533 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003534 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05003535 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07003536 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05003537 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003538 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003539 /** Maximal number of messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003540 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003541 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003542 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05003543 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003544 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05003545 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003547 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003548 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003549 /** Number of used messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003550 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003552 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003553 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05003554
3555 /** Message queue */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003556 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003558/**
3559 * @cond INTERNAL_HIDDEN
3560 */
3561
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003562
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003563#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003565 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003566 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003567 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003568 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003569 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003570 .read_ptr = q_buffer, \
3571 .write_ptr = q_buffer, \
3572 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003573 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003574 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003575#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003576/**
3577 * INTERNAL_HIDDEN @endcond
3578 */
3579
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003580
Andrew Boie0fe789f2018-04-12 18:35:56 -07003581#define K_MSGQ_FLAG_ALLOC BIT(0)
3582
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003583/**
3584 * @brief Message Queue Attributes
3585 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303586struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05003587 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303588 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003589 /** Maximal number of messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303590 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003591 /** Used messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303592 u32_t used_msgs;
3593};
3594
Allan Stephensc98da842016-11-11 15:45:03 -05003595
3596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3600 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003601 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3602 * message is similarly aligned to this boundary, @a q_msg_size must also be
3603 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * The message queue can be accessed outside the module where it is defined
3606 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003607 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003608 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @param q_name Name of the message queue.
3611 * @param q_msg_size Message size (in bytes).
3612 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003613 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003614 *
3615 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003616 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003617#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3618 static char __noinit __aligned(q_align) \
3619 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3620 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3621 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003622 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003623
Peter Mitsisd7a37502016-10-13 11:37:40 -04003624/**
3625 * @brief Initialize a message queue.
3626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003627 * This routine initializes a message queue object, prior to its first use.
3628 *
Allan Stephensda827222016-11-09 14:23:58 -06003629 * The message queue's ring buffer must contain space for @a max_msgs messages,
3630 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3631 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3632 * that each message is similarly aligned to this boundary, @a q_msg_size
3633 * must also be a multiple of N.
3634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * @param q Address of the message queue.
3636 * @param buffer Pointer to ring buffer that holds queued messages.
3637 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003638 * @param max_msgs Maximum number of messages that can be queued.
3639 *
3640 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003641 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003642 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003643void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3644 u32_t max_msgs);
3645
3646/**
3647 * @brief Initialize a message queue.
3648 *
3649 * This routine initializes a message queue object, prior to its first use,
3650 * allocating its internal ring buffer from the calling thread's resource
3651 * pool.
3652 *
3653 * Memory allocated for the ring buffer can be released by calling
3654 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3655 * all of its references.
3656 *
3657 * @param q Address of the message queue.
3658 * @param msg_size Message size (in bytes).
3659 * @param max_msgs Maximum number of messages that can be queued.
3660 *
3661 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3662 * thread's resource pool, or -EINVAL if the size parameters cause
3663 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003664 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003665 */
3666__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3667 u32_t max_msgs);
3668
Anas Nashife71293e2019-12-04 20:00:14 -05003669/**
3670 * @brief Cleanup message queue
3671 *
3672 * Releases memory allocated for the ring buffer.
3673 * @param q
3674 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003675void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003676
3677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003678 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003680 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003681 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003682 * @note Can be called by ISRs.
3683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003684 * @param q Address of the message queue.
3685 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003686 * @param timeout Non-negative waiting period to add the message (in
3687 * milliseconds), or one of the special values K_NO_WAIT and
3688 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003689 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003690 * @retval 0 Message sent.
3691 * @retval -ENOMSG Returned without waiting or queue purged.
3692 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003693 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003694 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003695__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003696
3697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003700 * This routine receives a message from message queue @a q in a "first in,
3701 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003702 *
Allan Stephensc98da842016-11-11 15:45:03 -05003703 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * @param q Address of the message queue.
3706 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003707 * @param timeout Non-negative waiting period to receive the message (in
3708 * milliseconds), or one of the special values K_NO_WAIT and
3709 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003710 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003711 * @retval 0 Message received.
3712 * @retval -ENOMSG Returned without waiting.
3713 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003714 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003715 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003716__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003717
3718/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003719 * @brief Peek/read a message from a message queue.
3720 *
3721 * This routine reads a message from message queue @a q in a "first in,
3722 * first out" manner and leaves the message in the queue.
3723 *
3724 * @note Can be called by ISRs.
3725 *
3726 * @param q Address of the message queue.
3727 * @param data Address of area to hold the message read from the queue.
3728 *
3729 * @retval 0 Message read.
3730 * @retval -ENOMSG Returned when the queue has no message.
3731 * @req K-MSGQ-002
3732 */
3733__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3734
3735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003736 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003738 * This routine discards all unreceived messages in a message queue's ring
3739 * buffer. Any threads that are blocked waiting to send a message to the
3740 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003742 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003743 *
3744 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003745 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003746 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003747__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003748
Peter Mitsis67be2492016-10-07 11:44:34 -04003749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003750 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003752 * This routine returns the number of unused entries in a message queue's
3753 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003755 * @param q Address of the message queue.
3756 *
3757 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003758 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003759 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003760__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3761
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303762/**
3763 * @brief Get basic attributes of a message queue.
3764 *
3765 * This routine fetches basic attributes of message queue into attr argument.
3766 *
3767 * @param q Address of the message queue.
3768 * @param attrs pointer to message queue attribute structure.
3769 *
3770 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003771 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303772 */
3773__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3774
3775
Patrik Flykt4344e272019-03-08 14:19:05 -07003776static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003777{
3778 return q->max_msgs - q->used_msgs;
3779}
3780
Peter Mitsisd7a37502016-10-13 11:37:40 -04003781/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003782 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003784 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003786 * @param q Address of the message queue.
3787 *
3788 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003789 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003790 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003791__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3792
Patrik Flykt4344e272019-03-08 14:19:05 -07003793static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794{
3795 return q->used_msgs;
3796}
3797
Anas Nashif166f5192018-02-25 08:02:36 -06003798/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003799
3800/**
3801 * @defgroup mem_pool_apis Memory Pool APIs
3802 * @ingroup kernel_apis
3803 * @{
3804 */
3805
Andy Ross73cb9582017-05-09 10:42:39 -07003806/* Note on sizing: the use of a 20 bit field for block means that,
3807 * assuming a reasonable minimum block size of 16 bytes, we're limited
3808 * to 16M of memory managed by a single pool. Long term it would be
3809 * good to move to a variable bit size based on configuration.
3810 */
3811struct k_mem_block_id {
3812 u32_t pool : 8;
3813 u32_t level : 4;
3814 u32_t block : 20;
3815};
3816
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003817struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003818 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003819 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003820};
3821
Anas Nashif166f5192018-02-25 08:02:36 -06003822/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003823
3824/**
3825 * @defgroup mailbox_apis Mailbox APIs
3826 * @ingroup kernel_apis
3827 * @{
3828 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829
Anas Nashife71293e2019-12-04 20:00:14 -05003830/**
3831 * @brief Mailbox Message Structure
3832 *
3833 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003834struct k_mbox_msg {
3835 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003836 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003837 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003838 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003840 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 /** sender's message data buffer */
3842 void *tx_data;
3843 /** internal use only - needed for legacy API support */
3844 void *_rx_data;
3845 /** message data block descriptor */
3846 struct k_mem_block tx_block;
3847 /** source thread id */
3848 k_tid_t rx_source_thread;
3849 /** target thread id */
3850 k_tid_t tx_target_thread;
3851 /** internal use only - thread waiting on send (may be a dummy) */
3852 k_tid_t _syncing_thread;
3853#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3854 /** internal use only - semaphore used during asynchronous send */
3855 struct k_sem *_async_sem;
3856#endif
3857};
Anas Nashife71293e2019-12-04 20:00:14 -05003858/**
3859 * @brief Mailbox Structure
3860 *
3861 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003862struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05003863 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05003865 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003867 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003868
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003869 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003870 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003871};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003872/**
3873 * @cond INTERNAL_HIDDEN
3874 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003876#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003877 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003878 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3879 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003880 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881 }
3882
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003883#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003884
Peter Mitsis12092702016-10-14 12:57:23 -04003885/**
Allan Stephensc98da842016-11-11 15:45:03 -05003886 * INTERNAL_HIDDEN @endcond
3887 */
3888
3889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003890 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003892 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003893 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003894 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003896 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003897 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003898 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003899#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003900 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003901 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003902
Peter Mitsis12092702016-10-14 12:57:23 -04003903/**
3904 * @brief Initialize a mailbox.
3905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003906 * This routine initializes a mailbox object, prior to its first use.
3907 *
3908 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003909 *
3910 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003911 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003913extern void k_mbox_init(struct k_mbox *mbox);
3914
Peter Mitsis12092702016-10-14 12:57:23 -04003915/**
3916 * @brief Send a mailbox message in a synchronous manner.
3917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003918 * This routine sends a message to @a mbox and waits for a receiver to both
3919 * receive and process it. The message data may be in a buffer, in a memory
3920 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * @param mbox Address of the mailbox.
3923 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003924 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003925 * milliseconds), or one of the special values K_NO_WAIT
3926 * and K_FOREVER. Once the message has been received,
3927 * this routine waits as long as necessary for the message
3928 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003929 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003930 * @retval 0 Message sent.
3931 * @retval -ENOMSG Returned without waiting.
3932 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003933 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003934 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003935extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003936 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003937
Peter Mitsis12092702016-10-14 12:57:23 -04003938/**
3939 * @brief Send a mailbox message in an asynchronous manner.
3940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * This routine sends a message to @a mbox without waiting for a receiver
3942 * to process it. The message data may be in a buffer, in a memory pool block,
3943 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3944 * will be given when the message has been both received and completely
3945 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * @param mbox Address of the mailbox.
3948 * @param tx_msg Address of the transmit message descriptor.
3949 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003950 *
3951 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003952 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003953 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003954extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003955 struct k_sem *sem);
3956
Peter Mitsis12092702016-10-14 12:57:23 -04003957/**
3958 * @brief Receive a mailbox message.
3959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * This routine receives a message from @a mbox, then optionally retrieves
3961 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * @param mbox Address of the mailbox.
3964 * @param rx_msg Address of the receive message descriptor.
3965 * @param buffer Address of the buffer to receive data, or NULL to defer data
3966 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003967 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003968 * milliseconds), or one of the special values K_NO_WAIT
3969 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003970 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003971 * @retval 0 Message received.
3972 * @retval -ENOMSG Returned without waiting.
3973 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003974 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003975 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003976extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003977 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003978
3979/**
3980 * @brief Retrieve mailbox message data into a buffer.
3981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003982 * This routine completes the processing of a received message by retrieving
3983 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003984 *
3985 * Alternatively, this routine can be used to dispose of a received message
3986 * without retrieving its data.
3987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003988 * @param rx_msg Address of the receive message descriptor.
3989 * @param buffer Address of the buffer to receive data, or NULL to discard
3990 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003991 *
3992 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003993 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003994 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003995extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003996
3997/**
3998 * @brief Retrieve mailbox message data into a memory pool block.
3999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004000 * This routine completes the processing of a received message by retrieving
4001 * its data into a memory pool block, then disposing of the message.
4002 * The memory pool block that results from successful retrieval must be
4003 * returned to the pool once the data has been processed, even in cases
4004 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04004005 *
4006 * Alternatively, this routine can be used to dispose of a received message
4007 * without retrieving its data. In this case there is no need to return a
4008 * memory pool block to the pool.
4009 *
4010 * This routine allocates a new memory pool block for the data only if the
4011 * data is not already in one. If a new block cannot be allocated, the routine
4012 * returns a failure code and the received message is left unchanged. This
4013 * permits the caller to reattempt data retrieval at a later time or to dispose
4014 * of the received message without retrieving its data.
4015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004016 * @param rx_msg Address of a receive message descriptor.
4017 * @param pool Address of memory pool, or NULL to discard data.
4018 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004019 * @param timeout Non-negative waiting period to wait for a memory pool block
4020 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004022 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004023 * @retval 0 Data retrieved.
4024 * @retval -ENOMEM Returned without waiting.
4025 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004026 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04004027 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004028extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04004029 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05004030 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004031
Anas Nashif166f5192018-02-25 08:02:36 -06004032/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004033
4034/**
Anas Nashifce78d162018-05-24 12:43:11 -05004035 * @defgroup pipe_apis Pipe APIs
4036 * @ingroup kernel_apis
4037 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004038 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004039
Anas Nashifce78d162018-05-24 12:43:11 -05004040/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004041struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004042 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4043 size_t size; /**< Buffer size */
4044 size_t bytes_used; /**< # bytes used in buffer */
4045 size_t read_index; /**< Where in buffer to read from */
4046 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004047 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004048
4049 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004050 _wait_q_t readers; /**< Reader wait queue */
4051 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004052 } wait_q;
4053
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004054 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004055 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05004056 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004057};
4058
Anas Nashifce78d162018-05-24 12:43:11 -05004059/**
4060 * @cond INTERNAL_HIDDEN
4061 */
4062#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4063
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004064#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
4065 { \
4066 .buffer = pipe_buffer, \
4067 .size = pipe_buffer_size, \
4068 .bytes_used = 0, \
4069 .read_index = 0, \
4070 .write_index = 0, \
4071 .lock = {}, \
4072 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004073 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4074 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004075 }, \
4076 _OBJECT_TRACING_INIT \
4077 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004078 }
4079
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004080#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004081
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004082/**
Allan Stephensc98da842016-11-11 15:45:03 -05004083 * INTERNAL_HIDDEN @endcond
4084 */
4085
4086/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004087 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004089 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004090 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004091 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004093 * @param name Name of the pipe.
4094 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4095 * or zero if no ring buffer is used.
4096 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004097 *
4098 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004099 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004100#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004101 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004102 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004103 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004104 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004107 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004109 * This routine initializes a pipe object, prior to its first use.
4110 *
4111 * @param pipe Address of the pipe.
4112 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4113 * is used.
4114 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4115 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004116 *
4117 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004118 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004119 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004120void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4121
4122/**
4123 * @brief Release a pipe's allocated buffer
4124 *
4125 * If a pipe object was given a dynamically allocated buffer via
4126 * k_pipe_alloc_init(), this will free it. This function does nothing
4127 * if the buffer wasn't dynamically allocated.
4128 *
4129 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004130 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004131 */
4132void k_pipe_cleanup(struct k_pipe *pipe);
4133
4134/**
4135 * @brief Initialize a pipe and allocate a buffer for it
4136 *
4137 * Storage for the buffer region will be allocated from the calling thread's
4138 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4139 * or userspace is enabled and the pipe object loses all references to it.
4140 *
4141 * This function should only be called on uninitialized pipe objects.
4142 *
4143 * @param pipe Address of the pipe.
4144 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4145 * buffer is used.
4146 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004147 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004148 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004149 */
4150__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004151
4152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004153 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004155 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004157 * @param pipe Address of the pipe.
4158 * @param data Address of data to write.
4159 * @param bytes_to_write Size of data (in bytes).
4160 * @param bytes_written Address of area to hold the number of bytes written.
4161 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004162 * @param timeout Non-negative waiting period to wait for the data to be written
4163 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004164 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004165 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004166 * @retval 0 At least @a min_xfer bytes of data were written.
4167 * @retval -EIO Returned without waiting; zero data bytes were written.
4168 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004169 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004170 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004171 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004172__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4173 size_t bytes_to_write, size_t *bytes_written,
4174 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004175
4176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004177 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004179 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004181 * @param pipe Address of the pipe.
4182 * @param data Address to place the data read from pipe.
4183 * @param bytes_to_read Maximum number of data bytes to read.
4184 * @param bytes_read Address of area to hold the number of bytes read.
4185 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004186 * @param timeout Non-negative waiting period to wait for the data to be read
4187 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004188 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004189 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004190 * @retval 0 At least @a min_xfer bytes of data were read.
4191 * @retval -EIO Returned without waiting; zero data bytes were read.
4192 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004193 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004194 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004195 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004196__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4197 size_t bytes_to_read, size_t *bytes_read,
4198 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004199
4200/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004201 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004203 * This routine writes the data contained in a memory block to @a pipe.
4204 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004205 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004207 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004208 * @param block Memory block containing data to send
4209 * @param size Number of data bytes in memory block to send
4210 * @param sem Semaphore to signal upon completion (else NULL)
4211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004212 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004213 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004214 */
4215extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4216 size_t size, struct k_sem *sem);
4217
Anas Nashif166f5192018-02-25 08:02:36 -06004218/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004219
Allan Stephensc98da842016-11-11 15:45:03 -05004220/**
4221 * @cond INTERNAL_HIDDEN
4222 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004223
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004224struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004225 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004226 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004227 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004228 char *buffer;
4229 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004230 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004231
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004232 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004233 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004234};
4235
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004236#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004237 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004238 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004239 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004240 .num_blocks = slab_num_blocks, \
4241 .block_size = slab_block_size, \
4242 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004243 .free_list = NULL, \
4244 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004245 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004246 }
4247
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004248#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004249
4250
Peter Mitsis578f9112016-10-07 13:50:31 -04004251/**
Allan Stephensc98da842016-11-11 15:45:03 -05004252 * INTERNAL_HIDDEN @endcond
4253 */
4254
4255/**
4256 * @defgroup mem_slab_apis Memory Slab APIs
4257 * @ingroup kernel_apis
4258 * @{
4259 */
4260
4261/**
Allan Stephensda827222016-11-09 14:23:58 -06004262 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004263 *
Allan Stephensda827222016-11-09 14:23:58 -06004264 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004265 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004266 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4267 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004268 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004269 *
Allan Stephensda827222016-11-09 14:23:58 -06004270 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004271 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004272 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004273 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004275 * @param name Name of the memory slab.
4276 * @param slab_block_size Size of each memory block (in bytes).
4277 * @param slab_num_blocks Number memory blocks.
4278 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004279 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004280 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004281#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004282 char __noinit __aligned(WB_UP(slab_align)) \
4283 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004284 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004285 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004286 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004287
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004288/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004289 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004291 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004292 *
Allan Stephensda827222016-11-09 14:23:58 -06004293 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4294 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004295 * N-byte boundary matching a word boundary, where N is a power of 2
4296 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004297 * To ensure that each memory block is similarly aligned to this boundary,
4298 * @a slab_block_size must also be a multiple of N.
4299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004300 * @param slab Address of the memory slab.
4301 * @param buffer Pointer to buffer used for the memory blocks.
4302 * @param block_size Size of each memory block (in bytes).
4303 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004304 *
4305 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004306 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004307 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004308extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004309 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004310
4311/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004312 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004314 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004316 * @param slab Address of the memory slab.
4317 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004318 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004319 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4320 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004321 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004322 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004323 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004324 * @retval -ENOMEM Returned without waiting.
4325 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004326 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004327 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004328extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004329 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004330
4331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004332 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004334 * This routine releases a previously allocated memory block back to its
4335 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004337 * @param slab Address of the memory slab.
4338 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004339 *
4340 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004341 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004342 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004343extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004344
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004345/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004346 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004348 * This routine gets the number of memory blocks that are currently
4349 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004351 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004353 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004354 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004355 */
Kumar Galacc334c72017-04-21 10:55:34 -05004356static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004357{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004358 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004359}
4360
Peter Mitsisc001aa82016-10-13 13:53:37 -04004361/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004362 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004364 * This routine gets the number of memory blocks that are currently
4365 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004367 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004369 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004370 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004371 */
Kumar Galacc334c72017-04-21 10:55:34 -05004372static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004373{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004374 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004375}
4376
Anas Nashif166f5192018-02-25 08:02:36 -06004377/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004378
4379/**
4380 * @cond INTERNAL_HIDDEN
4381 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004382
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004383struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004384 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004385 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004386};
4387
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004388/**
Allan Stephensc98da842016-11-11 15:45:03 -05004389 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004390 */
4391
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004392/**
Allan Stephensc98da842016-11-11 15:45:03 -05004393 * @addtogroup mem_pool_apis
4394 * @{
4395 */
4396
4397/**
4398 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004400 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4401 * long. The memory pool allows blocks to be repeatedly partitioned into
4402 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004403 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004404 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004405 * If the pool is to be accessed outside the module where it is defined, it
4406 * can be declared via
4407 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004408 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004410 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004411 * @param minsz Size of the smallest blocks in the pool (in bytes).
4412 * @param maxsz Size of the largest blocks in the pool (in bytes).
4413 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004414 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004415 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004416 */
Andy Ross73cb9582017-05-09 10:42:39 -07004417#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004418 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004419 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004420 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004421 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004422 .base = { \
4423 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004424 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004425 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004426 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004427 .levels = _mpool_lvls_##name, \
4428 .flags = SYS_MEM_POOL_KERNEL \
4429 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004430 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004431 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004432
Peter Mitsis937042c2016-10-13 13:18:26 -04004433/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004434 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004436 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004438 * @param pool Address of the memory pool.
4439 * @param block Pointer to block descriptor for the allocated memory.
4440 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004441 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004442 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4443 * or K_FOREVER to wait as long as necessary.
4444 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004445 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004446 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004447 * @retval -ENOMEM Returned without waiting.
4448 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004449 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004450 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004451extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004452 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004453
4454/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004455 * @brief Allocate memory from a memory pool with malloc() semantics
4456 *
4457 * Such memory must be released using k_free().
4458 *
4459 * @param pool Address of the memory pool.
4460 * @param size Amount of memory to allocate (in bytes).
4461 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004462 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004463 */
4464extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4465
4466/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004467 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004469 * This routine releases a previously allocated memory block back to its
4470 * memory pool.
4471 *
4472 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004473 *
4474 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004475 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004476 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004477extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004478
4479/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004480 * @brief Free memory allocated from a memory pool.
4481 *
4482 * This routine releases a previously allocated memory block back to its
4483 * memory pool
4484 *
4485 * @param id Memory block identifier.
4486 *
4487 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004488 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004489 */
4490extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4491
4492/**
Anas Nashif166f5192018-02-25 08:02:36 -06004493 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004494 */
4495
4496/**
4497 * @defgroup heap_apis Heap Memory Pool APIs
4498 * @ingroup kernel_apis
4499 * @{
4500 */
4501
4502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004503 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004505 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004506 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004508 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004510 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004511 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004512 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004513extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004514
4515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004516 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004517 *
4518 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004519 * returned must have been allocated from the heap memory pool or
4520 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004521 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004522 * If @a ptr is NULL, no operation is performed.
4523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004524 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004525 *
4526 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004527 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004528 */
4529extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004530
Allan Stephensc98da842016-11-11 15:45:03 -05004531/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004532 * @brief Allocate memory from heap, array style
4533 *
4534 * This routine provides traditional calloc() semantics. Memory is
4535 * allocated from the heap memory pool and zeroed.
4536 *
4537 * @param nmemb Number of elements in the requested array
4538 * @param size Size of each array element (in bytes).
4539 *
4540 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004541 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004542 */
4543extern void *k_calloc(size_t nmemb, size_t size);
4544
Anas Nashif166f5192018-02-25 08:02:36 -06004545/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004546
Benjamin Walshacc68c12017-01-29 18:57:45 -05004547/* polling API - PRIVATE */
4548
Benjamin Walshb0179862017-02-02 16:39:57 -05004549#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004550#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004551#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004552#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004553#endif
4554
Benjamin Walshacc68c12017-01-29 18:57:45 -05004555/* private - types bit positions */
4556enum _poll_types_bits {
4557 /* can be used to ignore an event */
4558 _POLL_TYPE_IGNORE,
4559
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004560 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004561 _POLL_TYPE_SIGNAL,
4562
4563 /* semaphore availability */
4564 _POLL_TYPE_SEM_AVAILABLE,
4565
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004566 /* queue/fifo/lifo data availability */
4567 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004568
4569 _POLL_NUM_TYPES
4570};
4571
Patrik Flykt4344e272019-03-08 14:19:05 -07004572#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004573
4574/* private - states bit positions */
4575enum _poll_states_bits {
4576 /* default state when creating event */
4577 _POLL_STATE_NOT_READY,
4578
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004579 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004580 _POLL_STATE_SIGNALED,
4581
4582 /* semaphore is available */
4583 _POLL_STATE_SEM_AVAILABLE,
4584
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004585 /* data is available to read on queue/fifo/lifo */
4586 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004587
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004588 /* queue/fifo/lifo wait was cancelled */
4589 _POLL_STATE_CANCELLED,
4590
Benjamin Walshacc68c12017-01-29 18:57:45 -05004591 _POLL_NUM_STATES
4592};
4593
Patrik Flykt4344e272019-03-08 14:19:05 -07004594#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004595
4596#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004597 (32 - (0 \
4598 + 8 /* tag */ \
4599 + _POLL_NUM_TYPES \
4600 + _POLL_NUM_STATES \
4601 + 1 /* modes */ \
4602 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004603
Benjamin Walshacc68c12017-01-29 18:57:45 -05004604/* end of polling API - PRIVATE */
4605
4606
4607/**
4608 * @defgroup poll_apis Async polling APIs
4609 * @ingroup kernel_apis
4610 * @{
4611 */
4612
4613/* Public polling API */
4614
4615/* public - values for k_poll_event.type bitfield */
4616#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004617#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4618#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4619#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004620#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004621
4622/* public - polling modes */
4623enum k_poll_modes {
4624 /* polling thread does not take ownership of objects when available */
4625 K_POLL_MODE_NOTIFY_ONLY = 0,
4626
4627 K_POLL_NUM_MODES
4628};
4629
4630/* public - values for k_poll_event.state bitfield */
4631#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004632#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4633#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4634#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004635#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004636#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004637
4638/* public - poll signal object */
4639struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05004640 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004641 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004642
Anas Nashife71293e2019-12-04 20:00:14 -05004643 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004644 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4645 * user resets it to 0.
4646 */
4647 unsigned int signaled;
4648
Anas Nashife71293e2019-12-04 20:00:14 -05004649 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004650 int result;
4651};
4652
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004653#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004654 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004655 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004656 .signaled = 0, \
4657 .result = 0, \
4658 }
Anas Nashife71293e2019-12-04 20:00:14 -05004659/**
4660 * @brief Poll Event
4661 *
4662 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004663struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05004664 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004665 sys_dnode_t _node;
4666
Anas Nashife71293e2019-12-04 20:00:14 -05004667 /** PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004668 struct _poller *poller;
4669
Anas Nashife71293e2019-12-04 20:00:14 -05004670 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004671 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004672
Anas Nashife71293e2019-12-04 20:00:14 -05004673 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004674 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004675
Anas Nashife71293e2019-12-04 20:00:14 -05004676 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004677 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004678
Anas Nashife71293e2019-12-04 20:00:14 -05004679 /** mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004680 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004681
Anas Nashife71293e2019-12-04 20:00:14 -05004682 /** unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004683 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004684
Anas Nashife71293e2019-12-04 20:00:14 -05004685 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004686 union {
4687 void *obj;
4688 struct k_poll_signal *signal;
4689 struct k_sem *sem;
4690 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004691 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004692 };
4693};
4694
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004695#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004696 { \
4697 .poller = NULL, \
4698 .type = event_type, \
4699 .state = K_POLL_STATE_NOT_READY, \
4700 .mode = event_mode, \
4701 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004702 { .obj = event_obj }, \
4703 }
4704
4705#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4706 event_tag) \
4707 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004708 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004709 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004710 .state = K_POLL_STATE_NOT_READY, \
4711 .mode = event_mode, \
4712 .unused = 0, \
4713 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004714 }
4715
4716/**
4717 * @brief Initialize one struct k_poll_event instance
4718 *
4719 * After this routine is called on a poll event, the event it ready to be
4720 * placed in an event array to be passed to k_poll().
4721 *
4722 * @param event The event to initialize.
4723 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4724 * values. Only values that apply to the same object being polled
4725 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4726 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004727 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004728 * @param obj Kernel object or poll signal.
4729 *
4730 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004731 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004732 */
4733
Kumar Galacc334c72017-04-21 10:55:34 -05004734extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004735 int mode, void *obj);
4736
4737/**
4738 * @brief Wait for one or many of multiple poll events to occur
4739 *
4740 * This routine allows a thread to wait concurrently for one or many of
4741 * multiple poll events to have occurred. Such events can be a kernel object
4742 * being available, like a semaphore, or a poll signal event.
4743 *
4744 * When an event notifies that a kernel object is available, the kernel object
4745 * is not "given" to the thread calling k_poll(): it merely signals the fact
4746 * that the object was available when the k_poll() call was in effect. Also,
4747 * all threads trying to acquire an object the regular way, i.e. by pending on
4748 * the object, have precedence over the thread polling on the object. This
4749 * means that the polling thread will never get the poll event on an object
4750 * until the object becomes available and its pend queue is empty. For this
4751 * reason, the k_poll() call is more effective when the objects being polled
4752 * only have one thread, the polling thread, trying to acquire them.
4753 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004754 * When k_poll() returns 0, the caller should loop on all the events that were
4755 * passed to k_poll() and check the state field for the values that were
4756 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004757 *
4758 * Before being reused for another call to k_poll(), the user has to reset the
4759 * state field to K_POLL_STATE_NOT_READY.
4760 *
Andrew Boie3772f772018-05-07 16:52:57 -07004761 * When called from user mode, a temporary memory allocation is required from
4762 * the caller's resource pool.
4763 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004764 * @param events An array of pointers to events to be polled for.
4765 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004766 * @param timeout Non-negative waiting period for an event to be ready (in
4767 * milliseconds), or one of the special values K_NO_WAIT and
4768 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004769 *
4770 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004771 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004772 * @retval -EINTR Polling has been interrupted, e.g. with
4773 * k_queue_cancel_wait(). All output events are still set and valid,
4774 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4775 * words, -EINTR status means that at least one of output events is
4776 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004777 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4778 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004779 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004780 */
4781
Andrew Boie3772f772018-05-07 16:52:57 -07004782__syscall int k_poll(struct k_poll_event *events, int num_events,
4783 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004784
4785/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004786 * @brief Initialize a poll signal object.
4787 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004788 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004789 *
4790 * @param signal A poll signal.
4791 *
4792 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004793 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004794 */
4795
Andrew Boie3772f772018-05-07 16:52:57 -07004796__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4797
4798/*
4799 * @brief Reset a poll signal object's state to unsignaled.
4800 *
4801 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004802 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004803 */
4804__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4805
Patrik Flykt4344e272019-03-08 14:19:05 -07004806static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004807{
Patrik Flykt24d71432019-03-26 19:57:45 -06004808 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004809}
4810
4811/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004812 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004813 *
4814 * @param signal A poll signal object
4815 * @param signaled An integer buffer which will be written nonzero if the
4816 * object was signaled
4817 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004818 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004819 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004820 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004821 */
4822__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4823 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004824
4825/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004826 * @brief Signal a poll signal object.
4827 *
4828 * This routine makes ready a poll signal, which is basically a poll event of
4829 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4830 * made ready to run. A @a result value can be specified.
4831 *
4832 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004833 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004834 * k_poll_signal_reset(). It thus has to be reset by the user before being
4835 * passed again to k_poll() or k_poll() will consider it being signaled, and
4836 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004837 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004838 * @note The result is stored and the 'signaled' field is set even if
4839 * this function returns an error indicating that an expiring poll was
4840 * not notified. The next k_poll() will detect the missed raise.
4841 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004842 * @param signal A poll signal.
4843 * @param result The value to store in the result field of the signal.
4844 *
4845 * @retval 0 The signal was delivered successfully.
4846 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004847 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004848 */
4849
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004850__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004851
Anas Nashif954d5502018-02-25 08:37:28 -06004852/**
4853 * @internal
4854 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004855extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004856
Anas Nashif166f5192018-02-25 08:02:36 -06004857/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004858
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004859/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004860 * @defgroup cpu_idle_apis CPU Idling APIs
4861 * @ingroup kernel_apis
4862 * @{
4863 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004864/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004865 * @brief Make the CPU idle.
4866 *
4867 * This function makes the CPU idle until an event wakes it up.
4868 *
4869 * In a regular system, the idle thread should be the only thread responsible
4870 * for making the CPU idle and triggering any type of power management.
4871 * However, in some more constrained systems, such as a single-threaded system,
4872 * the only thread would be responsible for this if needed.
4873 *
4874 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004875 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004876 */
Andrew Boie07525a32019-09-21 16:17:23 -07004877static inline void k_cpu_idle(void)
4878{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004879 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004880}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004881
4882/**
4883 * @brief Make the CPU idle in an atomic fashion.
4884 *
4885 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4886 * must be done atomically before making the CPU idle.
4887 *
4888 * @param key Interrupt locking key obtained from irq_lock().
4889 *
4890 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004891 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004892 */
Andrew Boie07525a32019-09-21 16:17:23 -07004893static inline void k_cpu_atomic_idle(unsigned int key)
4894{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004895 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004896}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004897
Anas Nashif30c3cff2019-01-22 08:18:13 -05004898/**
4899 * @}
4900 */
Anas Nashif954d5502018-02-25 08:37:28 -06004901
4902/**
4903 * @internal
4904 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004905extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004906
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004907#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004908/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004909#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004910#else
4911
Andrew Boiecdb94d62017-04-18 15:22:05 -07004912/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004913 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004914 *
4915 * We won't have a real exception frame to determine the PC value when
4916 * the oops occurred, so print file and line number before we jump into
4917 * the fatal error handler.
4918 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004919#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004920 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004921 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004922 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004923
4924#endif /* _ARCH__EXCEPT */
4925
4926/**
4927 * @brief Fatally terminate a thread
4928 *
4929 * This should be called when a thread has encountered an unrecoverable
4930 * runtime condition and needs to terminate. What this ultimately
4931 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004932 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004933 *
4934 * If this is called from ISR context, the default system fatal error handler
4935 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004936 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004937 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004938#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004939
4940/**
4941 * @brief Fatally terminate the system
4942 *
4943 * This should be called when the Zephyr kernel has encountered an
4944 * unrecoverable runtime condition and needs to terminate. What this ultimately
4945 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004946 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004947 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004948 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004949#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004950
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004951/*
4952 * private APIs that are utilized by one or more public APIs
4953 */
4954
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09004955/**
4956 * @internal
4957 */
4958extern void z_init_thread_base(struct _thread_base *thread_base,
4959 int priority, u32_t initial_state,
4960 unsigned int options);
4961
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004962#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004963/**
4964 * @internal
4965 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004966extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004967#else
Anas Nashif954d5502018-02-25 08:37:28 -06004968/**
4969 * @internal
4970 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004971#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004972#endif
4973
Anas Nashif954d5502018-02-25 08:37:28 -06004974/**
4975 * @internal
4976 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004977extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004978/**
4979 * @internal
4980 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004981extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004982
Andrew Boiedc5d9352017-06-02 12:56:47 -07004983/* arch/cpu.h may declare an architecture or platform-specific macro
4984 * for properly declaring stacks, compatible with MMU/MPU constraints if
4985 * enabled
4986 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004987
4988/**
4989 * @brief Obtain an extern reference to a stack
4990 *
4991 * This macro properly brings the symbol of a thread stack declared
4992 * elsewhere into scope.
4993 *
4994 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004995 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004996 */
4997#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4998
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004999#ifdef ARCH_THREAD_STACK_DEFINE
5000#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07005001#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005002 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
5003#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
5004#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
5005#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boieb5c68102019-11-21 17:38:17 -08005006#define K_THREAD_STACK_RESERVED ((size_t)ARCH_THREAD_STACK_RESERVED)
Andrew Boie4e5c0932019-04-04 12:05:28 -07005007static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005008{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005009 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07005010}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005011#else
5012/**
5013 * @brief Declare a toplevel thread stack memory region
5014 *
5015 * This declares a region of memory suitable for use as a thread's stack.
5016 *
5017 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5018 * 'noinit' section so that it isn't zeroed at boot
5019 *
Andrew Boie507852a2017-07-25 18:47:07 -07005020 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04005021 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07005022 * inside needs to be examined, examine thread->stack_info for the associated
5023 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005024 *
5025 * It is legal to precede this definition with the 'static' keyword.
5026 *
5027 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
5028 * parameter of k_thread_create(), it may not be the same as the
5029 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
5030 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005031 * Some arches may round the size of the usable stack region up to satisfy
5032 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
5033 * size.
5034 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07005035 * @param sym Thread stack symbol name
5036 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005037 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005038 */
5039#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005040 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005041
5042/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305043 * @brief Calculate size of stacks to be allocated in a stack array
5044 *
5045 * This macro calculates the size to be allocated for the stacks
5046 * inside a stack array. It accepts the indicated "size" as a parameter
5047 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
5048 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
5049 *
5050 * @param size Size of the stack memory region
5051 * @req K-TSTACK-001
5052 */
5053#define K_THREAD_STACK_LEN(size) (size)
5054
5055/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07005056 * @brief Declare a toplevel array of thread stack memory regions
5057 *
5058 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
5059 * definition for additional details and constraints.
5060 *
5061 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5062 * 'noinit' section so that it isn't zeroed at boot
5063 *
5064 * @param sym Thread stack symbol name
5065 * @param nmemb Number of stacks to declare
5066 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005067 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005068 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07005069#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005070 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305071 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005072
5073/**
5074 * @brief Declare an embedded stack memory region
5075 *
5076 * Used for stacks embedded within other data structures. Use is highly
5077 * discouraged but in some cases necessary. For memory protection scenarios,
5078 * it is very important that any RAM preceding this member not be writable
5079 * by threads else a stack overflow will lead to silent corruption. In other
5080 * words, the containing data structure should live in RAM owned by the kernel.
5081 *
5082 * @param sym Thread stack symbol name
5083 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005084 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005085 */
5086#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005087 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005088
5089/**
5090 * @brief Return the size in bytes of a stack memory region
5091 *
5092 * Convenience macro for passing the desired stack size to k_thread_create()
5093 * since the underlying implementation may actually create something larger
5094 * (for instance a guard area).
5095 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005096 * The value returned here is not guaranteed to match the 'size' parameter
5097 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005098 *
5099 * @param sym Stack memory symbol
5100 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005101 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005102 */
5103#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5104
Andrew Boie575abc02019-03-19 10:42:24 -07005105
5106/**
5107 * @brief Indicate how much additional memory is reserved for stack objects
5108 *
5109 * Any given stack declaration may have additional memory in it for guard
5110 * areas or supervisor mode stacks. This macro indicates how much space
5111 * is reserved for this. The memory reserved may not be contiguous within
5112 * the stack object, and does not account for additional space used due to
5113 * enforce alignment.
5114 */
Andrew Boieb5c68102019-11-21 17:38:17 -08005115#define K_THREAD_STACK_RESERVED ((size_t)0U)
Andrew Boie575abc02019-03-19 10:42:24 -07005116
Andrew Boiedc5d9352017-06-02 12:56:47 -07005117/**
5118 * @brief Get a pointer to the physical stack buffer
5119 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005120 * This macro is deprecated. If a stack buffer needs to be examined, the
5121 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005122 *
5123 * @param sym Declared stack symbol name
5124 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005125 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005126 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005127static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005128{
5129 return (char *)sym;
5130}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005131
5132#endif /* _ARCH_DECLARE_STACK */
5133
Chunlin Hane9c97022017-07-07 20:29:30 +08005134/**
5135 * @defgroup mem_domain_apis Memory domain APIs
5136 * @ingroup kernel_apis
5137 * @{
5138 */
5139
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005140/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005141 * @def K_MEM_PARTITION_DEFINE
5142 * @brief Used to declare a memory partition
5143 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005144 */
5145#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5146#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5147 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005148 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005149 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005150#else
5151#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005152 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005153 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005154#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5155
Chunlin Hane9c97022017-07-07 20:29:30 +08005156/* memory partition */
5157struct k_mem_partition {
Anas Nashife71293e2019-12-04 20:00:14 -05005158 /** start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005159 uintptr_t start;
Anas Nashife71293e2019-12-04 20:00:14 -05005160 /** size of memory partition */
Andrew Boiea8248212019-11-13 12:10:56 -08005161 size_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005162#if defined(CONFIG_MEMORY_PROTECTION)
Anas Nashife71293e2019-12-04 20:00:14 -05005163 /** attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305164 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005165#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005166};
5167
Anas Nashife71293e2019-12-04 20:00:14 -05005168/**
5169 * @brief Memory Domain
5170 *
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305171 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005172struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305173#ifdef CONFIG_USERSPACE
Anas Nashife71293e2019-12-04 20:00:14 -05005174 /** partitions in the domain */
Chunlin Hane9c97022017-07-07 20:29:30 +08005175 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305176#endif /* CONFIG_USERSPACE */
Anas Nashife71293e2019-12-04 20:00:14 -05005177 /** domain q */
Chunlin Hane9c97022017-07-07 20:29:30 +08005178 sys_dlist_t mem_domain_q;
Anas Nashife71293e2019-12-04 20:00:14 -05005179 /** number of partitions in the domain */
Leandro Pereira08de6582018-02-28 14:22:57 -08005180 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005181};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305182
Chunlin Hane9c97022017-07-07 20:29:30 +08005183
5184/**
5185 * @brief Initialize a memory domain.
5186 *
5187 * Initialize a memory domain with given name and memory partitions.
5188 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005189 * See documentation for k_mem_domain_add_partition() for details about
5190 * partition constraints.
5191 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005192 * @param domain The memory domain to be initialized.
5193 * @param num_parts The number of array items of "parts" parameter.
5194 * @param parts An array of pointers to the memory partitions. Can be NULL
5195 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005196 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005197 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005198extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005199 struct k_mem_partition *parts[]);
5200/**
5201 * @brief Destroy a memory domain.
5202 *
5203 * Destroy a memory domain.
5204 *
5205 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005206 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005207 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005208extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5209
5210/**
5211 * @brief Add a memory partition into a memory domain.
5212 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005213 * Add a memory partition into a memory domain. Partitions must conform to
5214 * the following constraints:
5215 *
5216 * - Partition bounds must be within system RAM boundaries on MMU-based
5217 * systems.
5218 * - Partitions in the same memory domain may not overlap each other.
5219 * - Partitions must not be defined which expose private kernel
5220 * data structures or kernel objects.
5221 * - The starting address alignment, and the partition size must conform to
5222 * the constraints of the underlying memory management hardware, which
5223 * varies per architecture.
5224 * - Memory domain partitions are only intended to control access to memory
5225 * from user mode threads.
5226 *
5227 * Violating these constraints may lead to CPU exceptions or undefined
5228 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005229 *
5230 * @param domain The memory domain to be added a memory partition.
5231 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005232 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005233 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005234extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5235 struct k_mem_partition *part);
5236
5237/**
5238 * @brief Remove a memory partition from a memory domain.
5239 *
5240 * Remove a memory partition from a memory domain.
5241 *
5242 * @param domain The memory domain to be removed a memory partition.
5243 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005244 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005245 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005246extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5247 struct k_mem_partition *part);
5248
5249/**
5250 * @brief Add a thread into a memory domain.
5251 *
5252 * Add a thread into a memory domain.
5253 *
5254 * @param domain The memory domain that the thread is going to be added into.
5255 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005256 *
5257 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005258 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005259extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5260 k_tid_t thread);
5261
5262/**
5263 * @brief Remove a thread from its memory domain.
5264 *
5265 * Remove a thread from its memory domain.
5266 *
5267 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005268 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005269 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005270extern void k_mem_domain_remove_thread(k_tid_t thread);
5271
Anas Nashif166f5192018-02-25 08:02:36 -06005272/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005273
Andrew Boie756f9072017-10-10 16:01:49 -07005274/**
5275 * @brief Emit a character buffer to the console device
5276 *
5277 * @param c String of characters to print
5278 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005279 *
5280 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005281 */
5282__syscall void k_str_out(char *c, size_t n);
5283
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005284/**
5285 * @brief Disable preservation of floating point context information.
5286 *
5287 * This routine informs the kernel that the specified thread
5288 * will no longer be using the floating point registers.
5289 *
5290 * @warning
5291 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005292 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005293 *
5294 * @warning
5295 * This routine should only be used to disable floating point support for
5296 * a thread that currently has such support enabled.
5297 *
5298 * @param thread ID of thread.
5299 *
5300 * @retval 0 On success.
5301 * @retval -ENOSYS If the floating point disabling is not implemented.
5302 * -EINVAL If the floating point disabling could not be performed.
5303 */
5304__syscall int k_float_disable(struct k_thread *thread);
5305
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005306#ifdef __cplusplus
5307}
5308#endif
5309
Anas Nashif10291a02019-06-25 12:25:12 -04005310#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005311#include <syscalls/kernel.h>
5312
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005313#endif /* !_ASMLANGUAGE */
5314
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005315#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */