blob: d13e93866a30d8eb3796c449e7dc8303535a9e99 [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>
James Harrisb1042812021-03-03 12:02:05 -080019#include <limits.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070020#include <stdbool.h>
Stephanos Ioannidis33fbe002019-09-09 21:26:59 +090021#include <toolchain.h>
Torbjörn Leksell16bbb8e2021-03-26 08:31:23 +010022#include <tracing/tracing_macros.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040023
Daniel Leungfd7a68d2020-10-14 12:17:12 -070024#ifdef CONFIG_THREAD_RUNTIME_STATS_USE_TIMING_FUNCTIONS
25#include <timing/timing.h>
26#endif
27
Benjamin Walsh456c6da2016-09-02 18:55:39 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31
Anas Nashifbbb157d2017-01-15 08:46:31 -050032/**
33 * @brief Kernel APIs
34 * @defgroup kernel_apis Kernel APIs
35 * @{
36 * @}
37 */
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
Anas Nashif2f203c22016-12-18 06:57:45 -050079#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -080080#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Kumar Galaa1b77fd2020-05-27 11:26:57 -050081#define _OBJECT_TRACING_LINKED_FLAG uint8_t __linked;
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +080082#define _OBJECT_TRACING_INIT \
83 .__next = NULL, \
84 .__linked = 0,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040085#else
Anas Nashif2f203c22016-12-18 06:57:45 -050086#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -080087#define _OBJECT_TRACING_NEXT_PTR(type)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +080088#define _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040089#endif
90
Benjamin Walshacc68c12017-01-29 18:57:45 -050091#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +030092#define _POLL_EVENT_OBJ_INIT(obj) \
93 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
94#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -050095#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +030096#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -050097#define _POLL_EVENT
98#endif
99
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500100struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101struct k_mutex;
102struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103struct k_msgq;
104struct k_mbox;
105struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200106struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107struct k_fifo;
108struct k_lifo;
109struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400110struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mem_pool;
112struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500113struct k_poll_event;
114struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800115struct k_mem_domain;
116struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800117struct k_futex;
Andrew Boiebca15da2017-10-15 14:17:48 -0700118
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119enum execution_context_types {
120 K_ISR = 0,
121 K_COOP_THREAD,
122 K_PREEMPT_THREAD,
123};
124
Anas Nashiffc1b5de2020-11-11 08:42:53 -0500125/* private, used by k_poll and k_work_poll */
126struct k_work_poll;
127typedef int (*_poller_cb_t)(struct k_poll_event *event, uint32_t state);
128
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400129/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500130 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100131 * @{
132 */
Anas Nashife71293e2019-12-04 20:00:14 -0500133
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530134typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
135 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100136
137/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530138 * @brief Iterate over all the threads in the system.
139 *
140 * This routine iterates over all the threads in the system and
141 * calls the user_cb function for each thread.
142 *
143 * @param user_cb Pointer to the user callback function.
144 * @param user_data Pointer to user data.
145 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300146 * @note @option{CONFIG_THREAD_MONITOR} must be set for this function
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100147 * to be effective.
148 * @note This API uses @ref k_spin_lock to protect the _kernel.threads
149 * list which means creation of new threads and terminations of existing
150 * threads are blocked until this API returns.
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530151 *
152 * @return N/A
153 */
154extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
155
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100156/**
157 * @brief Iterate over all the threads in the system without locking.
158 *
159 * This routine works exactly the same like @ref k_thread_foreach
160 * but unlocks interrupts when user_cb is executed.
161 *
162 * @param user_cb Pointer to the user callback function.
163 * @param user_data Pointer to user data.
164 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300165 * @note @option{CONFIG_THREAD_MONITOR} must be set for this function
Radoslaw Koppel2c529ce2019-11-27 14:20:37 +0100166 * to be effective.
167 * @note This API uses @ref k_spin_lock only when accessing the _kernel.threads
168 * queue elements. It unlocks it during user callback function processing.
169 * If a new task is created when this @c foreach function is in progress,
170 * the added new task would not be included in the enumeration.
171 * If a task is aborted during this enumeration, there would be a race here
172 * and there is a possibility that this aborted task would be included in the
173 * enumeration.
174 * @note If the task is aborted and the memory occupied by its @c k_thread
175 * structure is reused when this @c k_thread_foreach_unlocked is in progress
176 * it might even lead to the system behave unstable.
177 * This function may never return, as it would follow some @c next task
178 * pointers treating given pointer as a pointer to the k_thread structure
179 * while it is something different right now.
180 * Do not reuse the memory that was occupied by k_thread structure of aborted
181 * task if it was aborted after this function was called in any context.
182 */
183extern void k_thread_foreach_unlocked(
184 k_thread_user_cb_t user_cb, void *user_data);
185
Anas Nashif166f5192018-02-25 08:02:36 -0600186/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100187
188/**
Allan Stephensc98da842016-11-11 15:45:03 -0500189 * @defgroup thread_apis Thread APIs
190 * @ingroup kernel_apis
191 * @{
192 */
193
Benjamin Walshed240f22017-01-22 13:05:08 -0500194#endif /* !_ASMLANGUAGE */
195
196
197/*
198 * Thread user options. May be needed by assembly code. Common part uses low
199 * bits, arch-specific use high bits.
200 */
201
Anas Nashifa541e932018-05-24 11:19:16 -0500202/**
203 * @brief system thread that must not abort
Anas Nashifa541e932018-05-24 11:19:16 -0500204 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700205#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500206
Stephanos Ioannidisaaf93202020-05-03 18:03:19 +0900207#if defined(CONFIG_FPU_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500208/**
Katsuhiro Suzukifadef432020-12-16 11:22:13 +0900209 * @brief FPU registers are managed by context switch
210 *
211 * @details
212 * This option indicates that the thread uses the CPU's floating point
213 * registers. This instructs the kernel to take additional steps to save
214 * and restore the contents of these registers when scheduling the thread.
215 * No effect if @option{CONFIG_FPU_SHARING} is not enabled.
Anas Nashifa541e932018-05-24 11:19:16 -0500216 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700217#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500218#endif
219
Anas Nashifa541e932018-05-24 11:19:16 -0500220/**
221 * @brief user mode thread
222 *
223 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700224 * has additional restrictions
225 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700226#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700227
Anas Nashifa541e932018-05-24 11:19:16 -0500228/**
229 * @brief Inherit Permissions
230 *
231 * @details
232 * Indicates that the thread being created should inherit all kernel object
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300233 * permissions from the thread that created it. No effect if
234 * @option{CONFIG_USERSPACE} is not enabled.
Andrew Boie47f8fd12017-10-05 11:11:02 -0700235 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700236#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700237
Andy Ross9a594a02021-02-10 14:54:21 -0800238/**
239 * @brief Callback item state
240 *
241 * @details
242 * This is a single bit of state reserved for "callback manager"
243 * utilities (p4wq initially) who need to track operations invoked
244 * from within a user-provided callback they have been invoked.
245 * Effectively it serves as a tiny bit of zero-overhead TLS data.
246 */
247#define K_CALLBACK_STATE (BIT(4))
248
Benjamin Walshed240f22017-01-22 13:05:08 -0500249#ifdef CONFIG_X86
250/* x86 Bitmask definitions for threads user options */
251
Daniel Leungce440482021-01-07 15:07:29 -0800252#if defined(CONFIG_FPU_SHARING) && defined(CONFIG_X86_SSE)
Benjamin Walshed240f22017-01-22 13:05:08 -0500253/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700254#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500255#endif
256#endif
257
258/* end - thread options */
259
260#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400261/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700262 * @brief Create a thread.
263 *
264 * This routine initializes a thread, then schedules it for execution.
265 *
266 * The new thread may be scheduled for immediate execution or a delayed start.
267 * If the newly spawned thread does not have a delayed start the kernel
268 * scheduler may preempt the current thread to allow the new thread to
269 * execute.
270 *
271 * Thread options are architecture-specific, and can include K_ESSENTIAL,
272 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
273 * them using "|" (the logical OR operator).
274 *
Andrew Boie8ce260d2020-04-24 16:24:46 -0700275 * Stack objects passed to this function must be originally defined with
276 * either of these macros in order to be portable:
277 *
278 * - K_THREAD_STACK_DEFINE() - For stacks that may support either user or
279 * supervisor threads.
280 * - K_KERNEL_STACK_DEFINE() - For stacks that may support supervisor
281 * threads only. These stacks use less memory if CONFIG_USERSPACE is
282 * enabled.
283 *
284 * The stack_size parameter has constraints. It must either be:
285 *
286 * - The original size value passed to K_THREAD_STACK_DEFINE() or
287 * K_KERNEL_STACK_DEFINE()
288 * - The return value of K_THREAD_STACK_SIZEOF(stack) if the stack was
289 * defined with K_THREAD_STACK_DEFINE()
290 * - The return value of K_KERNEL_STACK_SIZEOF(stack) if the stack was
291 * defined with K_KERNEL_STACK_DEFINE().
292 *
293 * Using other values, or sizeof(stack) may produce undefined behavior.
Andrew Boied26cf2d2017-03-30 13:07:02 -0700294 *
295 * @param new_thread Pointer to uninitialized struct k_thread
296 * @param stack Pointer to the stack space.
297 * @param stack_size Stack size in bytes.
298 * @param entry Thread entry function.
299 * @param p1 1st entry point parameter.
300 * @param p2 2nd entry point parameter.
301 * @param p3 3rd entry point parameter.
302 * @param prio Thread priority.
303 * @param options Thread options.
Andy Ross78327382020-03-05 15:18:14 -0800304 * @param delay Scheduling delay, or K_NO_WAIT (for no delay).
Andrew Boied26cf2d2017-03-30 13:07:02 -0700305 *
306 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400307 *
Andrew Boied26cf2d2017-03-30 13:07:02 -0700308 */
Andrew Boie662c3452017-10-02 10:51:18 -0700309__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700310 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700311 size_t stack_size,
312 k_thread_entry_t entry,
313 void *p1, void *p2, void *p3,
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500314 int prio, uint32_t options, k_timeout_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700315
Andrew Boie3f091b52017-08-30 14:34:14 -0700316/**
317 * @brief Drop a thread's privileges permanently to user mode
318 *
Andrew Boie4d6bc472020-10-24 13:11:35 -0700319 * This allows a supervisor thread to be re-used as a user thread.
320 * This function does not return, but control will transfer to the provided
321 * entry point as if this was a new user thread.
322 *
323 * The implementation ensures that the stack buffer contents are erased.
324 * Any thread-local storage will be reverted to a pristine state.
325 *
326 * Memory domain membership, resource pool assignment, kernel object
327 * permissions, priority, and thread options are preserved.
328 *
329 * A common use of this function is to re-use the main thread as a user thread
330 * once all supervisor mode-only tasks have been completed.
331 *
Andrew Boie3f091b52017-08-30 14:34:14 -0700332 * @param entry Function to start executing from
333 * @param p1 1st entry point parameter
334 * @param p2 2nd entry point parameter
335 * @param p3 3rd entry point parameter
336 */
337extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
338 void *p1, void *p2,
339 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700340
Andrew Boied26cf2d2017-03-30 13:07:02 -0700341/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530342 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700343 *
344 * This is a convenience function. For the provided thread, grant access to
345 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700346 *
347 * The thread object must be initialized (i.e. running). The objects don't
348 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530349 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700350 *
351 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530352 * @param ... list of kernel object pointers
Andrew Boiee12857a2017-10-17 11:38:26 -0700353 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530354#define k_thread_access_grant(thread, ...) \
Krzysztof Chruscinski1b4b9382020-05-08 07:06:58 +0200355 FOR_EACH_FIXED_ARG(k_object_access_grant, (;), thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700356
357/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700358 * @brief Assign a resource memory pool to a thread
359 *
360 * By default, threads have no resource pool assigned unless their parent
361 * thread has a resource pool, in which case it is inherited. Multiple
362 * threads may be assigned to the same memory pool.
363 *
364 * Changing a thread's resource pool will not migrate allocations from the
365 * previous pool.
366 *
Jukka Rissanenfdf18482020-05-01 12:37:51 +0300367 * @param thread Target thread to assign a memory pool for resource requests.
Andy Rossc770cab2020-10-02 08:22:03 -0700368 * @param heap Heap object to use for resources,
Jukka Rissanenfdf18482020-05-01 12:37:51 +0300369 * or NULL if the thread should no longer have a memory pool.
Andrew Boie92e5bd72018-04-12 17:12:15 -0700370 */
Andy Rossc770cab2020-10-02 08:22:03 -0700371static inline void k_thread_heap_assign(struct k_thread *thread,
372 struct k_heap *heap)
373{
374 thread->resource_pool = heap;
375}
376
Andrew Boieefc5fe02020-02-05 10:41:58 -0800377#if defined(CONFIG_INIT_STACKS) && defined(CONFIG_THREAD_STACK_INFO)
378/**
379 * @brief Obtain stack usage information for the specified thread
380 *
381 * User threads will need to have permission on the target thread object.
382 *
383 * Some hardware may prevent inspection of a stack buffer currently in use.
384 * If this API is called from supervisor mode, on the currently running thread,
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300385 * on a platform which selects @option{CONFIG_NO_UNUSED_STACK_INSPECTION}, an
386 * error will be generated.
Andrew Boieefc5fe02020-02-05 10:41:58 -0800387 *
388 * @param thread Thread to inspect stack information
389 * @param unused_ptr Output parameter, filled in with the unused stack space
390 * of the target thread in bytes.
391 * @return 0 on success
392 * @return -EBADF Bad thread object (user mode only)
393 * @return -EPERM No permissions on thread object (user mode only)
394 * #return -ENOTSUP Forbidden by hardware policy
395 * @return -EINVAL Thread is uninitialized or exited (user mode only)
396 * @return -EFAULT Bad memory address for unused_ptr (user mode only)
397 */
398__syscall int k_thread_stack_space_get(const struct k_thread *thread,
399 size_t *unused_ptr);
400#endif
401
Andrew Boie92e5bd72018-04-12 17:12:15 -0700402#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
403/**
404 * @brief Assign the system heap as a thread's resource pool
405 *
Yasushi SHOJIa3e0f8c2021-03-11 20:45:20 +0900406 * Similar to z_thread_heap_assign(), but the thread will use
Andrew Boie92e5bd72018-04-12 17:12:15 -0700407 * the kernel heap to draw memory.
408 *
409 * Use with caution, as a malicious thread could perform DoS attacks on the
410 * kernel heap.
411 *
412 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400413 *
Andrew Boie92e5bd72018-04-12 17:12:15 -0700414 */
415void k_thread_system_pool_assign(struct k_thread *thread);
416#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
417
418/**
Andrew Boie322816e2020-02-20 16:33:06 -0800419 * @brief Sleep until a thread exits
420 *
421 * The caller will be put to sleep until the target thread exits, either due
422 * to being aborted, self-exiting, or taking a fatal error. This API returns
423 * immediately if the thread isn't running.
424 *
Andy Ross23f699b2021-02-23 06:12:17 -0800425 * This API may only be called from ISRs with a K_NO_WAIT timeout,
426 * where it can be useful as a predicate to detect when a thread has
427 * aborted.
Andrew Boie322816e2020-02-20 16:33:06 -0800428 *
429 * @param thread Thread to wait to exit
Andy Ross78327382020-03-05 15:18:14 -0800430 * @param timeout upper bound time to wait for the thread to exit.
Andrew Boie322816e2020-02-20 16:33:06 -0800431 * @retval 0 success, target thread has exited or wasn't running
432 * @retval -EBUSY returned without waiting
433 * @retval -EAGAIN waiting period timed out
434 * @retval -EDEADLK target thread is joining on the caller, or target thread
435 * is the caller
436 */
Andy Ross78327382020-03-05 15:18:14 -0800437__syscall int k_thread_join(struct k_thread *thread, k_timeout_t timeout);
438
439/**
440 * @brief Put the current thread to sleep.
441 *
442 * This routine puts the current thread to sleep for @a duration,
443 * specified as a k_timeout_t object.
444 *
Anas Nashifd2c71792020-10-17 07:52:17 -0400445 * @note if @a timeout is set to K_FOREVER then the thread is suspended.
446 *
Andy Ross78327382020-03-05 15:18:14 -0800447 * @param timeout Desired duration of sleep.
448 *
449 * @return Zero if the requested time has elapsed or the number of milliseconds
450 * left to sleep, if thread was woken up by \ref k_wakeup call.
451 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500452__syscall int32_t k_sleep(k_timeout_t timeout);
Andrew Boie322816e2020-02-20 16:33:06 -0800453
454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500455 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400456 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700457 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400458 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700459 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400460 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200461 * @return Zero if the requested time has elapsed or the number of milliseconds
462 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400463 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500464static inline int32_t k_msleep(int32_t ms)
Andy Ross78327382020-03-05 15:18:14 -0800465{
466 return k_sleep(Z_TIMEOUT_MS(ms));
467}
Charles E. Yousea5678312019-05-09 16:46:46 -0700468
469/**
470 * @brief Put the current thread to sleep with microsecond resolution.
471 *
472 * This function is unlikely to work as expected without kernel tuning.
473 * In particular, because the lower bound on the duration of a sleep is
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300474 * the duration of a tick, @option{CONFIG_SYS_CLOCK_TICKS_PER_SEC} must be
475 * adjusted to achieve the resolution desired. The implications of doing
476 * this must be understood before attempting to use k_usleep(). Use with
477 * caution.
Charles E. Yousea5678312019-05-09 16:46:46 -0700478 *
479 * @param us Number of microseconds to sleep.
480 *
481 * @return Zero if the requested time has elapsed or the number of microseconds
482 * left to sleep, if thread was woken up by \ref k_wakeup call.
483 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500484__syscall int32_t k_usleep(int32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485
486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500487 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400488 *
489 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500490 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400491 *
Peter Bigot6a794362020-05-22 14:17:01 -0500492 * @note The clock used for the microsecond-resolution delay here may
493 * be skewed relative to the clock used for system timeouts like
494 * k_sleep(). For example k_busy_wait(1000) may take slightly more or
495 * less time than k_sleep(K_MSEC(1)), with the offset dependent on
496 * clock tolerances.
497 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400498 * @return N/A
499 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500500__syscall void k_busy_wait(uint32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400501
502/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500503 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500505 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400506 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500507 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400508 *
509 * @return N/A
510 */
Andrew Boie468190a2017-09-29 14:00:48 -0700511__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400512
513/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500514 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500516 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500518 * If @a thread is not currently sleeping, the routine has no effect.
519 *
520 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400521 *
522 * @return N/A
523 */
Andrew Boie468190a2017-09-29 14:00:48 -0700524__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400525
526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500527 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500529 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400530 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400531 */
Nicolas Pitre0dc7b9e2021-04-09 00:40:41 -0400532__syscall k_tid_t k_current_get(void) __attribute_const__;
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400533
534/**
Allan Stephensc98da842016-11-11 15:45:03 -0500535 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500537 * This routine permanently stops execution of @a thread. The thread is taken
538 * off all kernel queues it is part of (i.e. the ready queue, the timeout
539 * queue, or a kernel object wait queue). However, any kernel resources the
540 * thread might currently own (such as mutexes or memory blocks) are not
541 * released. It is the responsibility of the caller of this routine to ensure
542 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400543 *
Andy Ross23f699b2021-02-23 06:12:17 -0800544 * After k_thread_abort() returns, the thread is guaranteed not to be
545 * running or to become runnable anywhere on the system. Normally
546 * this is done via blocking the caller (in the same manner as
547 * k_thread_join()), but in interrupt context on SMP systems the
548 * implementation is required to spin for threads that are running on
549 * other CPUs. Note that as specified, this means that on SMP
550 * platforms it is possible for application code to create a deadlock
551 * condition by simultaneously aborting a cycle of threads using at
552 * least one termination from interrupt context. Zephyr cannot detect
553 * all such conditions.
554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500555 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400556 *
557 * @return N/A
558 */
Andrew Boie468190a2017-09-29 14:00:48 -0700559__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400560
Andrew Boie7d627c52017-08-30 11:01:56 -0700561
562/**
563 * @brief Start an inactive thread
564 *
565 * If a thread was created with K_FOREVER in the delay parameter, it will
566 * not be added to the scheduling queue until this function is called
567 * on it.
568 *
569 * @param thread thread to start
570 */
Andrew Boie468190a2017-09-29 14:00:48 -0700571__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700572
Peter A. Bigot16a40812020-09-18 16:24:57 -0500573extern k_ticks_t z_timeout_expires(const struct _timeout *timeout);
574extern k_ticks_t z_timeout_remaining(const struct _timeout *timeout);
Andy Ross5a5d3da2020-03-09 13:59:15 -0700575
576#ifdef CONFIG_SYS_CLOCK_EXISTS
577
578/**
Andy Rosse39bf292020-03-19 10:30:33 -0700579 * @brief Get time when a thread wakes up, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -0700580 *
581 * This routine computes the system uptime when a waiting thread next
582 * executes, in units of system ticks. If the thread is not waiting,
583 * it returns current system time.
584 */
Peter Bigot0ab314f2020-11-16 15:28:59 -0600585__syscall k_ticks_t k_thread_timeout_expires_ticks(const struct k_thread *t);
Andy Ross5a5d3da2020-03-09 13:59:15 -0700586
587static inline k_ticks_t z_impl_k_thread_timeout_expires_ticks(
Peter Bigot0ab314f2020-11-16 15:28:59 -0600588 const struct k_thread *t)
Andy Ross5a5d3da2020-03-09 13:59:15 -0700589{
590 return z_timeout_expires(&t->base.timeout);
591}
592
593/**
Andy Rosse39bf292020-03-19 10:30:33 -0700594 * @brief Get time remaining before a thread wakes up, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -0700595 *
596 * This routine computes the time remaining before a waiting thread
597 * next executes, in units of system ticks. If the thread is not
598 * waiting, it returns zero.
599 */
Peter Bigot0ab314f2020-11-16 15:28:59 -0600600__syscall k_ticks_t k_thread_timeout_remaining_ticks(const struct k_thread *t);
Andy Ross5a5d3da2020-03-09 13:59:15 -0700601
602static inline k_ticks_t z_impl_k_thread_timeout_remaining_ticks(
Peter Bigot0ab314f2020-11-16 15:28:59 -0600603 const struct k_thread *t)
Andy Ross5a5d3da2020-03-09 13:59:15 -0700604{
605 return z_timeout_remaining(&t->base.timeout);
606}
607
608#endif /* CONFIG_SYS_CLOCK_EXISTS */
609
Allan Stephensc98da842016-11-11 15:45:03 -0500610/**
611 * @cond INTERNAL_HIDDEN
612 */
613
Benjamin Walshd211a522016-12-06 11:44:01 -0500614/* timeout has timed out and is not on _timeout_q anymore */
615#define _EXPIRED (-2)
616
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400617struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700618 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700619 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400620 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700621 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500622 void *init_p1;
623 void *init_p2;
624 void *init_p3;
625 int init_prio;
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500626 uint32_t init_options;
627 int32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500628 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600629 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400630};
631
Anas Nashif45a1d8a2020-04-24 11:29:17 -0400632#define Z_THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400633 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600634 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500635 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700636 .init_thread = (thread), \
637 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500638 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700639 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400640 .init_p1 = (void *)p1, \
641 .init_p2 = (void *)p2, \
642 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500643 .init_prio = (prio), \
644 .init_options = (options), \
645 .init_delay = (delay), \
646 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600647 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400648 }
649
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400650/**
Allan Stephensc98da842016-11-11 15:45:03 -0500651 * INTERNAL_HIDDEN @endcond
652 */
653
654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500655 * @brief Statically define and initialize a thread.
656 *
657 * The thread may be scheduled for immediate execution or a delayed start.
658 *
659 * Thread options are architecture-specific, and can include K_ESSENTIAL,
660 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
661 * them using "|" (the logical OR operator).
662 *
663 * The ID of the thread can be accessed using:
664 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500665 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500666 *
667 * @param name Name of the thread.
668 * @param stack_size Stack size in bytes.
669 * @param entry Thread entry function.
670 * @param p1 1st entry point parameter.
671 * @param p2 2nd entry point parameter.
672 * @param p3 3rd entry point parameter.
673 * @param prio Thread priority.
674 * @param options Thread options.
Peter Bigot73c387c2020-04-20 08:55:20 -0500675 * @param delay Scheduling delay (in milliseconds), zero for no delay.
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400676 *
Anas Nashif47420d02018-05-24 14:20:56 -0400677 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400678 * @internal It has been observed that the x86 compiler by default aligns
679 * these _static_thread_data structures to 32-byte boundaries, thereby
680 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400681 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400682 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500683#define K_THREAD_DEFINE(name, stack_size, \
684 entry, p1, p2, p3, \
685 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700686 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400687 struct k_thread _k_thread_obj_##name; \
688 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Anas Nashif45a1d8a2020-04-24 11:29:17 -0400689 Z_THREAD_INITIALIZER(&_k_thread_obj_##name, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700690 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500691 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600692 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700693 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400694
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500696 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500698 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500700 * @param thread ID of thread whose priority is needed.
701 *
702 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400703 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700704__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400705
706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500707 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500709 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400710 *
711 * Rescheduling can occur immediately depending on the priority @a thread is
712 * set to:
713 *
714 * - If its priority is raised above the priority of the caller of this
715 * function, and the caller is preemptible, @a thread will be scheduled in.
716 *
717 * - If the caller operates on itself, it lowers its priority below that of
718 * other threads in the system, and the caller is preemptible, the thread of
719 * highest priority will be scheduled in.
720 *
721 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
722 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
723 * highest priority.
724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500725 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400726 * @param prio New priority.
727 *
728 * @warning Changing the priority of a thread currently involved in mutex
729 * priority inheritance may result in undefined behavior.
730 *
731 * @return N/A
732 */
Andrew Boie468190a2017-09-29 14:00:48 -0700733__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400734
Andy Ross4a2e50f2018-05-15 11:06:25 -0700735
736#ifdef CONFIG_SCHED_DEADLINE
737/**
738 * @brief Set deadline expiration time for scheduler
739 *
740 * This sets the "deadline" expiration as a time delta from the
741 * current time, in the same units used by k_cycle_get_32(). The
742 * scheduler (when deadline scheduling is enabled) will choose the
743 * next expiring thread when selecting between threads at the same
744 * static priority. Threads at different priorities will be scheduled
745 * according to their static priority.
746 *
Andy Rossef626572020-07-10 09:43:36 -0700747 * @note Deadlines are stored internally using 32 bit unsigned
748 * integers. The number of cycles between the "first" deadline in the
749 * scheduler queue and the "last" deadline must be less than 2^31 (i.e
750 * a signed non-negative quantity). Failure to adhere to this rule
751 * may result in scheduled threads running in an incorrect dealine
752 * order.
Andy Ross4a2e50f2018-05-15 11:06:25 -0700753 *
754 * @note Despite the API naming, the scheduler makes no guarantees the
755 * the thread WILL be scheduled within that deadline, nor does it take
756 * extra metadata (like e.g. the "runtime" and "period" parameters in
757 * Linux sched_setattr()) that allows the kernel to validate the
758 * scheduling for achievability. Such features could be implemented
759 * above this call, which is simply input to the priority selection
760 * logic.
761 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300762 * @note You should enable @option{CONFIG_SCHED_DEADLINE} in your project
763 * configuration.
Anas Nashif240c5162019-06-10 12:25:50 -0400764 *
Andy Ross4a2e50f2018-05-15 11:06:25 -0700765 * @param thread A thread on which to set the deadline
766 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -0400767 *
Andy Ross4a2e50f2018-05-15 11:06:25 -0700768 */
769__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
770#endif
771
Andy Rossab46b1b2019-01-30 15:00:42 -0800772#ifdef CONFIG_SCHED_CPU_MASK
773/**
774 * @brief Sets all CPU enable masks to zero
775 *
776 * After this returns, the thread will no longer be schedulable on any
777 * CPUs. The thread must not be currently runnable.
778 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300779 * @note You should enable @option{CONFIG_SCHED_DEADLINE} in your project
780 * configuration.
Anas Nashif240c5162019-06-10 12:25:50 -0400781 *
Andy Rossab46b1b2019-01-30 15:00:42 -0800782 * @param thread Thread to operate upon
783 * @return Zero on success, otherwise error code
784 */
785int k_thread_cpu_mask_clear(k_tid_t thread);
786
787/**
788 * @brief Sets all CPU enable masks to one
789 *
790 * After this returns, the thread will be schedulable on any CPU. The
791 * thread must not be currently runnable.
792 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300793 * @note You should enable @option{CONFIG_SCHED_DEADLINE} in your project
794 * configuration.
Anas Nashif240c5162019-06-10 12:25:50 -0400795 *
Andy Rossab46b1b2019-01-30 15:00:42 -0800796 * @param thread Thread to operate upon
797 * @return Zero on success, otherwise error code
798 */
799int k_thread_cpu_mask_enable_all(k_tid_t thread);
800
801/**
802 * @brief Enable thread to run on specified CPU
803 *
804 * The thread must not be currently runnable.
805 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300806 * @note You should enable @option{CONFIG_SCHED_DEADLINE} in your project
807 * configuration.
Anas Nashif240c5162019-06-10 12:25:50 -0400808 *
Andy Rossab46b1b2019-01-30 15:00:42 -0800809 * @param thread Thread to operate upon
810 * @param cpu CPU index
811 * @return Zero on success, otherwise error code
812 */
813int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
814
815/**
816 * @brief Prevent thread to run on specified CPU
817 *
818 * The thread must not be currently runnable.
819 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -0300820 * @note You should enable @option{CONFIG_SCHED_DEADLINE} in your project
821 * configuration.
Anas Nashif240c5162019-06-10 12:25:50 -0400822 *
Andy Rossab46b1b2019-01-30 15:00:42 -0800823 * @param thread Thread to operate upon
824 * @param cpu CPU index
825 * @return Zero on success, otherwise error code
826 */
827int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
828#endif
829
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Andy Ross50d09422019-11-19 11:20:07 -0800833 * This routine prevents the kernel scheduler from making @a thread
834 * the current thread. All other internal operations on @a thread are
835 * still performed; for example, kernel objects it is waiting on are
836 * still handed to it. Note that any existing timeouts
837 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
838 * will be canceled. On resume, the thread will begin running
839 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * If @a thread is already suspended, the routine has no effect.
842 *
843 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
845 * @return N/A
846 */
Andrew Boie468190a2017-09-29 14:00:48 -0700847__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848
849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * This routine allows the kernel scheduler to make @a thread the current
853 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * If @a thread is not currently suspended, the routine has no effect.
856 *
857 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
859 * @return N/A
860 */
Andrew Boie468190a2017-09-29 14:00:48 -0700861__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400862
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * This routine specifies how the scheduler will perform time slicing of
867 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * To enable time slicing, @a slice must be non-zero. The scheduler
870 * ensures that no thread runs for more than the specified time limit
871 * before other threads of that priority are given a chance to execute.
872 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700873 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 * execute. Once the scheduler selects a thread for execution, there is no
877 * minimum guaranteed time the thread will execute before threads of greater or
878 * equal priority are scheduled.
879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 * for execution, this routine has no effect; the thread is immediately
882 * rescheduled after the slice period expires.
883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * To disable timeslicing, set both @a slice and @a prio to zero.
885 *
886 * @param slice Maximum time slice length (in milliseconds).
887 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
889 * @return N/A
890 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500891extern void k_sched_time_slice_set(int32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400892
Anas Nashif166f5192018-02-25 08:02:36 -0600893/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -0500894
895/**
896 * @addtogroup isr_apis
897 * @{
898 */
899
900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500901 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400902 *
Allan Stephensc98da842016-11-11 15:45:03 -0500903 * This routine allows the caller to customize its actions, depending on
904 * whether it is a thread or an ISR.
905 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +0100906 * @funcprops \isr_ok
Allan Stephensc98da842016-11-11 15:45:03 -0500907 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -0800908 * @return false if invoked by a thread.
909 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -0800911extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912
Benjamin Walsh445830d2016-11-10 15:54:27 -0500913/**
914 * @brief Determine if code is running in a preemptible thread.
915 *
Allan Stephensc98da842016-11-11 15:45:03 -0500916 * This routine allows the caller to customize its actions, depending on
917 * whether it can be preempted by another thread. The routine returns a 'true'
918 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500919 *
Allan Stephensc98da842016-11-11 15:45:03 -0500920 * - The code is running in a thread, not at ISR.
921 * - The thread's priority is in the preemptible range.
922 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500923 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +0100924 * @funcprops \isr_ok
Allan Stephensc98da842016-11-11 15:45:03 -0500925 *
926 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500927 * @return Non-zero if invoked by a preemptible thread.
928 */
Andrew Boie468190a2017-09-29 14:00:48 -0700929__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500930
Allan Stephensc98da842016-11-11 15:45:03 -0500931/**
Peter Bigot74ef3952019-12-23 11:48:43 -0600932 * @brief Test whether startup is in the before-main-task phase.
933 *
934 * This routine allows the caller to customize its actions, depending on
935 * whether it being invoked before the kernel is fully active.
936 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +0100937 * @funcprops \isr_ok
Peter Bigot74ef3952019-12-23 11:48:43 -0600938 *
939 * @return true if invoked before post-kernel initialization
940 * @return false if invoked during/after post-kernel initialization
941 */
942static inline bool k_is_pre_kernel(void)
943{
944 extern bool z_sys_post_kernel; /* in init.c */
945
946 return !z_sys_post_kernel;
947}
948
949/**
Anas Nashif166f5192018-02-25 08:02:36 -0600950 * @}
Allan Stephensc98da842016-11-11 15:45:03 -0500951 */
952
953/**
954 * @addtogroup thread_apis
955 * @{
956 */
957
958/**
959 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500960 *
Allan Stephensc98da842016-11-11 15:45:03 -0500961 * This routine prevents the current thread from being preempted by another
962 * thread by instructing the scheduler to treat it as a cooperative thread.
963 * If the thread subsequently performs an operation that makes it unready,
964 * it will be context switched out in the normal manner. When the thread
965 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500966 *
Allan Stephensc98da842016-11-11 15:45:03 -0500967 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500968 *
Allan Stephensc98da842016-11-11 15:45:03 -0500969 * @note k_sched_lock() and k_sched_unlock() should normally be used
970 * when the operation being performed can be safely interrupted by ISRs.
971 * However, if the amount of processing involved is very small, better
972 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500973 *
974 * @return N/A
975 */
976extern void k_sched_lock(void);
977
Allan Stephensc98da842016-11-11 15:45:03 -0500978/**
979 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500980 *
Allan Stephensc98da842016-11-11 15:45:03 -0500981 * This routine reverses the effect of a previous call to k_sched_lock().
982 * A thread must call the routine once for each time it called k_sched_lock()
983 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500984 *
985 * @return N/A
986 */
987extern void k_sched_unlock(void);
988
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * Custom data is not used by the kernel itself, and is freely available
995 * for a thread to use as it sees fit. It can be used as a framework
996 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
1000 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001001 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002 */
Andrew Boie468190a2017-09-29 14:00:48 -07001003__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004
1005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001011 */
Andrew Boie468190a2017-09-29 14:00:48 -07001012__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001013
1014/**
Anas Nashif57554052018-03-03 02:31:05 -06001015 * @brief Set current thread name
1016 *
Fabio Utzig39fa56b2020-09-11 10:14:37 -03001017 * Set the name of the thread to be used when @option{CONFIG_THREAD_MONITOR}
1018 * is enabled for tracing and debugging.
Anas Nashif57554052018-03-03 02:31:05 -06001019 *
Anas Nashif25c87db2021-03-29 10:54:23 -04001020 * @param thread Thread to set name, or NULL to set the current thread
1021 * @param str Name string
Andrew Boie38129ce2019-06-25 08:54:37 -07001022 * @retval 0 on success
1023 * @retval -EFAULT Memory access error with supplied string
1024 * @retval -ENOSYS Thread name configuration option not enabled
1025 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001026 */
Anas Nashif25c87db2021-03-29 10:54:23 -04001027__syscall int k_thread_name_set(k_tid_t thread, const char *str);
Anas Nashif57554052018-03-03 02:31:05 -06001028
1029/**
1030 * @brief Get thread name
1031 *
1032 * Get the name of a thread
1033 *
Anas Nashif25c87db2021-03-29 10:54:23 -04001034 * @param thread Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001035 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001036 */
Anas Nashif25c87db2021-03-29 10:54:23 -04001037const char *k_thread_name_get(k_tid_t thread);
Andrew Boie38129ce2019-06-25 08:54:37 -07001038
1039/**
1040 * @brief Copy the thread name into a supplied buffer
1041 *
Anas Nashif25c87db2021-03-29 10:54:23 -04001042 * @param thread Thread to obtain name information
Andrew Boie38129ce2019-06-25 08:54:37 -07001043 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001044 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001045 * @retval -ENOSPC Destination buffer too small
1046 * @retval -EFAULT Memory access error
1047 * @retval -ENOSYS Thread name feature not enabled
1048 * @retval 0 Success
1049 */
Anas Nashif25c87db2021-03-29 10:54:23 -04001050__syscall int k_thread_name_copy(k_tid_t thread, char *buf,
Andrew Boie38129ce2019-06-25 08:54:37 -07001051 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001052
1053/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001054 * @brief Get thread state string
1055 *
1056 * Get the human friendly thread state string
1057 *
1058 * @param thread_id Thread ID
1059 * @retval Thread state string, empty if no state flag is set
1060 */
1061const char *k_thread_state_str(k_tid_t thread_id);
1062
1063/**
Andy Rosscfe62032018-09-29 07:34:55 -07001064 * @}
1065 */
1066
1067/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001068 * @addtogroup clock_apis
1069 * @{
1070 */
1071
1072/**
1073 * @brief Generate null timeout delay.
1074 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001075 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001076 * not to wait if the requested operation cannot be performed immediately.
1077 *
1078 * @return Timeout delay value.
1079 */
Andy Ross78327382020-03-05 15:18:14 -08001080#define K_NO_WAIT Z_TIMEOUT_NO_WAIT
Allan Stephensc2f15a42016-11-17 12:24:22 -05001081
1082/**
Andy Rosse1bc5952020-03-09 12:19:54 -07001083 * @brief Generate timeout delay from nanoseconds.
1084 *
1085 * This macro generates a timeout delay that instructs a kernel API to
1086 * wait up to @a t nanoseconds to perform the requested operation.
1087 * Note that timer precision is limited to the tick rate, not the
1088 * requested value.
1089 *
Andy Rosse39bf292020-03-19 10:30:33 -07001090 * @param t Duration in nanoseconds.
Andy Rosse1bc5952020-03-09 12:19:54 -07001091 *
1092 * @return Timeout delay value.
1093 */
1094#define K_NSEC(t) Z_TIMEOUT_NS(t)
1095
1096/**
1097 * @brief Generate timeout delay from microseconds.
1098 *
1099 * This macro generates a timeout delay that instructs a kernel API
1100 * to wait up to @a t microseconds to perform the requested operation.
1101 * Note that timer precision is limited to the tick rate, not the
1102 * requested value.
1103 *
Andy Rosse39bf292020-03-19 10:30:33 -07001104 * @param t Duration in microseconds.
Andy Rosse1bc5952020-03-09 12:19:54 -07001105 *
1106 * @return Timeout delay value.
1107 */
1108#define K_USEC(t) Z_TIMEOUT_US(t)
1109
1110/**
1111 * @brief Generate timeout delay from cycles.
1112 *
1113 * This macro generates a timeout delay that instructs a kernel API
1114 * to wait up to @a t cycles to perform the requested operation.
1115 *
Andy Rosse39bf292020-03-19 10:30:33 -07001116 * @param t Duration in cycles.
Andy Rosse1bc5952020-03-09 12:19:54 -07001117 *
1118 * @return Timeout delay value.
1119 */
1120#define K_CYC(t) Z_TIMEOUT_CYC(t)
1121
1122/**
1123 * @brief Generate timeout delay from system ticks.
1124 *
1125 * This macro generates a timeout delay that instructs a kernel API
1126 * to wait up to @a t ticks to perform the requested operation.
1127 *
Andy Rosse39bf292020-03-19 10:30:33 -07001128 * @param t Duration in system ticks.
Andy Rosse1bc5952020-03-09 12:19:54 -07001129 *
1130 * @return Timeout delay value.
1131 */
1132#define K_TICKS(t) Z_TIMEOUT_TICKS(t)
1133
1134/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001135 * @brief Generate timeout delay from milliseconds.
1136 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001137 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001138 * to wait up to @a ms milliseconds to perform the requested operation.
1139 *
1140 * @param ms Duration in milliseconds.
1141 *
1142 * @return Timeout delay value.
1143 */
Andy Ross78327382020-03-05 15:18:14 -08001144#define K_MSEC(ms) Z_TIMEOUT_MS(ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001145
1146/**
1147 * @brief Generate timeout delay from seconds.
1148 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001149 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001150 * to wait up to @a s seconds to perform the requested operation.
1151 *
1152 * @param s Duration in seconds.
1153 *
1154 * @return Timeout delay value.
1155 */
Johan Hedberg14471692016-11-13 10:52:15 +02001156#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001157
1158/**
1159 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001160
1161 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001162 * to wait up to @a m minutes to perform the requested operation.
1163 *
1164 * @param m Duration in minutes.
1165 *
1166 * @return Timeout delay value.
1167 */
Johan Hedberg14471692016-11-13 10:52:15 +02001168#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001169
1170/**
1171 * @brief Generate timeout delay from hours.
1172 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001173 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001174 * to wait up to @a h hours to perform the requested operation.
1175 *
1176 * @param h Duration in hours.
1177 *
1178 * @return Timeout delay value.
1179 */
Johan Hedberg14471692016-11-13 10:52:15 +02001180#define K_HOURS(h) K_MINUTES((h) * 60)
1181
Allan Stephensc98da842016-11-11 15:45:03 -05001182/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001183 * @brief Generate infinite timeout delay.
1184 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001185 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001186 * to wait as long as necessary to perform the requested operation.
1187 *
1188 * @return Timeout delay value.
1189 */
Andy Ross78327382020-03-05 15:18:14 -08001190#define K_FOREVER Z_FOREVER
Allan Stephensc2f15a42016-11-17 12:24:22 -05001191
Andy Rosse1bc5952020-03-09 12:19:54 -07001192#ifdef CONFIG_TIMEOUT_64BIT
1193
Allan Stephensc2f15a42016-11-17 12:24:22 -05001194/**
Andy Rosse39bf292020-03-19 10:30:33 -07001195 * @brief Generates an absolute/uptime timeout value from system ticks
Andy Ross4c7b77a2020-03-09 09:35:35 -07001196 *
1197 * This macro generates a timeout delay that represents an expiration
Andy Rosse39bf292020-03-19 10:30:33 -07001198 * at the absolute uptime value specified, in system ticks. That is, the
Andy Ross4c7b77a2020-03-09 09:35:35 -07001199 * timeout will expire immediately after the system uptime reaches the
1200 * specified tick count.
1201 *
1202 * @param t Tick uptime value
1203 * @return Timeout delay value
1204 */
Martin Jäger19c2f782020-11-09 10:14:53 +01001205#define K_TIMEOUT_ABS_TICKS(t) \
1206 Z_TIMEOUT_TICKS(Z_TICK_ABS((k_ticks_t)MAX(t, 0)))
Andy Ross4c7b77a2020-03-09 09:35:35 -07001207
1208/**
Andy Rosse39bf292020-03-19 10:30:33 -07001209 * @brief Generates an absolute/uptime timeout value from milliseconds
Andy Ross4c7b77a2020-03-09 09:35:35 -07001210 *
1211 * This macro generates a timeout delay that represents an expiration
1212 * at the absolute uptime value specified, in milliseconds. That is,
1213 * the timeout will expire immediately after the system uptime reaches
1214 * the specified tick count.
1215 *
1216 * @param t Millisecond uptime value
1217 * @return Timeout delay value
1218 */
1219#define K_TIMEOUT_ABS_MS(t) K_TIMEOUT_ABS_TICKS(k_ms_to_ticks_ceil64(t))
1220
1221/**
Andy Rosse39bf292020-03-19 10:30:33 -07001222 * @brief Generates an absolute/uptime timeout value from microseconds
Andy Rosse1bc5952020-03-09 12:19:54 -07001223 *
1224 * This macro generates a timeout delay that represents an expiration
1225 * at the absolute uptime value specified, in microseconds. That is,
1226 * the timeout will expire immediately after the system uptime reaches
1227 * the specified time. Note that timer precision is limited by the
1228 * system tick rate and not the requested timeout value.
1229 *
1230 * @param t Microsecond uptime value
1231 * @return Timeout delay value
1232 */
1233#define K_TIMEOUT_ABS_US(t) K_TIMEOUT_ABS_TICKS(k_us_to_ticks_ceil64(t))
1234
1235/**
Andy Rosse39bf292020-03-19 10:30:33 -07001236 * @brief Generates an absolute/uptime timeout value from nanoseconds
Andy Rosse1bc5952020-03-09 12:19:54 -07001237 *
1238 * This macro generates a timeout delay that represents an expiration
1239 * at the absolute uptime value specified, in nanoseconds. That is,
1240 * the timeout will expire immediately after the system uptime reaches
1241 * the specified time. Note that timer precision is limited by the
1242 * system tick rate and not the requested timeout value.
1243 *
1244 * @param t Nanosecond uptime value
1245 * @return Timeout delay value
1246 */
1247#define K_TIMEOUT_ABS_NS(t) K_TIMEOUT_ABS_TICKS(k_ns_to_ticks_ceil64(t))
1248
1249/**
Andy Rosse39bf292020-03-19 10:30:33 -07001250 * @brief Generates an absolute/uptime timeout value from system cycles
Andy Rosse1bc5952020-03-09 12:19:54 -07001251 *
1252 * This macro generates a timeout delay that represents an expiration
1253 * at the absolute uptime value specified, in cycles. That is, the
1254 * timeout will expire immediately after the system uptime reaches the
1255 * specified time. Note that timer precision is limited by the system
1256 * tick rate and not the requested timeout value.
1257 *
1258 * @param t Cycle uptime value
1259 * @return Timeout delay value
1260 */
1261#define K_TIMEOUT_ABS_CYC(t) K_TIMEOUT_ABS_TICKS(k_cyc_to_ticks_ceil64(t))
1262
1263#endif
1264
1265/**
Anas Nashif166f5192018-02-25 08:02:36 -06001266 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001267 */
1268
1269/**
Allan Stephensc98da842016-11-11 15:45:03 -05001270 * @cond INTERNAL_HIDDEN
1271 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001272
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273struct k_timer {
1274 /*
1275 * _timeout structure must be first here if we want to use
1276 * dynamic timer allocation. timeout.node is used in the double-linked
1277 * list of free timers
1278 */
1279 struct _timeout timeout;
1280
Allan Stephens45bfa372016-10-12 12:39:42 -05001281 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001282 _wait_q_t wait_q;
1283
1284 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001285 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286
1287 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001288 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001289
1290 /* timer period */
Andy Ross78327382020-03-05 15:18:14 -08001291 k_timeout_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001292
Allan Stephens45bfa372016-10-12 12:39:42 -05001293 /* timer status */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001294 uint32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001295
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001296 /* user-specific data, also used to support legacy features */
1297 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001298
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001299 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001300 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001301};
1302
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001303#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001304 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001305 .timeout = { \
1306 .node = {},\
Peter Bigote37c7852020-07-07 12:34:05 -05001307 .fn = z_timer_expiration_handler, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001308 .dticks = 0, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001309 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001310 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001311 .expiry_fn = expiry, \
1312 .stop_fn = stop, \
1313 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001314 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001315 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001316 }
1317
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318/**
Allan Stephensc98da842016-11-11 15:45:03 -05001319 * INTERNAL_HIDDEN @endcond
1320 */
1321
1322/**
1323 * @defgroup timer_apis Timer APIs
1324 * @ingroup kernel_apis
1325 * @{
1326 */
1327
1328/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001329 * @typedef k_timer_expiry_t
1330 * @brief Timer expiry function type.
1331 *
1332 * A timer's expiry function is executed by the system clock interrupt handler
1333 * each time the timer expires. The expiry function is optional, and is only
1334 * invoked if the timer has been initialized with one.
1335 *
1336 * @param timer Address of timer.
1337 *
1338 * @return N/A
1339 */
1340typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1341
1342/**
1343 * @typedef k_timer_stop_t
1344 * @brief Timer stop function type.
1345 *
1346 * A timer's stop function is executed if the timer is stopped prematurely.
Peter A. Bigot82a98d72020-09-21 05:34:56 -05001347 * The function runs in the context of call that stops the timer. As
1348 * k_timer_stop() can be invoked from an ISR, the stop function must be
1349 * callable from interrupt context (isr-ok).
1350 *
Allan Stephens5eceb852016-11-16 10:16:30 -05001351 * The stop function is optional, and is only invoked if the timer has been
1352 * initialized with one.
1353 *
1354 * @param timer Address of timer.
1355 *
1356 * @return N/A
1357 */
1358typedef void (*k_timer_stop_t)(struct k_timer *timer);
1359
1360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001363 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001365 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 *
1367 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001368 * @param expiry_fn Function to invoke each time the timer expires.
1369 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001370 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001371#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001372 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001373 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001374
Allan Stephens45bfa372016-10-12 12:39:42 -05001375/**
1376 * @brief Initialize a timer.
1377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001378 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001379 *
1380 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001381 * @param expiry_fn Function to invoke each time the timer expires.
1382 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001383 *
1384 * @return N/A
1385 */
1386extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001387 k_timer_expiry_t expiry_fn,
1388 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001389
Allan Stephens45bfa372016-10-12 12:39:42 -05001390/**
1391 * @brief Start a timer.
1392 *
1393 * This routine starts a timer, and resets its status to zero. The timer
1394 * begins counting down using the specified duration and period values.
1395 *
1396 * Attempting to start a timer that is already running is permitted.
1397 * The timer's status is reset to zero and the timer begins counting down
1398 * using the new duration and period values.
1399 *
1400 * @param timer Address of timer.
Andy Ross78327382020-03-05 15:18:14 -08001401 * @param duration Initial timer duration.
1402 * @param period Timer period.
Allan Stephens45bfa372016-10-12 12:39:42 -05001403 *
1404 * @return N/A
1405 */
Andrew Boiea354d492017-09-29 16:22:28 -07001406__syscall void k_timer_start(struct k_timer *timer,
Andy Ross78327382020-03-05 15:18:14 -08001407 k_timeout_t duration, k_timeout_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001408
1409/**
1410 * @brief Stop a timer.
1411 *
1412 * This routine stops a running timer prematurely. The timer's stop function,
1413 * if one exists, is invoked by the caller.
1414 *
1415 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001416 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001417 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001418 * @note The stop handler has to be callable from ISRs if @a k_timer_stop is to
1419 * be called from ISRs.
1420 *
1421 * @funcprops \isr_ok
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001422 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001423 * @param timer Address of timer.
1424 *
1425 * @return N/A
1426 */
Andrew Boiea354d492017-09-29 16:22:28 -07001427__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001428
1429/**
1430 * @brief Read timer status.
1431 *
1432 * This routine reads the timer's status, which indicates the number of times
1433 * it has expired since its status was last read.
1434 *
1435 * Calling this routine resets the timer's status to zero.
1436 *
1437 * @param timer Address of timer.
1438 *
1439 * @return Timer status.
1440 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001441__syscall uint32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001442
1443/**
1444 * @brief Synchronize thread to timer expiration.
1445 *
1446 * This routine blocks the calling thread until the timer's status is non-zero
1447 * (indicating that it has expired at least once since it was last examined)
1448 * or the timer is stopped. If the timer status is already non-zero,
1449 * or the timer is already stopped, the caller continues without waiting.
1450 *
1451 * Calling this routine resets the timer's status to zero.
1452 *
1453 * This routine must not be used by interrupt handlers, since they are not
1454 * allowed to block.
1455 *
1456 * @param timer Address of timer.
1457 *
1458 * @return Timer status.
1459 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001460__syscall uint32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001461
Andy Ross5a5d3da2020-03-09 13:59:15 -07001462#ifdef CONFIG_SYS_CLOCK_EXISTS
1463
1464/**
Andy Rosse39bf292020-03-19 10:30:33 -07001465 * @brief Get next expiration time of a timer, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -07001466 *
1467 * This routine returns the future system uptime reached at the next
1468 * time of expiration of the timer, in units of system ticks. If the
1469 * timer is not running, current system time is returned.
1470 *
1471 * @param timer The timer object
1472 * @return Uptime of expiration, in ticks
1473 */
Peter Bigot0ab314f2020-11-16 15:28:59 -06001474__syscall k_ticks_t k_timer_expires_ticks(const struct k_timer *timer);
Andy Ross5a5d3da2020-03-09 13:59:15 -07001475
Peter Bigot0ab314f2020-11-16 15:28:59 -06001476static inline k_ticks_t z_impl_k_timer_expires_ticks(
1477 const struct k_timer *timer)
Andy Ross5a5d3da2020-03-09 13:59:15 -07001478{
1479 return z_timeout_expires(&timer->timeout);
1480}
1481
1482/**
Andy Rosse39bf292020-03-19 10:30:33 -07001483 * @brief Get time remaining before a timer next expires, in system ticks
Andy Ross5a5d3da2020-03-09 13:59:15 -07001484 *
1485 * This routine computes the time remaining before a running timer
1486 * next expires, in units of system ticks. If the timer is not
1487 * running, it returns zero.
1488 */
Peter Bigot0ab314f2020-11-16 15:28:59 -06001489__syscall k_ticks_t k_timer_remaining_ticks(const struct k_timer *timer);
Andy Ross5a5d3da2020-03-09 13:59:15 -07001490
Peter Bigot0ab314f2020-11-16 15:28:59 -06001491static inline k_ticks_t z_impl_k_timer_remaining_ticks(
1492 const struct k_timer *timer)
Andy Ross5a5d3da2020-03-09 13:59:15 -07001493{
1494 return z_timeout_remaining(&timer->timeout);
1495}
Andy Ross52e444b2018-09-28 09:06:37 -07001496
Allan Stephens45bfa372016-10-12 12:39:42 -05001497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001499 *
1500 * This routine computes the (approximate) time remaining before a running
1501 * timer next expires. If the timer is not running, it returns zero.
1502 *
1503 * @param timer Address of timer.
1504 *
1505 * @return Remaining time (in milliseconds).
1506 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001507static inline uint32_t k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001508{
Andy Ross5a5d3da2020-03-09 13:59:15 -07001509 return k_ticks_to_ms_floor32(k_timer_remaining_ticks(timer));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001510}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001511
Andy Ross5a5d3da2020-03-09 13:59:15 -07001512#endif /* CONFIG_SYS_CLOCK_EXISTS */
1513
Allan Stephensc98da842016-11-11 15:45:03 -05001514/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001515 * @brief Associate user-specific data with a timer.
1516 *
1517 * This routine records the @a user_data with the @a timer, to be retrieved
1518 * later.
1519 *
1520 * It can be used e.g. in a timer handler shared across multiple subsystems to
1521 * retrieve data specific to the subsystem this timer is associated with.
1522 *
1523 * @param timer Address of timer.
1524 * @param user_data User data to associate with the timer.
1525 *
1526 * @return N/A
1527 */
Andrew Boiea354d492017-09-29 16:22:28 -07001528__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1529
Anas Nashif954d5502018-02-25 08:37:28 -06001530/**
1531 * @internal
1532 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001533static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001534 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001535{
1536 timer->user_data = user_data;
1537}
1538
1539/**
1540 * @brief Retrieve the user-specific data from a timer.
1541 *
1542 * @param timer Address of timer.
1543 *
1544 * @return The user data.
1545 */
Peter A. Bigotf1b86ca2020-09-18 16:24:57 -05001546__syscall void *k_timer_user_data_get(const struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001547
Peter A. Bigotf1b86ca2020-09-18 16:24:57 -05001548static inline void *z_impl_k_timer_user_data_get(const struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001549{
1550 return timer->user_data;
1551}
1552
Anas Nashif166f5192018-02-25 08:02:36 -06001553/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001554
Allan Stephensc98da842016-11-11 15:45:03 -05001555/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001556 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001557 * @{
1558 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001559
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001560/**
Andy Rosse39bf292020-03-19 10:30:33 -07001561 * @brief Get system uptime, in system ticks.
Andy Ross914205c2020-03-10 15:26:38 -07001562 *
1563 * This routine returns the elapsed time since the system booted, in
Fabio Utzig39fa56b2020-09-11 10:14:37 -03001564 * ticks (c.f. @option{CONFIG_SYS_CLOCK_TICKS_PER_SEC}), which is the
Andy Ross914205c2020-03-10 15:26:38 -07001565 * fundamental unit of resolution of kernel timekeeping.
1566 *
1567 * @return Current uptime in ticks.
1568 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001569__syscall int64_t k_uptime_ticks(void);
Andy Ross914205c2020-03-10 15:26:38 -07001570
1571/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001572 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001574 * This routine returns the elapsed time since the system booted,
1575 * in milliseconds.
1576 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001577 * @note
David B. Kinder00c41ea2019-06-10 11:13:33 -07001578 * While this function returns time in milliseconds, it does
1579 * not mean it has millisecond resolution. The actual resolution depends on
Fabio Utzig39fa56b2020-09-11 10:14:37 -03001580 * @option{CONFIG_SYS_CLOCK_TICKS_PER_SEC} config option.
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001581 *
1582 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001583 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001584static inline int64_t k_uptime_get(void)
Andy Ross914205c2020-03-10 15:26:38 -07001585{
1586 return k_ticks_to_ms_floor64(k_uptime_ticks());
1587}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001588
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001591 *
Peter Bigota6067a32019-08-28 08:19:26 -05001592 * This routine returns the lower 32 bits of the system uptime in
1593 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001594 *
Peter Bigota6067a32019-08-28 08:19:26 -05001595 * Because correct conversion requires full precision of the system
1596 * clock there is no benefit to using this over k_uptime_get() unless
1597 * you know the application will never run long enough for the system
1598 * clock to approach 2^32 ticks. Calls to this function may involve
1599 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001600 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001601 * @note
David B. Kinder00c41ea2019-06-10 11:13:33 -07001602 * While this function returns time in milliseconds, it does
1603 * not mean it has millisecond resolution. The actual resolution depends on
Fabio Utzig39fa56b2020-09-11 10:14:37 -03001604 * @option{CONFIG_SYS_CLOCK_TICKS_PER_SEC} config option
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001605 *
Peter Bigota6067a32019-08-28 08:19:26 -05001606 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001607 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001608static inline uint32_t k_uptime_get_32(void)
Peter Bigota6067a32019-08-28 08:19:26 -05001609{
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001610 return (uint32_t)k_uptime_get();
Peter Bigota6067a32019-08-28 08:19:26 -05001611}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001612
1613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001616 * This routine computes the elapsed time between the current system uptime
1617 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * @param reftime Pointer to a reference time, which is updated to the current
1620 * uptime upon return.
1621 *
1622 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001623 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001624static inline int64_t k_uptime_delta(int64_t *reftime)
Andy Ross987c0e52018-09-27 16:50:00 -07001625{
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001626 int64_t uptime, delta;
Andy Ross987c0e52018-09-27 16:50:00 -07001627
1628 uptime = k_uptime_get();
1629 delta = uptime - *reftime;
1630 *reftime = uptime;
1631
1632 return delta;
1633}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001634
1635/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001636 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001638 * This routine returns the current time, as measured by the system's hardware
1639 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001641 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001642 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001643static inline uint32_t k_cycle_get_32(void)
Andrew Boie979b17f2019-10-03 15:20:41 -07001644{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001645 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001646}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001647
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001648/**
Anas Nashif166f5192018-02-25 08:02:36 -06001649 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001650 */
1651
Allan Stephensc98da842016-11-11 15:45:03 -05001652/**
1653 * @cond INTERNAL_HIDDEN
1654 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001655
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001656struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001657 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001658 struct k_spinlock lock;
Andy Ross99c2d2d2020-06-02 08:34:12 -07001659 _wait_q_t wait_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001660
Andy Ross99c2d2d2020-06-02 08:34:12 -07001661 _POLL_EVENT;
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001662 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001663 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001664};
1665
Anas Nashif45a1d8a2020-04-24 11:29:17 -04001666#define Z_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001667 { \
Toby Firth680ec0b2020-10-05 13:45:47 +01001668 .data_q = SYS_SFLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001669 .lock = { }, \
Andy Ross99c2d2d2020-06-02 08:34:12 -07001670 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1671 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001672 _OBJECT_TRACING_INIT \
1673 }
1674
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001675extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1676
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001677/**
1678 * INTERNAL_HIDDEN @endcond
1679 */
1680
1681/**
1682 * @defgroup queue_apis Queue APIs
1683 * @ingroup kernel_apis
1684 * @{
1685 */
1686
1687/**
1688 * @brief Initialize a queue.
1689 *
1690 * This routine initializes a queue object, prior to its first use.
1691 *
1692 * @param queue Address of the queue.
1693 *
1694 * @return N/A
1695 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001696__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001697
1698/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001699 * @brief Cancel waiting on a queue.
1700 *
1701 * This routine causes first thread pending on @a queue, if any, to
1702 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001703 * If the queue is being waited on by k_poll(), it will return with
1704 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1705 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001706 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001707 * @funcprops \isr_ok
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001708 *
1709 * @param queue Address of the queue.
1710 *
1711 * @return N/A
1712 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001713__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001714
1715/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001716 * @brief Append an element to the end of a queue.
1717 *
1718 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001719 * aligned on a word boundary, and the first word of the item is reserved
1720 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001721 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001722 * @funcprops \isr_ok
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001723 *
1724 * @param queue Address of the queue.
1725 * @param data Address of the data item.
1726 *
1727 * @return N/A
1728 */
1729extern void k_queue_append(struct k_queue *queue, void *data);
1730
1731/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001732 * @brief Append an element to a queue.
1733 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001734 * This routine appends a data item to @a queue. There is an implicit memory
1735 * allocation to create an additional temporary bookkeeping data structure from
1736 * the calling thread's resource pool, which is automatically freed when the
1737 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001738 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001739 * @funcprops \isr_ok
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001740 *
1741 * @param queue Address of the queue.
1742 * @param data Address of the data item.
1743 *
1744 * @retval 0 on success
1745 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1746 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001747__syscall int32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001748
1749/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001750 * @brief Prepend an element to a queue.
1751 *
1752 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001753 * aligned on a word boundary, and the first word of the item is reserved
1754 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001755 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001756 * @funcprops \isr_ok
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001757 *
1758 * @param queue Address of the queue.
1759 * @param data Address of the data item.
1760 *
1761 * @return N/A
1762 */
1763extern void k_queue_prepend(struct k_queue *queue, void *data);
1764
1765/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001766 * @brief Prepend an element to a queue.
1767 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001768 * This routine prepends a data item to @a queue. There is an implicit memory
1769 * allocation to create an additional temporary bookkeeping data structure from
1770 * the calling thread's resource pool, which is automatically freed when the
1771 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001772 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001773 * @funcprops \isr_ok
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001774 *
1775 * @param queue Address of the queue.
1776 * @param data Address of the data item.
1777 *
1778 * @retval 0 on success
1779 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1780 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05001781__syscall int32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001782
1783/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001784 * @brief Inserts an element to a queue.
1785 *
1786 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001787 * data item must be aligned on a word boundary, and the first word of
1788 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001789 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001790 * @funcprops \isr_ok
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001791 *
1792 * @param queue Address of the queue.
1793 * @param prev Address of the previous data item.
1794 * @param data Address of the data item.
1795 *
1796 * @return N/A
1797 */
1798extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1799
1800/**
1801 * @brief Atomically append a list of elements to a queue.
1802 *
1803 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001804 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001805 * in each data item pointing to the next data item; the list must be
1806 * NULL-terminated.
1807 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001808 * @funcprops \isr_ok
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001809 *
1810 * @param queue Address of the queue.
1811 * @param head Pointer to first node in singly-linked list.
1812 * @param tail Pointer to last node in singly-linked list.
1813 *
Anas Nashif756d8b02019-06-16 09:53:55 -04001814 * @retval 0 on success
1815 * @retval -EINVAL on invalid supplied data
1816 *
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001817 */
Anas Nashif756d8b02019-06-16 09:53:55 -04001818extern int k_queue_append_list(struct k_queue *queue, void *head, void *tail);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001819
1820/**
1821 * @brief Atomically add a list of elements to a queue.
1822 *
1823 * This routine adds a list of data items to @a queue in one operation.
1824 * The data items must be in a singly-linked list implemented using a
1825 * sys_slist_t object. Upon completion, the original list is empty.
1826 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001827 * @funcprops \isr_ok
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001828 *
1829 * @param queue Address of the queue.
1830 * @param list Pointer to sys_slist_t object.
1831 *
Anas Nashif756d8b02019-06-16 09:53:55 -04001832 * @retval 0 on success
1833 * @retval -EINVAL on invalid data
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001834 */
Anas Nashif756d8b02019-06-16 09:53:55 -04001835extern int k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001836
1837/**
1838 * @brief Get an element from a queue.
1839 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001840 * This routine removes first data item from @a queue. The first word of the
1841 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001842 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001843 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
1844 *
1845 * @funcprops \isr_ok
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001846 *
1847 * @param queue Address of the queue.
Andy Ross78327382020-03-05 15:18:14 -08001848 * @param timeout Non-negative waiting period to obtain a data item
1849 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01001850 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001851 *
1852 * @return Address of the data item if successful; NULL if returned
1853 * without waiting, or waiting period timed out.
1854 */
Andy Ross78327382020-03-05 15:18:14 -08001855__syscall void *k_queue_get(struct k_queue *queue, k_timeout_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001856
1857/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001858 * @brief Remove an element from a queue.
1859 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001860 * This routine removes data item from @a queue. The first word of the
1861 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001862 * rely on sys_slist_find_and_remove which is not a constant time operation.
1863 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001864 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
1865 *
1866 * @funcprops \isr_ok
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001867 *
1868 * @param queue Address of the queue.
1869 * @param data Address of the data item.
1870 *
1871 * @return true if data item was removed
1872 */
Torbjörn Leksellf9848232021-03-26 11:19:35 +01001873bool k_queue_remove(struct k_queue *queue, void *data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001874
1875/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001876 * @brief Append an element to a queue only if it's not present already.
1877 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001878 * This routine appends data item to @a queue. The first word of the data
1879 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001880 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1881 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001882 * @funcprops \isr_ok
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001883 *
1884 * @param queue Address of the queue.
1885 * @param data Address of the data item.
1886 *
1887 * @return true if data item was added, false if not
1888 */
Torbjörn Leksellf9848232021-03-26 11:19:35 +01001889bool k_queue_unique_append(struct k_queue *queue, void *data);
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001890
1891/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001892 * @brief Query a queue to see if it has data available.
1893 *
1894 * Note that the data might be already gone by the time this function returns
1895 * if other threads are also trying to read from the queue.
1896 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01001897 * @funcprops \isr_ok
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001898 *
1899 * @param queue Address of the queue.
1900 *
1901 * @return Non-zero if the queue is empty.
1902 * @return 0 if data is available.
1903 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001904__syscall int k_queue_is_empty(struct k_queue *queue);
1905
Patrik Flykt4344e272019-03-08 14:19:05 -07001906static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001907{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001908 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001909}
1910
1911/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001912 * @brief Peek element at the head of queue.
1913 *
1914 * Return element from the head of queue without removing it.
1915 *
1916 * @param queue Address of the queue.
1917 *
1918 * @return Head element, or NULL if queue is empty.
1919 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001920__syscall void *k_queue_peek_head(struct k_queue *queue);
1921
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001922/**
1923 * @brief Peek element at the tail of queue.
1924 *
1925 * Return element from the tail of queue without removing it.
1926 *
1927 * @param queue Address of the queue.
1928 *
1929 * @return Tail element, or NULL if queue is empty.
1930 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001931__syscall void *k_queue_peek_tail(struct k_queue *queue);
1932
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001933/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001934 * @brief Statically define and initialize a queue.
1935 *
1936 * The queue can be accessed outside the module where it is defined using:
1937 *
1938 * @code extern struct k_queue <name>; @endcode
1939 *
1940 * @param name Name of the queue.
1941 */
1942#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001943 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04001944 Z_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001945
Anas Nashif166f5192018-02-25 08:02:36 -06001946/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001947
Wentong Wu5611e922019-06-20 23:51:27 +08001948#ifdef CONFIG_USERSPACE
1949/**
1950 * @brief futex structure
1951 *
1952 * A k_futex is a lightweight mutual exclusion primitive designed
1953 * to minimize kernel involvement. Uncontended operation relies
1954 * only on atomic access to shared memory. k_futex are tracked as
Lauren Murphyd922fed2021-02-01 21:24:47 -06001955 * kernel objects and can live in user memory so that any access
1956 * bypasses the kernel object permission management mechanism.
Wentong Wu5611e922019-06-20 23:51:27 +08001957 */
1958struct k_futex {
1959 atomic_t val;
1960};
1961
1962/**
1963 * @brief futex kernel data structure
1964 *
1965 * z_futex_data are the helper data structure for k_futex to complete
1966 * futex contended operation on kernel side, structure z_futex_data
1967 * of every futex object is invisible in user mode.
1968 */
1969struct z_futex_data {
1970 _wait_q_t wait_q;
1971 struct k_spinlock lock;
1972};
1973
1974#define Z_FUTEX_DATA_INITIALIZER(obj) \
1975 { \
1976 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
1977 }
1978
1979/**
1980 * @defgroup futex_apis FUTEX APIs
1981 * @ingroup kernel_apis
1982 * @{
1983 */
1984
1985/**
Wentong Wu5611e922019-06-20 23:51:27 +08001986 * @brief Pend the current thread on a futex
1987 *
1988 * Tests that the supplied futex contains the expected value, and if so,
1989 * goes to sleep until some other thread calls k_futex_wake() on it.
1990 *
1991 * @param futex Address of the futex.
1992 * @param expected Expected value of the futex, if it is different the caller
1993 * will not wait on it.
Andy Ross78327382020-03-05 15:18:14 -08001994 * @param timeout Non-negative waiting period on the futex, or
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01001995 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08001996 * @retval -EACCES Caller does not have read access to futex address.
1997 * @retval -EAGAIN If the futex value did not match the expected parameter.
1998 * @retval -EINVAL Futex parameter address not recognized by the kernel.
1999 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2000 * @retval 0 if the caller went to sleep and was woken up. The caller
2001 * should check the futex's value on wakeup to determine if it needs
2002 * to block again.
2003 */
Andy Ross78327382020-03-05 15:18:14 -08002004__syscall int k_futex_wait(struct k_futex *futex, int expected,
2005 k_timeout_t timeout);
Wentong Wu5611e922019-06-20 23:51:27 +08002006
2007/**
2008 * @brief Wake one/all threads pending on a futex
2009 *
2010 * Wake up the highest priority thread pending on the supplied futex, or
2011 * wakeup all the threads pending on the supplied futex, and the behavior
2012 * depends on wake_all.
2013 *
2014 * @param futex Futex to wake up pending threads.
2015 * @param wake_all If true, wake up all pending threads; If false,
2016 * wakeup the highest priority thread.
2017 * @retval -EACCES Caller does not have access to the futex address.
2018 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2019 * @retval Number of threads that were woken up.
2020 */
2021__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2022
2023/** @} */
2024#endif
2025
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002027 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028};
2029
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002030/**
2031 * @cond INTERNAL_HIDDEN
2032 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002033#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002034 { \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002035 ._queue = Z_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002036 }
2037
2038/**
2039 * INTERNAL_HIDDEN @endcond
2040 */
2041
2042/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002043 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002044 * @ingroup kernel_apis
2045 * @{
2046 */
2047
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002049 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002051 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002052 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002053 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054 *
2055 * @return N/A
2056 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002057#define k_fifo_init(fifo) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002058 ({ \
2059 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, init, fifo); \
2060 k_queue_init(&(fifo)->_queue); \
2061 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, init, fifo); \
2062 })
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002063
2064/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002065 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002066 *
2067 * This routine causes first thread pending on @a fifo, if any, to
2068 * return from k_fifo_get() call with NULL value (as if timeout
2069 * expired).
2070 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002071 * @funcprops \isr_ok
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002072 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002074 *
2075 * @return N/A
2076 */
2077#define k_fifo_cancel_wait(fifo) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002078 ({ \
2079 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, cancel_wait, fifo); \
2080 k_queue_cancel_wait(&(fifo)->_queue); \
2081 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, cancel_wait, fifo); \
2082 })
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002083
2084/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002085 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002087 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002088 * aligned on a word boundary, and the first word of the item is reserved
2089 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002091 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002093 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002095 *
2096 * @return N/A
2097 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002098#define k_fifo_put(fifo, data) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002099 ({ \
2100 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, put, fifo, data); \
2101 k_queue_append(&(fifo)->_queue, data); \
2102 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, put, fifo, data); \
2103 })
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104
2105/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002106 * @brief Add an element to a FIFO queue.
2107 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002108 * This routine adds a data item to @a fifo. There is an implicit memory
2109 * allocation to create an additional temporary bookkeeping data structure from
2110 * the calling thread's resource pool, which is automatically freed when the
2111 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002112 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002113 * @funcprops \isr_ok
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002114 *
2115 * @param fifo Address of the FIFO.
2116 * @param data Address of the data item.
2117 *
2118 * @retval 0 on success
2119 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2120 */
2121#define k_fifo_alloc_put(fifo, data) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002122 ({ \
2123 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, alloc_put, fifo, data); \
2124 int ret = k_queue_alloc_append(&(fifo)->_queue, data); \
2125 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, alloc_put, fifo, data, ret); \
2126 ret; \
2127 })
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002128
2129/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002130 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002133 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002134 * each data item pointing to the next data item; the list must be
2135 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002137 * @funcprops \isr_ok
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002139 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002140 * @param head Pointer to first node in singly-linked list.
2141 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002142 *
2143 * @return N/A
2144 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002145#define k_fifo_put_list(fifo, head, tail) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002146 ({ \
2147 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, put_list, fifo, head, tail); \
2148 k_queue_append_list(&(fifo)->_queue, head, tail); \
2149 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, put_list, fifo, head, tail); \
2150 })
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002151
2152/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002153 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002155 * This routine adds a list of data items to @a fifo in one operation.
2156 * The data items must be in a singly-linked list implemented using a
2157 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 * and must be re-initialized via sys_slist_init().
2159 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002160 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002161 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002162 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
2165 * @return N/A
2166 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002167#define k_fifo_put_slist(fifo, list) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002168 ({ \
2169 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, put_slist, fifo, list); \
2170 k_queue_merge_slist(&(fifo)->_queue, list); \
2171 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, put_slist, fifo, list); \
2172 })
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173
2174/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002175 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002177 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002178 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002180 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
2181 *
2182 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002184 * @param fifo Address of the FIFO queue.
Andy Ross78327382020-03-05 15:18:14 -08002185 * @param timeout Waiting period to obtain a data item,
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186 * or one of the special values K_NO_WAIT and K_FOREVER.
2187 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002188 * @return Address of the data item if successful; NULL if returned
2189 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002190 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002191#define k_fifo_get(fifo, timeout) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002192 ({ \
2193 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, get, fifo, timeout); \
2194 void *ret = k_queue_get(&(fifo)->_queue, timeout); \
2195 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, get, fifo, timeout, ret); \
2196 ret; \
2197 })
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002199/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002200 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002201 *
2202 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002203 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002204 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002205 * @funcprops \isr_ok
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002206 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002207 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002208 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002209 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002210 * @return 0 if data is available.
2211 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002212#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002213 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002214
2215/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002217 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302219 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002220 * on each iteration of processing, a head container will be peeked,
2221 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002222 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002223 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002224 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002225 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002226 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002227 */
2228#define k_fifo_peek_head(fifo) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002229 ({ \
2230 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, peek_head, fifo); \
2231 void *ret = k_queue_peek_head(&(fifo)->_queue); \
2232 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, peek_head, fifo, ret); \
2233 ret; \
2234 })
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002235
2236/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002237 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002238 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002239 * Return element from the tail of FIFO queue (without removing it). A usecase
2240 * for this is if elements of the FIFO queue are themselves containers. Then
2241 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002242 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002243 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002244 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002245 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002246 */
2247#define k_fifo_peek_tail(fifo) \
Torbjörn Leksell83ae27b2021-03-26 11:42:16 +01002248 ({ \
2249 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_fifo, peek_tail, fifo); \
2250 void *ret = k_queue_peek_tail(&(fifo)->_queue); \
2251 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_fifo, peek_tail, fifo, ret); \
2252 ret; \
2253 })
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002254
2255/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002256 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002257 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002258 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002259 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002260 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002261 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002262 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264#define K_FIFO_DEFINE(name) \
Andrew Boie45979da2020-05-23 14:38:39 -07002265 Z_STRUCT_SECTION_ITERABLE_ALTERNATE(k_queue, k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002266 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267
Anas Nashif166f5192018-02-25 08:02:36 -06002268/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002269
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002270struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002271 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272};
2273
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002274/**
2275 * @cond INTERNAL_HIDDEN
2276 */
2277
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002278#define Z_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002279 { \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002280 ._queue = Z_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002281 }
2282
2283/**
2284 * INTERNAL_HIDDEN @endcond
2285 */
2286
2287/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002288 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002289 * @ingroup kernel_apis
2290 * @{
2291 */
2292
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002294 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002296 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002298 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 *
2300 * @return N/A
2301 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002302#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002303 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304
2305/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002306 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002307 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002308 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002309 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002310 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002312 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002314 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002316 *
2317 * @return N/A
2318 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002319#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002320 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321
2322/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002323 * @brief Add an element to a LIFO queue.
2324 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002325 * This routine adds a data item to @a lifo. There is an implicit memory
2326 * allocation to create an additional temporary bookkeeping data structure from
2327 * the calling thread's resource pool, which is automatically freed when the
2328 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002329 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002330 * @funcprops \isr_ok
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002331 *
2332 * @param lifo Address of the LIFO.
2333 * @param data Address of the data item.
2334 *
2335 * @retval 0 on success
2336 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2337 */
2338#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002339 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002340
2341/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002342 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
Anas Nashif56821172020-07-08 14:14:25 -04002344 * This routine removes a data item from @a LIFO in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002345 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002347 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
2348 *
2349 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002351 * @param lifo Address of the LIFO queue.
Andy Ross78327382020-03-05 15:18:14 -08002352 * @param timeout Waiting period to obtain a data item,
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002353 * or one of the special values K_NO_WAIT and K_FOREVER.
2354 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002355 * @return Address of the data item if successful; NULL if returned
2356 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002358#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002359 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002362 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002363 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002364 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002366 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370#define K_LIFO_DEFINE(name) \
Andrew Boie45979da2020-05-23 14:38:39 -07002371 Z_STRUCT_SECTION_ITERABLE_ALTERNATE(k_queue, k_lifo, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002372 Z_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373
Anas Nashif166f5192018-02-25 08:02:36 -06002374/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002375
2376/**
2377 * @cond INTERNAL_HIDDEN
2378 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05002379#define K_STACK_FLAG_ALLOC ((uint8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002381typedef uintptr_t stack_data_t;
2382
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002383struct k_stack {
2384 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002385 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002386 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002388 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002389 _OBJECT_TRACING_LINKED_FLAG
Kumar Galaa1b77fd2020-05-27 11:26:57 -05002390 uint8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391};
2392
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002393#define Z_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002394 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002395 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002396 .base = stack_buffer, \
2397 .next = stack_buffer, \
2398 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002399 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002400 }
2401
2402/**
2403 * INTERNAL_HIDDEN @endcond
2404 */
2405
2406/**
2407 * @defgroup stack_apis Stack APIs
2408 * @ingroup kernel_apis
2409 * @{
2410 */
2411
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412/**
2413 * @brief Initialize a stack.
2414 *
2415 * This routine initializes a stack object, prior to its first use.
2416 *
2417 * @param stack Address of the stack.
2418 * @param buffer Address of array used to hold stacked values.
2419 * @param num_entries Maximum number of values that can be stacked.
2420 *
2421 * @return N/A
2422 */
Andrew Boief3bee952018-05-02 17:44:39 -07002423void k_stack_init(struct k_stack *stack,
Kumar Galaa1b77fd2020-05-27 11:26:57 -05002424 stack_data_t *buffer, uint32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002425
2426
2427/**
2428 * @brief Initialize a stack.
2429 *
2430 * This routine initializes a stack object, prior to its first use. Internal
2431 * buffers will be allocated from the calling thread's resource pool.
2432 * This memory will be released if k_stack_cleanup() is called, or
2433 * userspace is enabled and the stack object loses all references to it.
2434 *
2435 * @param stack Address of the stack.
2436 * @param num_entries Maximum number of values that can be stacked.
2437 *
2438 * @return -ENOMEM if memory couldn't be allocated
2439 */
2440
Kumar Galaa1b77fd2020-05-27 11:26:57 -05002441__syscall int32_t k_stack_alloc_init(struct k_stack *stack,
2442 uint32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002443
2444/**
2445 * @brief Release a stack's allocated buffer
2446 *
2447 * If a stack object was given a dynamically allocated buffer via
2448 * k_stack_alloc_init(), this will free it. This function does nothing
2449 * if the buffer wasn't dynamically allocated.
2450 *
2451 * @param stack Address of the stack.
Anas Nashif1ed67d12019-06-16 08:58:10 -04002452 * @retval 0 on success
2453 * @retval -EAGAIN when object is still in use
Andrew Boief3bee952018-05-02 17:44:39 -07002454 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002455int k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456
2457/**
2458 * @brief Push an element onto a stack.
2459 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002460 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002462 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 *
2464 * @param stack Address of the stack.
2465 * @param data Value to push onto the stack.
2466 *
Anas Nashif1ed67d12019-06-16 08:58:10 -04002467 * @retval 0 on success
2468 * @retval -ENOMEM if stack is full
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 */
Anas Nashif1ed67d12019-06-16 08:58:10 -04002470__syscall int k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471
2472/**
2473 * @brief Pop an element from a stack.
2474 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002475 * This routine removes a stack_data_t value from @a stack in a "last in,
2476 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002478 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
2479 *
2480 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 *
2482 * @param stack Address of the stack.
2483 * @param data Address of area to hold the value popped from the stack.
Andy Ross78327382020-03-05 15:18:14 -08002484 * @param timeout Waiting period to obtain a value,
2485 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002486 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002488 * @retval 0 Element popped from stack.
2489 * @retval -EBUSY Returned without waiting.
2490 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002491 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002492__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
Andy Ross78327382020-03-05 15:18:14 -08002493 k_timeout_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002495/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002496 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002500 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002502 * @param name Name of the stack.
2503 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002504 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002505#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002506 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002507 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002508 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002509 Z_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002510 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511
Anas Nashif166f5192018-02-25 08:02:36 -06002512/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002513
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002514/**
2515 * @cond INTERNAL_HIDDEN
2516 */
Peter Bigot44539ed2020-11-21 06:58:58 -06002517
Allan Stephens6bba9b02016-11-16 14:56:54 -05002518struct k_work;
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002519struct k_work_q;
2520struct k_work_queue_config;
2521struct k_delayed_work;
2522extern struct k_work_q k_sys_work_q;
2523
2524/**
2525 * INTERNAL_HIDDEN @endcond
2526 */
2527
Allan Stephensc98da842016-11-11 15:45:03 -05002528/**
Anas Nashifce78d162018-05-24 12:43:11 -05002529 * @defgroup mutex_apis Mutex APIs
2530 * @ingroup kernel_apis
2531 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002532 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533
Anas Nashifce78d162018-05-24 12:43:11 -05002534/**
2535 * Mutex Structure
2536 * @ingroup mutex_apis
2537 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05002539 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002541 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002542 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05002543
2544 /** Current lock count */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05002545 uint32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05002546
2547 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002550 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002551 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552};
2553
Anas Nashifce78d162018-05-24 12:43:11 -05002554/**
2555 * @cond INTERNAL_HIDDEN
2556 */
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002557#define Z_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002559 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560 .owner = NULL, \
2561 .lock_count = 0, \
2562 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002563 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564 }
2565
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002566/**
Allan Stephensc98da842016-11-11 15:45:03 -05002567 * INTERNAL_HIDDEN @endcond
2568 */
2569
2570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002574 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002575 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002578 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002580 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04002581 Z_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002583/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * Upon completion, the mutex is available and does not have an owner.
2589 *
2590 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002591 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04002592 * @retval 0 Mutex object created
2593 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002594 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04002595__syscall int k_mutex_init(struct k_mutex *mutex);
2596
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002597
2598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002599 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002601 * This routine locks @a mutex. If the mutex is locked by another thread,
2602 * the calling thread waits until the mutex becomes available or until
2603 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * A thread is permitted to lock a mutex it has already locked. The operation
2606 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002607 *
Andrew Boie6af97932020-05-27 11:48:30 -07002608 * Mutexes may not be locked in ISRs.
2609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * @param mutex Address of the mutex.
Andy Ross78327382020-03-05 15:18:14 -08002611 * @param timeout Waiting period to lock the mutex,
2612 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002613 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002614 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002615 * @retval 0 Mutex locked.
2616 * @retval -EBUSY Returned without waiting.
2617 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002618 */
Andy Ross78327382020-03-05 15:18:14 -08002619__syscall int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620
2621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002622 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * This routine unlocks @a mutex. The mutex must already be locked by the
2625 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 *
2627 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002629 * thread.
2630 *
Andrew Boie6af97932020-05-27 11:48:30 -07002631 * Mutexes may not be unlocked in ISRs, as mutexes must only be manipulated
2632 * in thread context due to ownership and priority inheritance semantics.
2633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002634 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002635 *
Anas Nashif86bb2d02019-05-04 10:18:13 -04002636 * @retval 0 Mutex unlocked.
2637 * @retval -EPERM The current thread does not own the mutex
2638 * @retval -EINVAL The mutex is not locked
2639 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002640 */
Anas Nashif86bb2d02019-05-04 10:18:13 -04002641__syscall int k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002642
Allan Stephensc98da842016-11-11 15:45:03 -05002643/**
Anas Nashif166f5192018-02-25 08:02:36 -06002644 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002645 */
2646
Anas Nashif06eb4892020-08-23 12:39:09 -04002647
2648struct k_condvar {
2649 _wait_q_t wait_q;
2650};
2651
2652#define Z_CONDVAR_INITIALIZER(obj) \
2653 { \
2654 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
2655 }
2656
2657/**
2658 * @defgroup condvar_apis Condition Variables APIs
2659 * @ingroup kernel_apis
2660 * @{
2661 */
2662
2663/**
2664 * @brief Initialize a condition variable
2665 *
2666 * @param condvar pointer to a @p k_condvar structure
2667 * @retval 0 Condition variable created successfully
2668 */
2669__syscall int k_condvar_init(struct k_condvar *condvar);
2670
2671/**
2672 * @brief Signals one thread that is pending on the condition variable
2673 *
2674 * @param condvar pointer to a @p k_condvar structure
2675 * @retval 0 On success
2676 */
2677__syscall int k_condvar_signal(struct k_condvar *condvar);
2678
2679/**
2680 * @brief Unblock all threads that are pending on the condition
2681 * variable
2682 *
2683 * @param condvar pointer to a @p k_condvar structure
2684 * @return An integer with number of woken threads on success
2685 */
2686__syscall int k_condvar_broadcast(struct k_condvar *condvar);
2687
2688/**
2689 * @brief Waits on the condition variable releasing the mutex lock
2690 *
2691 * Automically releases the currently owned mutex, blocks the current thread
2692 * waiting on the condition variable specified by @a condvar,
2693 * and finally acquires the mutex again.
2694 *
2695 * The waiting thread unblocks only after another thread calls
2696 * k_condvar_signal, or k_condvar_broadcast with the same condition variable.
2697 *
2698 * @param condvar pointer to a @p k_condvar structure
2699 * @param mutex Address of the mutex.
2700 * @param timeout Waiting period for the condition variable
2701 * or one of the special values K_NO_WAIT and K_FOREVER.
2702 * @retval 0 On success
2703 * @retval -EAGAIN Waiting period timed out.
2704 */
2705__syscall int k_condvar_wait(struct k_condvar *condvar, struct k_mutex *mutex,
2706 k_timeout_t timeout);
2707
2708/**
2709 * @brief Statically define and initialize a condition variable.
2710 *
2711 * The condition variable can be accessed outside the module where it is
2712 * defined using:
2713 *
2714 * @code extern struct k_condvar <name>; @endcode
2715 *
2716 * @param name Name of the condition variable.
2717 */
2718#define K_CONDVAR_DEFINE(name) \
2719 Z_STRUCT_SECTION_ITERABLE(k_condvar, name) = \
2720 Z_CONDVAR_INITIALIZER(name)
2721/**
2722 * @}
2723 */
2724
Allan Stephensc98da842016-11-11 15:45:03 -05002725/**
2726 * @cond INTERNAL_HIDDEN
2727 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728
2729struct k_sem {
2730 _wait_q_t wait_q;
James Harrisb1042812021-03-03 12:02:05 -08002731 unsigned int count;
2732 unsigned int limit;
Peter Bigot7aefa3d2021-03-02 06:18:29 -06002733
Benjamin Walshacc68c12017-01-29 18:57:45 -05002734 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002736 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002737 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738};
2739
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002740#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002741 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002742 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002743 .count = initial_count, \
2744 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002745 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002746 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002747 }
2748
2749/**
2750 * INTERNAL_HIDDEN @endcond
2751 */
2752
2753/**
2754 * @defgroup semaphore_apis Semaphore APIs
2755 * @ingroup kernel_apis
2756 * @{
2757 */
2758
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002759/**
James Harrisb1042812021-03-03 12:02:05 -08002760 * @brief Maximum limit value allowed for a semaphore.
2761 *
2762 * This is intended for use when a semaphore does not have
2763 * an explicit maximum limit, and instead is just used for
2764 * counting purposes.
2765 *
2766 */
2767#define K_SEM_MAX_LIMIT UINT_MAX
2768
2769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002772 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002774 * @param sem Address of the semaphore.
2775 * @param initial_count Initial semaphore count.
2776 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002777 *
James Harrisb1042812021-03-03 12:02:05 -08002778 * @see K_SEM_MAX_LIMIT
2779 *
Anas Nashif928af3c2019-05-04 10:36:14 -04002780 * @retval 0 Semaphore created successfully
2781 * @retval -EINVAL Invalid values
2782 *
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002783 */
Anas Nashif928af3c2019-05-04 10:36:14 -04002784__syscall int k_sem_init(struct k_sem *sem, unsigned int initial_count,
Andrew Boie99280232017-09-29 14:17:47 -07002785 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002786
2787/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002788 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002791 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002792 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
2793 *
2794 * @funcprops \isr_ok
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002795 *
2796 * @param sem Address of the semaphore.
Andy Ross78327382020-03-05 15:18:14 -08002797 * @param timeout Waiting period to take the semaphore,
2798 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002799 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002800 * @retval 0 Semaphore taken.
2801 * @retval -EBUSY Returned without waiting.
James Harris53b81792021-03-04 15:47:27 -08002802 * @retval -EAGAIN Waiting period timed out,
2803 * or the semaphore was reset during the waiting period.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002804 */
Andy Ross78327382020-03-05 15:18:14 -08002805__syscall int k_sem_take(struct k_sem *sem, k_timeout_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002806
2807/**
2808 * @brief Give a semaphore.
2809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002810 * This routine gives @a sem, unless the semaphore is already at its maximum
2811 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002812 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002813 * @funcprops \isr_ok
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002816 *
2817 * @return N/A
2818 */
Andrew Boie99280232017-09-29 14:17:47 -07002819__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002821/**
James Harris53b81792021-03-04 15:47:27 -08002822 * @brief Resets a semaphore's count to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * This routine sets the count of @a sem to zero.
James Harris53b81792021-03-04 15:47:27 -08002825 * Any outstanding semaphore takes will be aborted
2826 * with -EAGAIN.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002828 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002829 *
2830 * @return N/A
2831 */
Andrew Boie990bf162017-10-03 12:36:49 -07002832__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002833
Anas Nashif954d5502018-02-25 08:37:28 -06002834/**
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002835 * @brief Get a semaphore's count.
2836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002837 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002842 */
Andrew Boie990bf162017-10-03 12:36:49 -07002843__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002844
Anas Nashif954d5502018-02-25 08:37:28 -06002845/**
2846 * @internal
2847 */
Patrik Flykt4344e272019-03-08 14:19:05 -07002848static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849{
2850 return sem->count;
2851}
2852
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002857 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002858 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @param name Name of the semaphore.
2861 * @param initial_count Initial semaphore count.
2862 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002863 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002865 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002866 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302867 BUILD_ASSERT(((count_limit) != 0) && \
James Harrisb1042812021-03-03 12:02:05 -08002868 ((initial_count) <= (count_limit)) && \
2869 ((count_limit) <= K_SEM_MAX_LIMIT));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870
Anas Nashif166f5192018-02-25 08:02:36 -06002871/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002872
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002873/**
2874 * @cond INTERNAL_HIDDEN
2875 */
2876
2877struct k_work_delayable;
2878struct k_work_sync;
2879
2880/**
2881 * INTERNAL_HIDDEN @endcond
2882 */
2883
2884/**
Anas Nashifc355b7e2021-04-14 08:49:05 -04002885 * @defgroup workqueue_apis Work Queue APIs
2886 * @ingroup kernel_apis
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002887 * @{
2888 */
2889
2890/** @brief The signature for a work item handler function.
2891 *
2892 * The function will be invoked by the thread animating a work queue.
2893 *
2894 * @param work the work item that provided the handler.
2895 */
2896typedef void (*k_work_handler_t)(struct k_work *work);
2897
2898/** @brief Initialize a (non-delayable) work structure.
2899 *
2900 * This must be invoked before submitting a work structure for the first time.
2901 * It need not be invoked again on the same work structure. It can be
2902 * re-invoked to change the associated handler, but this must be done when the
2903 * work item is idle.
2904 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002905 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002906 *
2907 * @param work the work structure to be initialized.
2908 *
2909 * @param handler the handler to be invoked by the work item.
2910 */
2911void k_work_init(struct k_work *work,
2912 k_work_handler_t handler);
2913
2914/** @brief Busy state flags from the work item.
2915 *
2916 * A zero return value indicates the work item appears to be idle.
2917 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002918 * @note This is a live snapshot of state, which may change before the result
2919 * is checked. Use locks where appropriate.
2920 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002921 * @funcprops \isr_ok
2922 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002923 * @param work pointer to the work item.
2924 *
2925 * @return a mask of flags K_WORK_DELAYED, K_WORK_QUEUED,
2926 * K_WORK_RUNNING, and K_WORK_CANCELING.
2927 */
2928int k_work_busy_get(const struct k_work *work);
2929
2930/** @brief Test whether a work item is currently pending.
2931 *
2932 * Wrapper to determine whether a work item is in a non-idle dstate.
2933 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002934 * @note This is a live snapshot of state, which may change before the result
2935 * is checked. Use locks where appropriate.
2936 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002937 * @funcprops \isr_ok
2938 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002939 * @param work pointer to the work item.
2940 *
2941 * @return true if and only if k_work_busy_get() returns a non-zero value.
2942 */
2943static inline bool k_work_is_pending(const struct k_work *work);
2944
2945/** @brief Submit a work item to a queue.
2946 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002947 * @param queue pointer to the work queue on which the item should run. If
2948 * NULL the queue from the most recent submission will be used.
2949 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002950 * @funcprops \isr_ok
2951 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002952 * @param work pointer to the work item.
2953 *
2954 * @retval 0 if work was already submitted to a queue
2955 * @retval 1 if work was not submitted and has been queued to @p queue
2956 * @retval 2 if work was running and has been queued to the queue that was
2957 * running it
2958 * @retval -EBUSY
2959 * * if work submission was rejected because the work item is cancelling; or
2960 * * @p queue is draining; or
2961 * * @p queue is plugged.
2962 * @retval -EINVAL if @p queue is null and the work item has never been run.
2963 */
2964int k_work_submit_to_queue(struct k_work_q *queue,
2965 struct k_work *work);
2966
2967/** @brief Submit a work item to the system queue.
2968 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01002969 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05002970 *
2971 * @param work pointer to the work item.
2972 *
2973 * @return as with k_work_submit_to_queue().
2974 */
2975static inline int k_work_submit(struct k_work *work)
2976{
2977 return k_work_submit_to_queue(&k_sys_work_q, work);
2978}
2979
2980/** @brief Wait for last-submitted instance to complete.
2981 *
2982 * Resubmissions may occur while waiting, including chained submissions (from
2983 * within the handler).
2984 *
2985 * @note Be careful of caller and work queue thread relative priority. If
2986 * this function sleeps it will not return until the work queue thread
2987 * completes the tasks that allow this thread to resume.
2988 *
2989 * @note Behavior is undefined if this function is invoked on @p work from a
2990 * work queue running @p work.
2991 *
2992 * @param work pointer to the work item.
2993 *
2994 * @param sync pointer to an opaque item containing state related to the
2995 * pending cancellation. The object must persist until the call returns, and
2996 * be accessible from both the caller thread and the work queue thread. The
2997 * object must not be used for any other flush or cancel operation until this
2998 * one completes. On architectures with CONFIG_KERNEL_COHERENCE the object
2999 * must be allocated in coherent memory.
3000 *
3001 * @retval true if call had to wait for completion
3002 * @retval false if work was already idle
3003 */
3004bool k_work_flush(struct k_work *work,
3005 struct k_work_sync *sync);
3006
3007/** @brief Cancel a work item.
3008 *
3009 * This attempts to prevent a pending (non-delayable) work item from being
3010 * processed by removing it from the work queue. If the item is being
3011 * processed, the work item will continue to be processed, but resubmissions
3012 * are rejected until cancellation completes.
3013 *
3014 * If this returns zero cancellation is complete, otherwise something
3015 * (probably a work queue thread) is still referencing the item.
3016 *
3017 * See also k_work_cancel_sync().
3018 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003019 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003020 *
3021 * @param work pointer to the work item.
3022 *
3023 * @return the k_work_busy_get() status indicating the state of the item after all
3024 * cancellation steps performed by this call are completed.
3025 */
3026int k_work_cancel(struct k_work *work);
3027
3028/** @brief Cancel a work item and wait for it to complete.
3029 *
3030 * Same as k_work_cancel() but does not return until cancellation is complete.
3031 * This can be invoked by a thread after k_work_cancel() to synchronize with a
3032 * previous cancellation.
3033 *
3034 * On return the work structure will be idle unless something submits it after
3035 * the cancellation was complete.
3036 *
3037 * @note Be careful of caller and work queue thread relative priority. If
3038 * this function sleeps it will not return until the work queue thread
3039 * completes the tasks that allow this thread to resume.
3040 *
3041 * @note Behavior is undefined if this function is invoked on @p work from a
3042 * work queue running @p work.
3043 *
3044 * @param work pointer to the work item.
3045 *
3046 * @param sync pointer to an opaque item containing state related to the
3047 * pending cancellation. The object must persist until the call returns, and
3048 * be accessible from both the caller thread and the work queue thread. The
3049 * object must not be used for any other flush or cancel operation until this
3050 * one completes. On architectures with CONFIG_KERNEL_COHERENCE the object
3051 * must be allocated in coherent memory.
3052 *
Peter Bigot707dc222021-04-16 11:48:50 -05003053 * @retval true if work was pending (call had to wait for cancellation of a
3054 * running handler to complete, or scheduled or submitted operations were
3055 * cancelled);
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003056 * @retval false otherwise
3057 */
3058bool k_work_cancel_sync(struct k_work *work, struct k_work_sync *sync);
3059
3060/** @brief Initialize a work queue.
3061 *
3062 * This configures the work queue thread and starts it running. The function
3063 * should not be re-invoked on a queue.
3064 *
3065 * @param queue pointer to the queue structure.
3066 *
3067 * @param stack pointer to the work thread stack area.
3068 *
3069 * @param stack_size size of the the work thread stack area, in bytes.
3070 *
3071 * @param prio initial thread priority
3072 *
3073 * @param cfg optional additional configuration parameters. Pass @c
3074 * NULL if not required, to use the defaults documented in
3075 * k_work_queue_config.
3076 */
3077void k_work_queue_start(struct k_work_q *queue,
3078 k_thread_stack_t *stack, size_t stack_size,
3079 int prio, const struct k_work_queue_config *cfg);
3080
3081/** @brief Access the thread that animates a work queue.
3082 *
3083 * This is necessary to grant a work queue thread access to things the work
3084 * items it will process are expected to use.
3085 *
3086 * @param queue pointer to the queue structure.
3087 *
3088 * @return the thread associated with the work queue.
3089 */
3090static inline k_tid_t k_work_queue_thread_get(struct k_work_q *queue);
3091
3092/** @brief Wait until the work queue has drained, optionally plugging it.
3093 *
3094 * This blocks submission to the work queue except when coming from queue
3095 * thread, and blocks the caller until no more work items are available in the
3096 * queue.
3097 *
3098 * If @p plug is true then submission will continue to be blocked after the
3099 * drain operation completes until k_work_queue_unplug() is invoked.
3100 *
3101 * Note that work items that are delayed are not yet associated with their
3102 * work queue. They must be cancelled externally if a goal is to ensure the
3103 * work queue remains empty. The @p plug feature can be used to prevent
3104 * delayed items from being submitted after the drain completes.
3105 *
3106 * @param queue pointer to the queue structure.
3107 *
3108 * @param plug if true the work queue will continue to block new submissions
3109 * after all items have drained.
3110 *
3111 * @retval 1 if call had to wait for the drain to complete
3112 * @retval 0 if call did not have to wait
3113 * @retval negative if wait was interrupted or failed
3114 */
3115int k_work_queue_drain(struct k_work_q *queue, bool plug);
3116
3117/** @brief Release a work queue to accept new submissions.
3118 *
3119 * This releases the block on new submissions placed when k_work_queue_drain()
3120 * is invoked with the @p plug option enabled. If this is invoked before the
3121 * drain completes new items may be submitted as soon as the drain completes.
3122 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003123 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003124 *
3125 * @param queue pointer to the queue structure.
3126 *
3127 * @retval 0 if successfully unplugged
3128 * @retval -EALREADY if the work queue was not plugged.
3129 */
3130int k_work_queue_unplug(struct k_work_q *queue);
3131
3132/** @brief Initialize a delayable work structure.
3133 *
3134 * This must be invoked before scheduling a delayable work structure for the
3135 * first time. It need not be invoked again on the same work structure. It
3136 * can be re-invoked to change the associated handler, but this must be done
3137 * when the work item is idle.
3138 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003139 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003140 *
3141 * @param dwork the delayable work structure to be initialized.
3142 *
3143 * @param handler the handler to be invoked by the work item.
3144 */
3145void k_work_init_delayable(struct k_work_delayable *dwork,
3146 k_work_handler_t handler);
3147
3148/**
3149 * @brief Get the parent delayable work structure from a work pointer.
3150 *
3151 * This function is necessary when a @c k_work_handler_t function is passed to
3152 * k_work_schedule_for_queue() and the handler needs to access data from the
3153 * container of the containing `k_work_delayable`.
3154 *
3155 * @param work Address passed to the work handler
3156 *
3157 * @return Address of the containing @c k_work_delayable structure.
3158 */
3159static inline struct k_work_delayable *
3160k_work_delayable_from_work(struct k_work *work);
3161
3162/** @brief Busy state flags from the delayable work item.
3163 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003164 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003165 *
3166 * @note This is a live snapshot of state, which may change before the result
3167 * can be inspected. Use locks where appropriate.
3168 *
3169 * @param dwork pointer to the delayable work item.
3170 *
3171 * @return a mask of flags K_WORK_DELAYED, K_WORK_QUEUED, K_WORK_RUNNING, and
3172 * K_WORK_CANCELING. A zero return value indicates the work item appears to
3173 * be idle.
3174 */
3175int k_work_delayable_busy_get(const struct k_work_delayable *dwork);
3176
3177/** @brief Test whether a delayed work item is currently pending.
3178 *
3179 * Wrapper to determine whether a delayed work item is in a non-idle state.
3180 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003181 * @note This is a live snapshot of state, which may change before the result
3182 * can be inspected. Use locks where appropriate.
3183 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003184 * @funcprops \isr_ok
3185 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003186 * @param dwork pointer to the delayable work item.
3187 *
3188 * @return true if and only if k_work_delayable_busy_get() returns a non-zero
3189 * value.
3190 */
3191static inline bool k_work_delayable_is_pending(
3192 const struct k_work_delayable *dwork);
3193
3194/** @brief Get the absolute tick count at which a scheduled delayable work
3195 * will be submitted.
3196 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003197 * @note This is a live snapshot of state, which may change before the result
3198 * can be inspected. Use locks where appropriate.
3199 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003200 * @funcprops \isr_ok
3201 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003202 * @param dwork pointer to the delayable work item.
3203 *
3204 * @return the tick count when the timer that will schedule the work item will
3205 * expire, or the current tick count if the work is not scheduled.
3206 */
3207static inline k_ticks_t k_work_delayable_expires_get(
3208 const struct k_work_delayable *dwork);
3209
3210/** @brief Get the number of ticks until a scheduled delayable work will be
3211 * submitted.
3212 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003213 * @note This is a live snapshot of state, which may change before the result
3214 * can be inspected. Use locks where appropriate.
3215 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003216 * @funcprops \isr_ok
3217 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003218 * @param dwork pointer to the delayable work item.
3219 *
3220 * @return the number of ticks until the timer that will schedule the work
3221 * item will expire, or zero if the item is not scheduled.
3222 */
3223static inline k_ticks_t k_work_delayable_remaining_get(
3224 const struct k_work_delayable *dwork);
3225
3226/** @brief Submit an idle work item to a queue after a delay.
3227 *
3228 * Unlike k_work_reschedule_for_queue() this is a no-op if the work item is
3229 * already scheduled or submitted, even if @p delay is @c K_NO_WAIT.
3230 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003231 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003232 *
3233 * @param queue the queue on which the work item should be submitted after the
3234 * delay.
3235 *
3236 * @param dwork pointer to the delayable work item.
3237 *
3238 * @param delay the time to wait before submitting the work item. If @c
3239 * K_NO_WAIT and the work is not pending this is equivalent to
3240 * k_work_submit_to_queue().
3241 *
3242 * @retval 0 if work was already scheduled or submitted.
3243 * @retval 1 if work has been scheduled.
3244 */
3245int k_work_schedule_for_queue(struct k_work_q *queue,
3246 struct k_work_delayable *dwork,
3247 k_timeout_t delay);
3248
3249/** @brief Submit an idle work item to the system work queue after a
3250 * delay.
3251 *
3252 * This is a thin wrapper around k_work_schedule_for_queue(), with all the API
3253 * characteristcs of that function.
3254 *
3255 * @param dwork pointer to the delayable work item.
3256 *
3257 * @param delay the time to wait before submitting the work item. If @c
3258 * K_NO_WAIT this is equivalent to k_work_submit_to_queue().
3259 *
3260 * @return as with k_work_schedule_for_queue().
3261 */
3262static inline int k_work_schedule(struct k_work_delayable *dwork,
3263 k_timeout_t delay)
3264{
3265 return k_work_schedule_for_queue(&k_sys_work_q, dwork, delay);
3266}
3267
3268/** @brief Reschedule a work item to a queue after a delay.
3269 *
3270 * Unlike k_work_schedule_for_queue() this function can change the deadline of
3271 * a scheduled work item, and will schedule a work item that isn't idle
3272 * (e.g. is submitted or running). This function does not affect ("unsubmit")
3273 * a work item that has been submitted to a queue.
3274 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003275 * @funcprops \isr_ok
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003276 *
3277 * @param queue the queue on which the work item should be submitted after the
3278 * delay.
3279 *
3280 * @param dwork pointer to the delayable work item.
3281 *
3282 * @param delay the time to wait before submitting the work item. If @c
3283 * K_NO_WAIT this is equivalent to k_work_submit_to_queue() after canceling
3284 * any previous scheduled submission.
3285 *
3286 * @note If delay is @c K_NO_WAIT ("no delay") the return values are as with
3287 * k_work_submit_to_queue().
3288 *
3289 * @retval 0 if delay is @c K_NO_WAIT and work was already on a queue
3290 * @retval 1 if
3291 * * delay is @c K_NO_WAIT and work was not submitted but has now been queued
3292 * to @p queue; or
3293 * * delay not @c K_NO_WAIT and work has been scheduled
3294 * @retval 2 if delay is @c K_NO_WAIT and work was running and has been queued
3295 * to the queue that was running it
3296 */
3297int k_work_reschedule_for_queue(struct k_work_q *queue,
3298 struct k_work_delayable *dwork,
3299 k_timeout_t delay);
3300
3301/** @brief Reschedule a work item to the system work queue after a
3302 * delay.
3303 *
3304 * This is a thin wrapper around k_work_reschedule_for_queue(), with all the
3305 * API characteristcs of that function.
3306 *
3307 * @param dwork pointer to the delayable work item.
3308 *
3309 * @param delay the time to wait before submitting the work item.
3310 *
3311 * @return as with k_work_reschedule_for_queue().
3312 */
3313static inline int k_work_reschedule(struct k_work_delayable *dwork,
3314 k_timeout_t delay)
3315{
3316 return k_work_reschedule_for_queue(&k_sys_work_q, dwork, delay);
3317}
3318
3319/** @brief Flush delayable work.
3320 *
3321 * If the work is scheduled, it is immediately submitted. Then the caller
3322 * blocks until the work completes, as with k_work_flush().
3323 *
3324 * @note Be careful of caller and work queue thread relative priority. If
3325 * this function sleeps it will not return until the work queue thread
3326 * completes the tasks that allow this thread to resume.
3327 *
3328 * @note Behavior is undefined if this function is invoked on @p dwork from a
3329 * work queue running @p dwork.
3330 *
3331 * @param dwork pointer to the delayable work item.
3332 *
3333 * @param sync pointer to an opaque item containing state related to the
3334 * pending cancellation. The object must persist until the call returns, and
3335 * be accessible from both the caller thread and the work queue thread. The
3336 * object must not be used for any other flush or cancel operation until this
3337 * one completes. On architectures with CONFIG_KERNEL_COHERENCE the object
3338 * must be allocated in coherent memory.
3339 *
3340 * @retval true if call had to wait for completion
3341 * @retval false if work was already idle
3342 */
3343bool k_work_flush_delayable(struct k_work_delayable *dwork,
3344 struct k_work_sync *sync);
3345
3346/** @brief Cancel delayable work.
3347 *
3348 * Similar to k_work_cancel() but for delayable work. If the work is
3349 * scheduled or submitted it is canceled. This function does not wait for the
3350 * cancellation to complete.
3351 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003352 * @note The work may still be running when this returns. Use
3353 * k_work_flush_delayable() or k_work_cancel_delayable_sync() to ensure it is
3354 * not running.
3355 *
3356 * @note Canceling delayable work does not prevent rescheduling it. It does
3357 * prevent submitting it until the cancellation completes.
3358 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003359 * @funcprops \isr_ok
3360 *
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003361 * @param dwork pointer to the delayable work item.
3362 *
3363 * @return the k_work_delayable_busy_get() status indicating the state of the
3364 * item after all cancellation steps performed by this call are completed.
3365 */
3366int k_work_cancel_delayable(struct k_work_delayable *dwork);
3367
3368/** @brief Cancel delayable work and wait.
3369 *
3370 * Like k_work_cancel_delayable() but waits until the work becomes idle.
3371 *
3372 * @note Canceling delayable work does not prevent rescheduling it. It does
3373 * prevent submitting it until the cancellation completes.
3374 *
3375 * @note Be careful of caller and work queue thread relative priority. If
3376 * this function sleeps it will not return until the work queue thread
3377 * completes the tasks that allow this thread to resume.
3378 *
3379 * @note Behavior is undefined if this function is invoked on @p dwork from a
3380 * work queue running @p dwork.
3381 *
3382 * @param dwork pointer to the delayable work item.
3383 *
3384 * @param sync pointer to an opaque item containing state related to the
3385 * pending cancellation. The object must persist until the call returns, and
3386 * be accessible from both the caller thread and the work queue thread. The
3387 * object must not be used for any other flush or cancel operation until this
3388 * one completes. On architectures with CONFIG_KERNEL_COHERENCE the object
3389 * must be allocated in coherent memory.
3390 *
Peter Bigot707dc222021-04-16 11:48:50 -05003391 * @retval true if work was not idle (call had to wait for cancellation of a
3392 * running handler to complete, or scheduled or submitted operations were
3393 * cancelled);
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003394 * @retval false otherwise
3395 */
3396bool k_work_cancel_delayable_sync(struct k_work_delayable *dwork,
3397 struct k_work_sync *sync);
3398
3399enum {
3400/**
3401 * @cond INTERNAL_HIDDEN
3402 */
3403
3404 /* The atomic API is used for all work and queue flags fields to
3405 * enforce sequential consistency in SMP environments.
3406 */
3407
3408 /* Bits that represent the work item states. At least nine of the
3409 * combinations are distinct valid stable states.
3410 */
3411 K_WORK_RUNNING_BIT = 0,
3412 K_WORK_CANCELING_BIT = 1,
3413 K_WORK_QUEUED_BIT = 2,
3414 K_WORK_DELAYED_BIT = 3,
3415
3416 K_WORK_MASK = BIT(K_WORK_DELAYED_BIT) | BIT(K_WORK_QUEUED_BIT)
3417 | BIT(K_WORK_RUNNING_BIT) | BIT(K_WORK_CANCELING_BIT),
3418
3419 /* Static work flags */
3420 K_WORK_DELAYABLE_BIT = 8,
3421 K_WORK_DELAYABLE = BIT(K_WORK_DELAYABLE_BIT),
3422
3423 /* Dynamic work queue flags */
3424 K_WORK_QUEUE_STARTED_BIT = 0,
3425 K_WORK_QUEUE_STARTED = BIT(K_WORK_QUEUE_STARTED_BIT),
3426 K_WORK_QUEUE_BUSY_BIT = 1,
3427 K_WORK_QUEUE_BUSY = BIT(K_WORK_QUEUE_BUSY_BIT),
3428 K_WORK_QUEUE_DRAIN_BIT = 2,
3429 K_WORK_QUEUE_DRAIN = BIT(K_WORK_QUEUE_DRAIN_BIT),
3430 K_WORK_QUEUE_PLUGGED_BIT = 3,
3431 K_WORK_QUEUE_PLUGGED = BIT(K_WORK_QUEUE_PLUGGED_BIT),
3432
3433 /* Static work queue flags */
3434 K_WORK_QUEUE_NO_YIELD_BIT = 8,
3435 K_WORK_QUEUE_NO_YIELD = BIT(K_WORK_QUEUE_NO_YIELD_BIT),
3436
3437/**
3438 * INTERNAL_HIDDEN @endcond
3439 */
3440 /* Transient work flags */
3441
3442 /** @brief Flag indicating a work item that is running under a work
3443 * queue thread.
3444 *
3445 * Accessed via k_work_busy_get(). May co-occur with other flags.
3446 */
3447 K_WORK_RUNNING = BIT(K_WORK_RUNNING_BIT),
3448
3449 /** @brief Flag indicating a work item that is being canceled.
3450 *
3451 * Accessed via k_work_busy_get(). May co-occur with other flags.
3452 */
3453 K_WORK_CANCELING = BIT(K_WORK_CANCELING_BIT),
3454
3455 /** @brief Flag indicating a work item that has been submitted to a
3456 * queue but has not started running.
3457 *
3458 * Accessed via k_work_busy_get(). May co-occur with other flags.
3459 */
3460 K_WORK_QUEUED = BIT(K_WORK_QUEUED_BIT),
3461
3462 /** @brief Flag indicating a delayed work item that is scheduled for
3463 * submission to a queue.
3464 *
3465 * Accessed via k_work_busy_get(). May co-occur with other flags.
3466 */
3467 K_WORK_DELAYED = BIT(K_WORK_DELAYED_BIT),
3468};
3469
3470/** @brief A structure used to submit work. */
3471struct k_work {
3472 /* All fields are protected by the work module spinlock. No fields
3473 * are to be accessed except through kernel API.
3474 */
3475
3476 /* Node to link into k_work_q pending list. */
3477 sys_snode_t node;
3478
3479 /* The function to be invoked by the work queue thread. */
3480 k_work_handler_t handler;
3481
3482 /* The queue on which the work item was last submitted. */
3483 struct k_work_q *queue;
3484
3485 /* State of the work item.
3486 *
3487 * The item can be DELAYED, QUEUED, and RUNNING simultaneously.
3488 *
3489 * It can be RUNNING and CANCELING simultaneously.
3490 */
3491 uint32_t flags;
3492};
3493
3494#define Z_WORK_INITIALIZER(work_handler) { \
3495 .handler = work_handler, \
3496}
3497
3498/** @brief A structure used to submit work after a delay. */
3499struct k_work_delayable {
3500 /* The work item. */
3501 struct k_work work;
3502
3503 /* Timeout used to submit work after a delay. */
3504 struct _timeout timeout;
3505
3506 /* The queue to which the work should be submitted. */
3507 struct k_work_q *queue;
3508};
3509
3510#define Z_WORK_DELAYABLE_INITIALIZER(work_handler) { \
3511 .work = { \
3512 .handler = work_handler, \
3513 .flags = K_WORK_DELAYABLE, \
3514 }, \
3515}
3516
3517/**
3518 * @brief Initialize a statically-defined delayable work item.
3519 *
3520 * This macro can be used to initialize a statically-defined delayable
3521 * work item, prior to its first use. For example,
3522 *
3523 * @code static K_WORK_DELAYABLE_DEFINE(<dwork>, <work_handler>); @endcode
3524 *
3525 * Note that if the runtime dependencies support initialization with
3526 * k_work_init_delayable() using that will eliminate the initialized
3527 * object in ROM that is produced by this macro and copied in at
3528 * system startup.
3529 *
3530 * @param work Symbol name for delayable work item object
3531 * @param work_handler Function to invoke each time work item is processed.
3532 */
3533#define K_WORK_DELAYABLE_DEFINE(work, work_handler) \
3534 struct k_work_delayable work \
3535 = Z_WORK_DELAYABLE_INITIALIZER(work_handler)
3536
3537/**
3538 * @cond INTERNAL_HIDDEN
3539 */
3540
3541/* Record used to wait for work to flush.
3542 *
3543 * The work item is inserted into the queue that will process (or is
3544 * processing) the item, and will be processed as soon as the item
3545 * completes. When the flusher is processed the semaphore will be
3546 * signaled, releasing the thread waiting for the flush.
3547 */
3548struct z_work_flusher {
3549 struct k_work work;
3550 struct k_sem sem;
3551};
3552
3553/* Record used to wait for work to complete a cancellation.
3554 *
3555 * The work item is inserted into a global queue of pending cancels.
3556 * When a cancelling work item goes idle any matching waiters are
3557 * removed from pending_cancels and are woken.
3558 */
3559struct z_work_canceller {
3560 sys_snode_t node;
3561 struct k_work *work;
3562 struct k_sem sem;
3563};
3564
3565/**
3566 * INTERNAL_HIDDEN @endcond
3567 */
3568
3569/** @brief A structure holding internal state for a pending synchronous
3570 * operation on a work item or queue.
3571 *
3572 * Instances of this type are provided by the caller for invocation of
3573 * k_work_flush(), k_work_cancel_sync() and sibling flush and cancel APIs. A
3574 * referenced object must persist until the call returns, and be accessible
3575 * from both the caller thread and the work queue thread.
3576 *
3577 * @note If CONFIG_KERNEL_COHERENCE is enabled the object must be allocated in
3578 * coherent memory; see arch_mem_coherent(). The stack on these architectures
3579 * is generally not coherent. be stack-allocated. Violations are detected by
3580 * runtime assertion.
3581 */
3582struct k_work_sync {
3583 union {
3584 struct z_work_flusher flusher;
3585 struct z_work_canceller canceller;
3586 };
3587};
3588
3589/** @brief A structure holding optional configuration items for a work
3590 * queue.
3591 *
3592 * This structure, and values it references, are not retained by
3593 * k_work_queue_start().
3594 */
3595struct k_work_queue_config {
3596 /** The name to be given to the work queue thread.
3597 *
3598 * If left null the thread will not have a name.
3599 */
3600 const char *name;
3601
3602 /** Control whether the work queue thread should yield between
3603 * items.
3604 *
3605 * Yielding between items helps guarantee the work queue
3606 * thread does not starve other threads, including cooperative
3607 * ones released by a work item. This is the default behavior.
3608 *
3609 * Set this to @c true to prevent the work queue thread from
3610 * yielding between items. This may be appropriate when a
3611 * sequence of items should complete without yielding
3612 * control.
3613 */
3614 bool no_yield;
3615};
3616
3617/** @brief A structure used to hold work until it can be processed. */
3618struct k_work_q {
3619 /* The thread that animates the work. */
3620 struct k_thread thread;
3621
3622 /* All the following fields must be accessed only while the
3623 * work module spinlock is held.
3624 */
3625
3626 /* List of k_work items to be worked. */
3627 sys_slist_t pending;
3628
3629 /* Wait queue for idle work thread. */
3630 _wait_q_t notifyq;
3631
3632 /* Wait queue for threads waiting for the queue to drain. */
3633 _wait_q_t drainq;
3634
3635 /* Flags describing queue state. */
3636 uint32_t flags;
3637};
3638
3639/* Provide the implementation for inline functions declared above */
3640
3641static inline bool k_work_is_pending(const struct k_work *work)
3642{
3643 return k_work_busy_get(work) != 0;
3644}
3645
3646static inline struct k_work_delayable *
3647k_work_delayable_from_work(struct k_work *work)
3648{
3649 return CONTAINER_OF(work, struct k_work_delayable, work);
3650}
3651
3652static inline bool k_work_delayable_is_pending(
3653 const struct k_work_delayable *dwork)
3654{
3655 return k_work_delayable_busy_get(dwork) != 0;
3656}
3657
3658static inline k_ticks_t k_work_delayable_expires_get(
3659 const struct k_work_delayable *dwork)
3660{
3661 return z_timeout_expires(&dwork->timeout);
3662}
3663
3664static inline k_ticks_t k_work_delayable_remaining_get(
3665 const struct k_work_delayable *dwork)
3666{
3667 return z_timeout_remaining(&dwork->timeout);
3668}
3669
3670static inline k_tid_t k_work_queue_thread_get(struct k_work_q *queue)
3671{
3672 return &queue->thread;
3673}
3674
3675/* Legacy wrappers */
3676
Peter Bigot09a31ce2021-03-04 11:21:46 -06003677__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003678static inline bool k_work_pending(const struct k_work *work)
3679{
3680 return k_work_is_pending(work);
3681}
3682
Peter Bigot09a31ce2021-03-04 11:21:46 -06003683__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003684static inline void k_work_q_start(struct k_work_q *work_q,
3685 k_thread_stack_t *stack,
3686 size_t stack_size, int prio)
3687{
3688 k_work_queue_start(work_q, stack, stack_size, prio, NULL);
3689}
3690
Peter Bigot09a31ce2021-03-04 11:21:46 -06003691/* deprecated, remove when corresponding deprecated API is removed. */
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003692struct k_delayed_work {
3693 struct k_work_delayable work;
3694};
3695
Peter Bigot09a31ce2021-03-04 11:21:46 -06003696#define Z_DELAYED_WORK_INITIALIZER(work_handler) __DEPRECATED_MACRO { \
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003697 .work = Z_WORK_DELAYABLE_INITIALIZER(work_handler), \
3698}
3699
Peter Bigot09a31ce2021-03-04 11:21:46 -06003700__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003701static inline void k_delayed_work_init(struct k_delayed_work *work,
3702 k_work_handler_t handler)
3703{
3704 k_work_init_delayable(&work->work, handler);
3705}
3706
Peter Bigot09a31ce2021-03-04 11:21:46 -06003707__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003708static inline int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3709 struct k_delayed_work *work,
3710 k_timeout_t delay)
3711{
3712 int rc = k_work_reschedule_for_queue(work_q, &work->work, delay);
3713
3714 /* Legacy API doesn't distinguish success cases. */
3715 return (rc >= 0) ? 0 : rc;
3716}
3717
Peter Bigot09a31ce2021-03-04 11:21:46 -06003718__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003719static inline int k_delayed_work_submit(struct k_delayed_work *work,
3720 k_timeout_t delay)
3721{
3722 int rc = k_work_reschedule(&work->work, delay);
3723
3724 /* Legacy API doesn't distinguish success cases. */
3725 return (rc >= 0) ? 0 : rc;
3726}
3727
Peter Bigot09a31ce2021-03-04 11:21:46 -06003728__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003729static inline int k_delayed_work_cancel(struct k_delayed_work *work)
3730{
3731 bool pending = k_work_delayable_is_pending(&work->work);
3732 int rc = k_work_cancel_delayable(&work->work);
3733
3734 /* Old return value rules:
3735 *
3736 * 0 if:
3737 * * Work item countdown cancelled before the item was submitted to
3738 * its queue; or
3739 * * Work item was removed from its queue before it was processed.
3740 *
3741 * -EINVAL if:
3742 * * Work item has never been submitted; or
3743 * * Work item has been successfully cancelled; or
3744 * * Timeout handler is in the process of submitting the work item to
3745 * its queue; or
3746 * * Work queue thread has removed the work item from the queue but
3747 * has not called its handler.
3748 *
3749 * -EALREADY if:
3750 * * Work queue thread has removed the work item from the queue and
3751 * cleared its pending flag; or
3752 * * Work queue thread is invoking the item handler; or
3753 * * Work item handler has completed.
3754 *
3755
3756 * We can't reconstruct those states, so call it successful only when
3757 * a pending item is no longer pending, -EINVAL if it was pending and
3758 * still is, and cancel, and -EALREADY if it wasn't pending (so
3759 * presumably cancellation should have had no effect, assuming we
3760 * didn't hit a race condition).
3761 */
3762 if (pending) {
3763 return (rc == 0) ? 0 : -EINVAL;
3764 }
3765
3766 return -EALREADY;
3767}
3768
Peter Bigot09a31ce2021-03-04 11:21:46 -06003769__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003770static inline bool k_delayed_work_pending(struct k_delayed_work *work)
3771{
3772 return k_work_delayable_is_pending(&work->work);
3773}
3774
Peter Bigot09a31ce2021-03-04 11:21:46 -06003775__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003776static inline int32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
3777{
3778 k_ticks_t rem = k_work_delayable_remaining_get(&work->work);
3779
3780 /* Probably should be ceil32, but was floor32 */
3781 return k_ticks_to_ms_floor32(rem);
3782}
3783
Peter Bigot09a31ce2021-03-04 11:21:46 -06003784__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003785static inline k_ticks_t k_delayed_work_expires_ticks(
3786 struct k_delayed_work *work)
3787{
3788 return k_work_delayable_expires_get(&work->work);
3789}
3790
Peter Bigot09a31ce2021-03-04 11:21:46 -06003791__deprecated
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003792static inline k_ticks_t k_delayed_work_remaining_ticks(
3793 struct k_delayed_work *work)
3794{
3795 return k_work_delayable_remaining_get(&work->work);
3796}
3797
3798/** @} */
3799
Peter Bigot4e3b9262021-01-15 10:52:38 -06003800struct k_work_user;
3801
3802/**
Anas Nashifc355b7e2021-04-14 08:49:05 -04003803 * @addtogroup workqueue_apis
Peter Bigot4e3b9262021-01-15 10:52:38 -06003804 * @{
3805 */
3806
3807/**
3808 * @typedef k_work_user_handler_t
3809 * @brief Work item handler function type for user work queues.
3810 *
3811 * A work item's handler function is executed by a user workqueue's thread
3812 * when the work item is processed by the workqueue.
3813 *
3814 * @param work Address of the work item.
3815 *
3816 * @return N/A
3817 */
3818typedef void (*k_work_user_handler_t)(struct k_work_user *work);
3819
3820/**
3821 * @cond INTERNAL_HIDDEN
3822 */
3823
3824struct k_work_user_q {
3825 struct k_queue queue;
3826 struct k_thread thread;
3827};
3828
3829enum {
3830 K_WORK_USER_STATE_PENDING, /* Work item pending state */
3831};
3832
3833struct k_work_user {
3834 void *_reserved; /* Used by k_queue implementation. */
3835 k_work_user_handler_t handler;
3836 atomic_t flags;
3837};
3838
3839/**
3840 * INTERNAL_HIDDEN @endcond
3841 */
3842
3843#define Z_WORK_USER_INITIALIZER(work_handler) \
3844 { \
3845 .handler = work_handler, \
3846 }
3847
3848/**
3849 * @brief Initialize a statically-defined user work item.
3850 *
3851 * This macro can be used to initialize a statically-defined user work
3852 * item, prior to its first use. For example,
3853 *
3854 * @code static K_WORK_USER_DEFINE(<work>, <work_handler>); @endcode
3855 *
3856 * @param work Symbol name for work item object
3857 * @param work_handler Function to invoke each time work item is processed.
3858 */
3859#define K_WORK_USER_DEFINE(work, work_handler) \
3860 struct k_work_user work = Z_WORK_USER_INITIALIZER(work_handler)
3861
3862/**
3863 * @brief Initialize a userspace work item.
3864 *
3865 * This routine initializes a user workqueue work item, prior to its
3866 * first use.
3867 *
3868 * @param work Address of work item.
3869 * @param handler Function to invoke each time work item is processed.
3870 *
3871 * @return N/A
3872 */
3873static inline void k_work_user_init(struct k_work_user *work,
3874 k_work_user_handler_t handler)
3875{
3876 *work = (struct k_work_user)Z_WORK_USER_INITIALIZER(handler);
3877}
3878
3879/**
3880 * @brief Check if a userspace work item is pending.
3881 *
3882 * This routine indicates if user work item @a work is pending in a workqueue's
3883 * queue.
3884 *
3885 * @note Checking if the work is pending gives no guarantee that the
3886 * work will still be pending when this information is used. It is up to
3887 * the caller to make sure that this information is used in a safe manner.
3888 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003889 * @funcprops \isr_ok
Peter Bigot4e3b9262021-01-15 10:52:38 -06003890 *
3891 * @param work Address of work item.
3892 *
3893 * @return true if work item is pending, or false if it is not pending.
3894 */
3895static inline bool k_work_user_is_pending(struct k_work_user *work)
3896{
3897 return atomic_test_bit(&work->flags, K_WORK_USER_STATE_PENDING);
3898}
3899
3900/**
3901 * @brief Submit a work item to a user mode workqueue
3902 *
3903 * Submits a work item to a workqueue that runs in user mode. A temporary
3904 * memory allocation is made from the caller's resource pool which is freed
3905 * once the worker thread consumes the k_work item. The workqueue
3906 * thread must have memory access to the k_work item being submitted. The caller
3907 * must have permission granted on the work_q parameter's queue object.
3908 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01003909 * @funcprops \isr_ok
Peter Bigot4e3b9262021-01-15 10:52:38 -06003910 *
3911 * @param work_q Address of workqueue.
3912 * @param work Address of work item.
3913 *
3914 * @retval -EBUSY if the work item was already in some workqueue
3915 * @retval -ENOMEM if no memory for thread resource pool allocation
3916 * @retval 0 Success
3917 */
3918static inline int k_work_user_submit_to_queue(struct k_work_user_q *work_q,
3919 struct k_work_user *work)
3920{
3921 int ret = -EBUSY;
3922
3923 if (!atomic_test_and_set_bit(&work->flags,
3924 K_WORK_USER_STATE_PENDING)) {
3925 ret = k_queue_alloc_append(&work_q->queue, work);
3926
3927 /* Couldn't insert into the queue. Clear the pending bit
3928 * so the work item can be submitted again
3929 */
3930 if (ret != 0) {
3931 atomic_clear_bit(&work->flags,
3932 K_WORK_USER_STATE_PENDING);
3933 }
3934 }
3935
3936 return ret;
3937}
3938
3939/**
3940 * @brief Start a workqueue in user mode
3941 *
3942 * This works identically to k_work_queue_start() except it is callable from
3943 * user mode, and the worker thread created will run in user mode. The caller
3944 * must have permissions granted on both the work_q parameter's thread and
3945 * queue objects, and the same restrictions on priority apply as
3946 * k_thread_create().
3947 *
3948 * @param work_q Address of workqueue.
3949 * @param stack Pointer to work queue thread's stack space, as defined by
3950 * K_THREAD_STACK_DEFINE()
3951 * @param stack_size Size of the work queue thread's stack (in bytes), which
3952 * should either be the same constant passed to
3953 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
3954 * @param prio Priority of the work queue's thread.
3955 * @param name optional thread name. If not null a copy is made into the
3956 * thread's name buffer.
3957 *
3958 * @return N/A
3959 */
3960extern void k_work_user_queue_start(struct k_work_user_q *work_q,
3961 k_thread_stack_t *stack,
3962 size_t stack_size, int prio,
3963 const char *name);
3964
3965/** @} */
3966
Allan Stephensc98da842016-11-11 15:45:03 -05003967/**
Peter Bigot3d583982020-11-18 08:55:32 -06003968 * @cond INTERNAL_HIDDEN
3969 */
3970
3971struct k_work_poll {
3972 struct k_work work;
3973 struct k_work_q *workq;
3974 struct z_poller poller;
3975 struct k_poll_event *events;
3976 int num_events;
3977 k_work_handler_t real_handler;
3978 struct _timeout timeout;
3979 int poll_result;
3980};
3981
3982/**
3983 * INTERNAL_HIDDEN @endcond
3984 */
3985
3986/**
Anas Nashifc355b7e2021-04-14 08:49:05 -04003987 * @addtogroup workqueue_apis
Peter Bigot3d583982020-11-18 08:55:32 -06003988 * @{
3989 */
3990
3991/**
Peter Bigotdc34e7c2020-10-28 11:24:05 -05003992 * @brief Initialize a statically-defined work item.
3993 *
3994 * This macro can be used to initialize a statically-defined workqueue work
3995 * item, prior to its first use. For example,
3996 *
3997 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
3998 *
3999 * @param work Symbol name for work item object
4000 * @param work_handler Function to invoke each time work item is processed.
4001 */
4002#define K_WORK_DEFINE(work, work_handler) \
4003 struct k_work work = Z_WORK_INITIALIZER(work_handler)
4004
4005/**
4006 * @brief Initialize a statically-defined delayed work item.
4007 *
4008 * This macro can be used to initialize a statically-defined workqueue
4009 * delayed work item, prior to its first use. For example,
4010 *
4011 * @code static K_DELAYED_WORK_DEFINE(<work>, <work_handler>); @endcode
4012 *
4013 * @param work Symbol name for delayed work item object
4014 * @param work_handler Function to invoke each time work item is processed.
4015 */
Peter Bigot09a31ce2021-03-04 11:21:46 -06004016#define K_DELAYED_WORK_DEFINE(work, work_handler) __DEPRECATED_MACRO \
Peter Bigotdc34e7c2020-10-28 11:24:05 -05004017 struct k_delayed_work work = Z_DELAYED_WORK_INITIALIZER(work_handler)
4018
4019/**
Peter Bigot3d583982020-11-18 08:55:32 -06004020 * @brief Initialize a triggered work item.
4021 *
4022 * This routine initializes a workqueue triggered work item, prior to
4023 * its first use.
4024 *
4025 * @param work Address of triggered work item.
4026 * @param handler Function to invoke each time work item is processed.
4027 *
4028 * @return N/A
4029 */
4030extern void k_work_poll_init(struct k_work_poll *work,
4031 k_work_handler_t handler);
4032
4033/**
4034 * @brief Submit a triggered work item.
4035 *
4036 * This routine schedules work item @a work to be processed by workqueue
4037 * @a work_q when one of the given @a events is signaled. The routine
4038 * initiates internal poller for the work item and then returns to the caller.
4039 * Only when one of the watched events happen the work item is actually
4040 * submitted to the workqueue and becomes pending.
4041 *
4042 * Submitting a previously submitted triggered work item that is still
4043 * waiting for the event cancels the existing submission and reschedules it
4044 * the using the new event list. Note that this behavior is inherently subject
4045 * to race conditions with the pre-existing triggered work item and work queue,
4046 * so care must be taken to synchronize such resubmissions externally.
4047 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004048 * @funcprops \isr_ok
Peter Bigot3d583982020-11-18 08:55:32 -06004049 *
4050 * @warning
4051 * Provided array of events as well as a triggered work item must be placed
4052 * in persistent memory (valid until work handler execution or work
4053 * cancellation) and cannot be modified after submission.
4054 *
4055 * @param work_q Address of workqueue.
4056 * @param work Address of delayed work item.
4057 * @param events An array of events which trigger the work.
4058 * @param num_events The number of events in the array.
4059 * @param timeout Timeout after which the work will be scheduled
4060 * for execution even if not triggered.
4061 *
4062 *
4063 * @retval 0 Work item started watching for events.
4064 * @retval -EINVAL Work item is being processed or has completed its work.
4065 * @retval -EADDRINUSE Work item is pending on a different workqueue.
4066 */
4067extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
4068 struct k_work_poll *work,
4069 struct k_poll_event *events,
4070 int num_events,
4071 k_timeout_t timeout);
4072
4073/**
4074 * @brief Submit a triggered work item to the system workqueue.
4075 *
4076 * This routine schedules work item @a work to be processed by system
4077 * workqueue when one of the given @a events is signaled. The routine
4078 * initiates internal poller for the work item and then returns to the caller.
4079 * Only when one of the watched events happen the work item is actually
4080 * submitted to the workqueue and becomes pending.
4081 *
4082 * Submitting a previously submitted triggered work item that is still
4083 * waiting for the event cancels the existing submission and reschedules it
4084 * the using the new event list. Note that this behavior is inherently subject
4085 * to race conditions with the pre-existing triggered work item and work queue,
4086 * so care must be taken to synchronize such resubmissions externally.
4087 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004088 * @funcprops \isr_ok
Peter Bigot3d583982020-11-18 08:55:32 -06004089 *
4090 * @warning
4091 * Provided array of events as well as a triggered work item must not be
4092 * modified until the item has been processed by the workqueue.
4093 *
4094 * @param work Address of delayed work item.
4095 * @param events An array of events which trigger the work.
4096 * @param num_events The number of events in the array.
4097 * @param timeout Timeout after which the work will be scheduled
4098 * for execution even if not triggered.
4099 *
4100 * @retval 0 Work item started watching for events.
4101 * @retval -EINVAL Work item is being processed or has completed its work.
4102 * @retval -EADDRINUSE Work item is pending on a different workqueue.
4103 */
4104static inline int k_work_poll_submit(struct k_work_poll *work,
4105 struct k_poll_event *events,
4106 int num_events,
4107 k_timeout_t timeout)
4108{
4109 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
4110 events, num_events, timeout);
4111}
4112
4113/**
4114 * @brief Cancel a triggered work item.
4115 *
4116 * This routine cancels the submission of triggered work item @a work.
4117 * A triggered work item can only be canceled if no event triggered work
4118 * submission.
4119 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004120 * @funcprops \isr_ok
Peter Bigot3d583982020-11-18 08:55:32 -06004121 *
4122 * @param work Address of delayed work item.
4123 *
4124 * @retval 0 Work item canceled.
4125 * @retval -EINVAL Work item is being processed or has completed its work.
4126 */
4127extern int k_work_poll_cancel(struct k_work_poll *work);
4128
4129/** @} */
4130
4131/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004132 * @defgroup msgq_apis Message Queue APIs
4133 * @ingroup kernel_apis
4134 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004135 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004137/**
4138 * @brief Message Queue Structure
4139 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004140struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05004141 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004142 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05004143 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07004144 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05004145 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04004146 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05004147 /** Maximal number of messages */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004148 uint32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05004149 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004150 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05004151 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004152 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05004153 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004154 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05004155 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004156 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05004157 /** Number of used messages */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004158 uint32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004159
Nick Gravesb445f132021-04-12 12:35:18 -07004160 _POLL_EVENT;
4161
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004162 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004163 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05004164
4165 /** Message queue */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004166 uint8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004167};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004168/**
4169 * @cond INTERNAL_HIDDEN
4170 */
4171
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004172
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004173#define Z_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004174 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004175 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04004176 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07004177 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004178 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04004179 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004180 .read_ptr = q_buffer, \
4181 .write_ptr = q_buffer, \
4182 .used_msgs = 0, \
Nick Gravesb445f132021-04-12 12:35:18 -07004183 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05004184 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004185 }
Kumar Galac8b94f42020-09-29 09:52:23 -05004186
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004187/**
4188 * INTERNAL_HIDDEN @endcond
4189 */
4190
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004191
Andrew Boie0fe789f2018-04-12 18:35:56 -07004192#define K_MSGQ_FLAG_ALLOC BIT(0)
4193
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004194/**
4195 * @brief Message Queue Attributes
4196 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304197struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05004198 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304199 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05004200 /** Maximal number of messages */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004201 uint32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05004202 /** Used messages */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004203 uint32_t used_msgs;
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304204};
4205
Allan Stephensc98da842016-11-11 15:45:03 -05004206
4207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004208 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04004209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004210 * The message queue's ring buffer contains space for @a q_max_msgs messages,
4211 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004212 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
4213 * message is similarly aligned to this boundary, @a q_msg_size must also be
4214 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04004215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004216 * The message queue can be accessed outside the module where it is defined
4217 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004218 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004219 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004221 * @param q_name Name of the message queue.
4222 * @param q_msg_size Message size (in bytes).
4223 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06004224 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004225 *
Peter Mitsis1da807e2016-10-06 11:36:59 -04004226 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004227#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
4228 static char __noinit __aligned(q_align) \
4229 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
4230 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004231 Z_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04004232 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004233
Peter Mitsisd7a37502016-10-13 11:37:40 -04004234/**
4235 * @brief Initialize a message queue.
4236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004237 * This routine initializes a message queue object, prior to its first use.
4238 *
Allan Stephensda827222016-11-09 14:23:58 -06004239 * The message queue's ring buffer must contain space for @a max_msgs messages,
4240 * each of which is @a msg_size bytes long. The buffer must be aligned to an
4241 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
4242 * that each message is similarly aligned to this boundary, @a q_msg_size
4243 * must also be a multiple of N.
4244 *
Anas Nashif25c87db2021-03-29 10:54:23 -04004245 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004246 * @param buffer Pointer to ring buffer that holds queued messages.
4247 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04004248 * @param max_msgs Maximum number of messages that can be queued.
4249 *
4250 * @return N/A
4251 */
Anas Nashif25c87db2021-03-29 10:54:23 -04004252void k_msgq_init(struct k_msgq *msgq, char *buffer, size_t msg_size,
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004253 uint32_t max_msgs);
Andrew Boie0fe789f2018-04-12 18:35:56 -07004254
4255/**
4256 * @brief Initialize a message queue.
4257 *
4258 * This routine initializes a message queue object, prior to its first use,
4259 * allocating its internal ring buffer from the calling thread's resource
4260 * pool.
4261 *
4262 * Memory allocated for the ring buffer can be released by calling
4263 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
4264 * all of its references.
4265 *
Anas Nashif4b386592019-11-25 09:30:47 -05004266 * @param msgq Address of the message queue.
Andrew Boie0fe789f2018-04-12 18:35:56 -07004267 * @param msg_size Message size (in bytes).
4268 * @param max_msgs Maximum number of messages that can be queued.
4269 *
4270 * @return 0 on success, -ENOMEM if there was insufficient memory in the
4271 * thread's resource pool, or -EINVAL if the size parameters cause
4272 * an integer overflow.
4273 */
Anas Nashif4b386592019-11-25 09:30:47 -05004274__syscall int k_msgq_alloc_init(struct k_msgq *msgq, size_t msg_size,
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004275 uint32_t max_msgs);
Andrew Boie0fe789f2018-04-12 18:35:56 -07004276
Anas Nashife71293e2019-12-04 20:00:14 -05004277/**
Anas Nashif4b386592019-11-25 09:30:47 -05004278 * @brief Release allocated buffer for a queue
Anas Nashife71293e2019-12-04 20:00:14 -05004279 *
4280 * Releases memory allocated for the ring buffer.
Anas Nashif4b386592019-11-25 09:30:47 -05004281 *
4282 * @param msgq message queue to cleanup
4283 *
Anas Nashif11b93652019-06-16 08:43:48 -04004284 * @retval 0 on success
4285 * @retval -EBUSY Queue not empty
Anas Nashife71293e2019-12-04 20:00:14 -05004286 */
Anas Nashif11b93652019-06-16 08:43:48 -04004287int k_msgq_cleanup(struct k_msgq *msgq);
Peter Mitsisd7a37502016-10-13 11:37:40 -04004288
4289/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004290 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004292 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004293 *
Lauren Murphyf29a2d12020-09-16 21:13:40 -05004294 * @note The message content is copied from @a data into @a msgq and the @a data
4295 * pointer is not retained, so the message content will not be modified
4296 * by this function.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05004297 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004298 * @funcprops \isr_ok
4299 *
Anas Nashif4b386592019-11-25 09:30:47 -05004300 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004301 * @param data Pointer to the message.
Andy Ross78327382020-03-05 15:18:14 -08004302 * @param timeout Non-negative waiting period to add the message,
4303 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004304 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004305 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004306 * @retval 0 Message sent.
4307 * @retval -ENOMSG Returned without waiting or queue purged.
4308 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004309 */
Lauren Murphyf29a2d12020-09-16 21:13:40 -05004310__syscall int k_msgq_put(struct k_msgq *msgq, const void *data, k_timeout_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04004311
4312/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004313 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004315 * This routine receives a message from message queue @a q in a "first in,
4316 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004317 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004318 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
4319 *
4320 * @funcprops \isr_ok
Benjamin Walsh8215ce12016-11-09 19:45:19 -05004321 *
Anas Nashif4b386592019-11-25 09:30:47 -05004322 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004323 * @param data Address of area to hold the received message.
Andy Ross78327382020-03-05 15:18:14 -08004324 * @param timeout Waiting period to receive the message,
4325 * or one of the special values K_NO_WAIT and
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004326 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004327 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004328 * @retval 0 Message received.
4329 * @retval -ENOMSG Returned without waiting.
4330 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004331 */
Andy Ross78327382020-03-05 15:18:14 -08004332__syscall int k_msgq_get(struct k_msgq *msgq, void *data, k_timeout_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04004333
4334/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08004335 * @brief Peek/read a message from a message queue.
4336 *
4337 * This routine reads a message from message queue @a q in a "first in,
4338 * first out" manner and leaves the message in the queue.
4339 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004340 * @funcprops \isr_ok
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08004341 *
Anas Nashif4b386592019-11-25 09:30:47 -05004342 * @param msgq Address of the message queue.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08004343 * @param data Address of area to hold the message read from the queue.
4344 *
4345 * @retval 0 Message read.
4346 * @retval -ENOMSG Returned when the queue has no message.
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08004347 */
Anas Nashif4b386592019-11-25 09:30:47 -05004348__syscall int k_msgq_peek(struct k_msgq *msgq, void *data);
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08004349
4350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004351 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004353 * This routine discards all unreceived messages in a message queue's ring
4354 * buffer. Any threads that are blocked waiting to send a message to the
4355 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004356 *
Anas Nashif4b386592019-11-25 09:30:47 -05004357 * @param msgq Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004358 *
4359 * @return N/A
4360 */
Anas Nashif4b386592019-11-25 09:30:47 -05004361__syscall void k_msgq_purge(struct k_msgq *msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004362
Peter Mitsis67be2492016-10-07 11:44:34 -04004363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004364 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04004365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004366 * This routine returns the number of unused entries in a message queue's
4367 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04004368 *
Anas Nashif4b386592019-11-25 09:30:47 -05004369 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004370 *
4371 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04004372 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004373__syscall uint32_t k_msgq_num_free_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07004374
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304375/**
4376 * @brief Get basic attributes of a message queue.
4377 *
4378 * This routine fetches basic attributes of message queue into attr argument.
4379 *
Anas Nashif4b386592019-11-25 09:30:47 -05004380 * @param msgq Address of the message queue.
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304381 * @param attrs pointer to message queue attribute structure.
4382 *
4383 * @return N/A
4384 */
Anas Nashif4b386592019-11-25 09:30:47 -05004385__syscall void k_msgq_get_attrs(struct k_msgq *msgq,
4386 struct k_msgq_attrs *attrs);
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05304387
4388
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004389static inline uint32_t z_impl_k_msgq_num_free_get(struct k_msgq *msgq)
Peter Mitsis67be2492016-10-07 11:44:34 -04004390{
Anas Nashif4b386592019-11-25 09:30:47 -05004391 return msgq->max_msgs - msgq->used_msgs;
Peter Mitsis67be2492016-10-07 11:44:34 -04004392}
4393
Peter Mitsisd7a37502016-10-13 11:37:40 -04004394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004395 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004397 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004398 *
Anas Nashif4b386592019-11-25 09:30:47 -05004399 * @param msgq Address of the message queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004400 *
4401 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04004402 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004403__syscall uint32_t k_msgq_num_used_get(struct k_msgq *msgq);
Andrew Boie82edb6e2017-10-02 10:53:06 -07004404
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004405static inline uint32_t z_impl_k_msgq_num_used_get(struct k_msgq *msgq)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004406{
Anas Nashif4b386592019-11-25 09:30:47 -05004407 return msgq->used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004408}
4409
Anas Nashif166f5192018-02-25 08:02:36 -06004410/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004411
4412/**
Allan Stephensc98da842016-11-11 15:45:03 -05004413 * @defgroup mailbox_apis Mailbox APIs
4414 * @ingroup kernel_apis
4415 * @{
4416 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004417
Anas Nashife71293e2019-12-04 20:00:14 -05004418/**
4419 * @brief Mailbox Message Structure
4420 *
4421 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004422struct k_mbox_msg {
4423 /** internal use only - needed for legacy API support */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004424 uint32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004425 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04004426 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004427 /** application-defined information value */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004428 uint32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004429 /** sender's message data buffer */
4430 void *tx_data;
4431 /** internal use only - needed for legacy API support */
4432 void *_rx_data;
4433 /** message data block descriptor */
4434 struct k_mem_block tx_block;
4435 /** source thread id */
4436 k_tid_t rx_source_thread;
4437 /** target thread id */
4438 k_tid_t tx_target_thread;
4439 /** internal use only - thread waiting on send (may be a dummy) */
4440 k_tid_t _syncing_thread;
4441#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
4442 /** internal use only - semaphore used during asynchronous send */
4443 struct k_sem *_async_sem;
4444#endif
4445};
Anas Nashife71293e2019-12-04 20:00:14 -05004446/**
4447 * @brief Mailbox Structure
4448 *
4449 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004450struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05004451 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004452 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05004453 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004454 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07004455 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004456
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004457 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004458 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004459};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004460/**
4461 * @cond INTERNAL_HIDDEN
4462 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004463
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004464#define Z_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004465 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004466 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
4467 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05004468 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004469 }
4470
Peter Mitsis12092702016-10-14 12:57:23 -04004471/**
Allan Stephensc98da842016-11-11 15:45:03 -05004472 * INTERNAL_HIDDEN @endcond
4473 */
4474
4475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004476 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04004477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004478 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004479 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004480 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004482 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04004483 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004484#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004485 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004486 Z_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004487
Peter Mitsis12092702016-10-14 12:57:23 -04004488/**
4489 * @brief Initialize a mailbox.
4490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004491 * This routine initializes a mailbox object, prior to its first use.
4492 *
4493 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04004494 *
4495 * @return N/A
4496 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004497extern void k_mbox_init(struct k_mbox *mbox);
4498
Peter Mitsis12092702016-10-14 12:57:23 -04004499/**
4500 * @brief Send a mailbox message in a synchronous manner.
4501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004502 * This routine sends a message to @a mbox and waits for a receiver to both
4503 * receive and process it. The message data may be in a buffer, in a memory
4504 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04004505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004506 * @param mbox Address of the mailbox.
4507 * @param tx_msg Address of the transmit message descriptor.
Andy Ross78327382020-03-05 15:18:14 -08004508 * @param timeout Waiting period for the message to be received,
4509 * or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004510 * and K_FOREVER. Once the message has been received,
4511 * this routine waits as long as necessary for the message
4512 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04004513 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004514 * @retval 0 Message sent.
4515 * @retval -ENOMSG Returned without waiting.
4516 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04004517 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004518extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Andy Ross78327382020-03-05 15:18:14 -08004519 k_timeout_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004520
Peter Mitsis12092702016-10-14 12:57:23 -04004521/**
4522 * @brief Send a mailbox message in an asynchronous manner.
4523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004524 * This routine sends a message to @a mbox without waiting for a receiver
4525 * to process it. The message data may be in a buffer, in a memory pool block,
4526 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
4527 * will be given when the message has been both received and completely
4528 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04004529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004530 * @param mbox Address of the mailbox.
4531 * @param tx_msg Address of the transmit message descriptor.
4532 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04004533 *
4534 * @return N/A
4535 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004536extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004537 struct k_sem *sem);
4538
Peter Mitsis12092702016-10-14 12:57:23 -04004539/**
4540 * @brief Receive a mailbox message.
4541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004542 * This routine receives a message from @a mbox, then optionally retrieves
4543 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004545 * @param mbox Address of the mailbox.
4546 * @param rx_msg Address of the receive message descriptor.
4547 * @param buffer Address of the buffer to receive data, or NULL to defer data
4548 * retrieval and message disposal until later.
Andy Ross78327382020-03-05 15:18:14 -08004549 * @param timeout Waiting period for a message to be received,
4550 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04004551 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004552 * @retval 0 Message received.
4553 * @retval -ENOMSG Returned without waiting.
4554 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04004555 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004556extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Andy Ross78327382020-03-05 15:18:14 -08004557 void *buffer, k_timeout_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04004558
4559/**
4560 * @brief Retrieve mailbox message data into a buffer.
4561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004562 * This routine completes the processing of a received message by retrieving
4563 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04004564 *
4565 * Alternatively, this routine can be used to dispose of a received message
4566 * without retrieving its data.
4567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004568 * @param rx_msg Address of the receive message descriptor.
4569 * @param buffer Address of the buffer to receive data, or NULL to discard
4570 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04004571 *
4572 * @return N/A
4573 */
Peter Mitsis40680f62016-10-14 10:04:55 -04004574extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04004575
Anas Nashif166f5192018-02-25 08:02:36 -06004576/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004577
4578/**
Anas Nashifce78d162018-05-24 12:43:11 -05004579 * @defgroup pipe_apis Pipe APIs
4580 * @ingroup kernel_apis
4581 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004582 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004583
Anas Nashifce78d162018-05-24 12:43:11 -05004584/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004585struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004586 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4587 size_t size; /**< Buffer size */
4588 size_t bytes_used; /**< # bytes used in buffer */
4589 size_t read_index; /**< Where in buffer to read from */
4590 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004591 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004592
4593 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004594 _wait_q_t readers; /**< Reader wait queue */
4595 _wait_q_t writers; /**< Writer wait queue */
Anas Nashif0ff33d12020-07-13 20:21:56 -04004596 } wait_q; /** Wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004597
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004598 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004599 _OBJECT_TRACING_LINKED_FLAG
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004600 uint8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004601};
4602
Anas Nashifce78d162018-05-24 12:43:11 -05004603/**
4604 * @cond INTERNAL_HIDDEN
4605 */
4606#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4607
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004608#define Z_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004609 { \
4610 .buffer = pipe_buffer, \
4611 .size = pipe_buffer_size, \
4612 .bytes_used = 0, \
4613 .read_index = 0, \
4614 .write_index = 0, \
4615 .lock = {}, \
4616 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004617 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4618 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004619 }, \
4620 _OBJECT_TRACING_INIT \
4621 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004622 }
4623
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004624/**
Allan Stephensc98da842016-11-11 15:45:03 -05004625 * INTERNAL_HIDDEN @endcond
4626 */
4627
4628/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004629 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004631 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004632 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004633 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004635 * @param name Name of the pipe.
4636 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4637 * or zero if no ring buffer is used.
4638 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004639 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004640 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004641#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004642 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004643 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004644 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004645 Z_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004646
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004648 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004650 * This routine initializes a pipe object, prior to its first use.
4651 *
4652 * @param pipe Address of the pipe.
4653 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4654 * is used.
4655 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4656 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004657 *
4658 * @return N/A
4659 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004660void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4661
4662/**
4663 * @brief Release a pipe's allocated buffer
4664 *
4665 * If a pipe object was given a dynamically allocated buffer via
4666 * k_pipe_alloc_init(), this will free it. This function does nothing
4667 * if the buffer wasn't dynamically allocated.
4668 *
4669 * @param pipe Address of the pipe.
Anas Nashif361a84d2019-06-16 08:22:08 -04004670 * @retval 0 on success
4671 * @retval -EAGAIN nothing to cleanup
Andrew Boie44fe8122018-04-12 17:38:12 -07004672 */
Anas Nashif361a84d2019-06-16 08:22:08 -04004673int k_pipe_cleanup(struct k_pipe *pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07004674
4675/**
4676 * @brief Initialize a pipe and allocate a buffer for it
4677 *
4678 * Storage for the buffer region will be allocated from the calling thread's
4679 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4680 * or userspace is enabled and the pipe object loses all references to it.
4681 *
4682 * This function should only be called on uninitialized pipe objects.
4683 *
4684 * @param pipe Address of the pipe.
4685 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4686 * buffer is used.
4687 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004688 * @retval -ENOMEM if memory couldn't be allocated
Andrew Boie44fe8122018-04-12 17:38:12 -07004689 */
4690__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004691
4692/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004693 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004695 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004697 * @param pipe Address of the pipe.
4698 * @param data Address of data to write.
4699 * @param bytes_to_write Size of data (in bytes).
4700 * @param bytes_written Address of area to hold the number of bytes written.
4701 * @param min_xfer Minimum number of bytes to write.
Andy Ross78327382020-03-05 15:18:14 -08004702 * @param timeout Waiting period to wait for the data to be written,
4703 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004704 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004705 * @retval 0 At least @a min_xfer bytes of data were written.
4706 * @retval -EIO Returned without waiting; zero data bytes were written.
4707 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004708 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004709 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004710__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4711 size_t bytes_to_write, size_t *bytes_written,
Andy Ross78327382020-03-05 15:18:14 -08004712 size_t min_xfer, k_timeout_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004713
4714/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004715 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004717 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004719 * @param pipe Address of the pipe.
4720 * @param data Address to place the data read from pipe.
4721 * @param bytes_to_read Maximum number of data bytes to read.
4722 * @param bytes_read Address of area to hold the number of bytes read.
4723 * @param min_xfer Minimum number of data bytes to read.
Andy Ross78327382020-03-05 15:18:14 -08004724 * @param timeout Waiting period to wait for the data to be read,
4725 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004726 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004727 * @retval 0 At least @a min_xfer bytes of data were read.
Anas Nashif361a84d2019-06-16 08:22:08 -04004728 * @retval -EINVAL invalid parameters supplied
Allan Stephens9ef50f42016-11-16 15:33:31 -05004729 * @retval -EIO Returned without waiting; zero data bytes were read.
4730 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004731 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004732 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004733__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4734 size_t bytes_to_read, size_t *bytes_read,
Andy Ross78327382020-03-05 15:18:14 -08004735 size_t min_xfer, k_timeout_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004736
4737/**
Christopher Friedt3315f8f2020-05-06 18:43:58 -04004738 * @brief Query the number of bytes that may be read from @a pipe.
4739 *
4740 * @param pipe Address of the pipe.
4741 *
4742 * @retval a number n such that 0 <= n <= @ref k_pipe.size; the
4743 * result is zero for unbuffered pipes.
4744 */
4745__syscall size_t k_pipe_read_avail(struct k_pipe *pipe);
4746
4747/**
4748 * @brief Query the number of bytes that may be written to @a pipe
4749 *
4750 * @param pipe Address of the pipe.
4751 *
4752 * @retval a number n such that 0 <= n <= @ref k_pipe.size; the
4753 * result is zero for unbuffered pipes.
4754 */
4755__syscall size_t k_pipe_write_avail(struct k_pipe *pipe);
4756
Anas Nashif166f5192018-02-25 08:02:36 -06004757/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004758
Allan Stephensc98da842016-11-11 15:45:03 -05004759/**
4760 * @cond INTERNAL_HIDDEN
4761 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004762
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004763struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004764 _wait_q_t wait_q;
Nicolas Pitre2bed37e2021-04-13 11:10:22 -04004765 struct k_spinlock lock;
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004766 uint32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004767 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004768 char *buffer;
4769 char *free_list;
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004770 uint32_t num_used;
Kamil Lazowski104f1002020-09-11 14:27:55 +02004771#ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
4772 uint32_t max_used;
4773#endif
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004774
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004775 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004776 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004777};
4778
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004779#define Z_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004780 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004781 { \
Nicolas Pitre2bed37e2021-04-13 11:10:22 -04004782 .lock = {}, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004783 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004784 .num_blocks = slab_num_blocks, \
4785 .block_size = slab_block_size, \
4786 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004787 .free_list = NULL, \
4788 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004789 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004790 }
4791
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004792
Peter Mitsis578f9112016-10-07 13:50:31 -04004793/**
Allan Stephensc98da842016-11-11 15:45:03 -05004794 * INTERNAL_HIDDEN @endcond
4795 */
4796
4797/**
4798 * @defgroup mem_slab_apis Memory Slab APIs
4799 * @ingroup kernel_apis
4800 * @{
4801 */
4802
4803/**
Allan Stephensda827222016-11-09 14:23:58 -06004804 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004805 *
Allan Stephensda827222016-11-09 14:23:58 -06004806 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004807 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004808 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4809 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004810 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004811 *
Allan Stephensda827222016-11-09 14:23:58 -06004812 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004813 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004814 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004815 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004817 * @param name Name of the memory slab.
4818 * @param slab_block_size Size of each memory block (in bytes).
4819 * @param slab_num_blocks Number memory blocks.
4820 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04004821 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004822#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004823 char __noinit __aligned(WB_UP(slab_align)) \
4824 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004825 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Anas Nashif45a1d8a2020-04-24 11:29:17 -04004826 Z_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004827 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004828
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004829/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004830 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004832 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004833 *
Allan Stephensda827222016-11-09 14:23:58 -06004834 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4835 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004836 * N-byte boundary matching a word boundary, where N is a power of 2
4837 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004838 * To ensure that each memory block is similarly aligned to this boundary,
4839 * @a slab_block_size must also be a multiple of N.
4840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004841 * @param slab Address of the memory slab.
4842 * @param buffer Pointer to buffer used for the memory blocks.
4843 * @param block_size Size of each memory block (in bytes).
4844 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004845 *
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004846 * @retval 0 on success
4847 * @retval -EINVAL invalid data supplied
4848 *
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004849 */
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004850extern int k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004851 size_t block_size, uint32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004852
4853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004854 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004856 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004857 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004858 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
Krzysztof Chruscinskic482a572021-04-19 10:52:34 +02004859 * @note When CONFIG_MULTITHREADING=n any @a timeout is treated as K_NO_WAIT.
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004860 *
4861 * @funcprops \isr_ok
Spoorthy Priya Yerabolu04d3c3c2020-09-17 02:54:50 -07004862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004863 * @param slab Address of the memory slab.
4864 * @param mem Pointer to block address area.
Andy Ross78327382020-03-05 15:18:14 -08004865 * @param timeout Non-negative waiting period to wait for operation to complete.
4866 * Use K_NO_WAIT to return without waiting,
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004867 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004868 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004869 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004870 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004871 * @retval -ENOMEM Returned without waiting.
4872 * @retval -EAGAIN Waiting period timed out.
Anas Nashifdfc2bbc2019-06-16 09:22:21 -04004873 * @retval -EINVAL Invalid data supplied
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004874 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004875extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Andy Ross78327382020-03-05 15:18:14 -08004876 k_timeout_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004877
4878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004879 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004881 * This routine releases a previously allocated memory block back to its
4882 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004884 * @param slab Address of the memory slab.
4885 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004886 *
4887 * @return N/A
4888 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004889extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004890
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004891/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004892 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004894 * This routine gets the number of memory blocks that are currently
4895 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004897 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004899 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004900 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004901static inline uint32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004902{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004903 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004904}
4905
Peter Mitsisc001aa82016-10-13 13:53:37 -04004906/**
Kamil Lazowski104f1002020-09-11 14:27:55 +02004907 * @brief Get the number of maximum used blocks so far in a memory slab.
4908 *
4909 * This routine gets the maximum number of memory blocks that were
4910 * allocated in @a slab.
4911 *
4912 * @param slab Address of the memory slab.
4913 *
4914 * @return Maximum number of allocated memory blocks.
4915 */
4916static inline uint32_t k_mem_slab_max_used_get(struct k_mem_slab *slab)
4917{
4918#ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
4919 return slab->max_used;
4920#else
4921 ARG_UNUSED(slab);
4922 return 0;
4923#endif
4924}
4925
4926/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004927 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004929 * This routine gets the number of memory blocks that are currently
4930 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004932 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004934 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004935 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05004936static inline uint32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004937{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004938 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004939}
4940
Anas Nashif166f5192018-02-25 08:02:36 -06004941/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004942
4943/**
Anas Nashifdbac76f2020-12-09 12:04:53 -05004944 * @addtogroup heap_apis
Allan Stephensc98da842016-11-11 15:45:03 -05004945 * @{
4946 */
4947
Andrew Boieb95e9602020-09-28 13:26:38 -07004948/* kernel synchronized heap struct */
4949
4950struct k_heap {
4951 struct sys_heap heap;
4952 _wait_q_t wait_q;
4953 struct k_spinlock lock;
4954};
4955
Allan Stephensc98da842016-11-11 15:45:03 -05004956/**
Andy Ross0dd83b82020-04-03 10:01:03 -07004957 * @brief Initialize a k_heap
4958 *
4959 * This constructs a synchronized k_heap object over a memory region
4960 * specified by the user. Note that while any alignment and size can
4961 * be passed as valid parameters, internal alignment restrictions
4962 * inside the inner sys_heap mean that not all bytes may be usable as
4963 * allocated memory.
4964 *
4965 * @param h Heap struct to initialize
4966 * @param mem Pointer to memory.
4967 * @param bytes Size of memory region, in bytes
4968 */
4969void k_heap_init(struct k_heap *h, void *mem, size_t bytes);
4970
Maximilian Bachmann34d7c782020-11-13 15:12:31 +01004971/** @brief Allocate aligned memory from a k_heap
4972 *
4973 * Behaves in all ways like k_heap_alloc(), except that the returned
4974 * memory (if available) will have a starting address in memory which
4975 * is a multiple of the specified power-of-two alignment value in
4976 * bytes. The resulting memory can be returned to the heap using
4977 * k_heap_free().
4978 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004979 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
Krzysztof Chruscinskic482a572021-04-19 10:52:34 +02004980 * @note When CONFIG_MULTITHREADING=n any @a timeout is treated as K_NO_WAIT.
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01004981 *
4982 * @funcprops \isr_ok
Maximilian Bachmann34d7c782020-11-13 15:12:31 +01004983 *
4984 * @param h Heap from which to allocate
4985 * @param align Alignment in bytes, must be a power of two
4986 * @param bytes Number of bytes requested
4987 * @param timeout How long to wait, or K_NO_WAIT
4988 * @return Pointer to memory the caller can now use
4989 */
4990void *k_heap_aligned_alloc(struct k_heap *h, size_t align, size_t bytes,
4991 k_timeout_t timeout);
4992
Andy Ross0dd83b82020-04-03 10:01:03 -07004993/**
4994 * @brief Allocate memory from a k_heap
4995 *
4996 * Allocates and returns a memory buffer from the memory region owned
4997 * by the heap. If no memory is available immediately, the call will
4998 * block for the specified timeout (constructed via the standard
4999 * timeout API, or K_NO_WAIT or K_FOREVER) waiting for memory to be
5000 * freed. If the allocation cannot be performed by the expiration of
5001 * the timeout, NULL will be returned.
5002 *
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01005003 * @note @a timeout must be set to K_NO_WAIT if called from ISR.
Krzysztof Chruscinskic482a572021-04-19 10:52:34 +02005004 * @note When CONFIG_MULTITHREADING=n any @a timeout is treated as K_NO_WAIT.
Gerard Marull-Paretas9de14e82021-03-04 19:50:02 +01005005 *
5006 * @funcprops \isr_ok
Spoorthy Priya Yerabolu04d3c3c2020-09-17 02:54:50 -07005007 *
Andy Ross0dd83b82020-04-03 10:01:03 -07005008 * @param h Heap from which to allocate
5009 * @param bytes Desired size of block to allocate
5010 * @param timeout How long to wait, or K_NO_WAIT
5011 * @return A pointer to valid heap memory, or NULL
5012 */
Maximilian Bachmann34d7c782020-11-13 15:12:31 +01005013static inline void *k_heap_alloc(struct k_heap *h, size_t bytes,
5014 k_timeout_t timeout)
5015{
5016 return k_heap_aligned_alloc(h, sizeof(void *), bytes, timeout);
5017}
Andy Ross0dd83b82020-04-03 10:01:03 -07005018
5019/**
5020 * @brief Free memory allocated by k_heap_alloc()
5021 *
5022 * Returns the specified memory block, which must have been returned
5023 * from k_heap_alloc(), to the heap for use by other callers. Passing
5024 * a NULL block is legal, and has no effect.
5025 *
5026 * @param h Heap to which to return the memory
5027 * @param mem A valid memory block, or NULL
5028 */
5029void k_heap_free(struct k_heap *h, void *mem);
5030
5031/**
5032 * @brief Define a static k_heap
5033 *
5034 * This macro defines and initializes a static memory region and
5035 * k_heap of the requested size. After kernel start, &name can be
5036 * used as if k_heap_init() had been called.
5037 *
5038 * @param name Symbol name for the struct k_heap object
5039 * @param bytes Size of memory region, in bytes
5040 */
5041#define K_HEAP_DEFINE(name, bytes) \
5042 char __aligned(sizeof(void *)) kheap_##name[bytes]; \
5043 Z_STRUCT_SECTION_ITERABLE(k_heap, name) = { \
5044 .heap = { \
5045 .init_mem = kheap_##name, \
5046 .init_bytes = (bytes), \
5047 }, \
5048 }
5049
Johan Hedberg7d887cb2018-01-11 20:45:27 +02005050/**
Anas Nashif166f5192018-02-25 08:02:36 -06005051 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05005052 */
5053
5054/**
Anas Nashifdbac76f2020-12-09 12:04:53 -05005055 * @defgroup heap_apis Heap APIs
Allan Stephensc98da842016-11-11 15:45:03 -05005056 * @ingroup kernel_apis
5057 * @{
5058 */
5059
5060/**
Christopher Friedt135ffaf2020-11-26 08:19:10 -05005061 * @brief Allocate memory from the heap with a specified alignment.
5062 *
5063 * This routine provides semantics similar to aligned_alloc(); memory is
5064 * allocated from the heap with a specified alignment. However, one minor
5065 * difference is that k_aligned_alloc() accepts any non-zero @p size,
5066 * wherase aligned_alloc() only accepts a @p size that is an integral
5067 * multiple of @p align.
5068 *
5069 * Above, aligned_alloc() refers to:
5070 * C11 standard (ISO/IEC 9899:2011): 7.22.3.1
5071 * The aligned_alloc function (p: 347-348)
5072 *
5073 * @param align Alignment of memory requested (in bytes).
5074 * @param size Amount of memory requested (in bytes).
5075 *
5076 * @return Address of the allocated memory if successful; otherwise NULL.
5077 */
5078extern void *k_aligned_alloc(size_t align, size_t size);
5079
5080/**
5081 * @brief Allocate memory from the heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04005082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05005083 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05005084 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04005085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05005086 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04005087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05005088 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04005089 */
Christopher Friedt135ffaf2020-11-26 08:19:10 -05005090static inline void *k_malloc(size_t size)
5091{
5092 return k_aligned_alloc(sizeof(void *), size);
5093}
Peter Mitsis937042c2016-10-13 13:18:26 -04005094
5095/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05005096 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05005097 *
5098 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07005099 * returned must have been allocated from the heap memory pool or
5100 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04005101 *
Anas Nashif345fdd52016-12-20 08:36:04 -05005102 * If @a ptr is NULL, no operation is performed.
5103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05005104 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04005105 *
5106 * @return N/A
5107 */
5108extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005109
Allan Stephensc98da842016-11-11 15:45:03 -05005110/**
Andrew Boie7f95e832017-11-08 14:40:01 -08005111 * @brief Allocate memory from heap, array style
5112 *
5113 * This routine provides traditional calloc() semantics. Memory is
5114 * allocated from the heap memory pool and zeroed.
5115 *
5116 * @param nmemb Number of elements in the requested array
5117 * @param size Size of each array element (in bytes).
5118 *
5119 * @return Address of the allocated memory if successful; otherwise NULL.
5120 */
5121extern void *k_calloc(size_t nmemb, size_t size);
5122
Anas Nashif166f5192018-02-25 08:02:36 -06005123/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05005124
Benjamin Walshacc68c12017-01-29 18:57:45 -05005125/* polling API - PRIVATE */
5126
Benjamin Walshb0179862017-02-02 16:39:57 -05005127#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07005128#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05005129#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07005130#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05005131#endif
5132
Benjamin Walshacc68c12017-01-29 18:57:45 -05005133/* private - types bit positions */
5134enum _poll_types_bits {
5135 /* can be used to ignore an event */
5136 _POLL_TYPE_IGNORE,
5137
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07005138 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05005139 _POLL_TYPE_SIGNAL,
5140
5141 /* semaphore availability */
5142 _POLL_TYPE_SEM_AVAILABLE,
5143
Anas Nashif56821172020-07-08 14:14:25 -04005144 /* queue/FIFO/LIFO data availability */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02005145 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05005146
Nick Gravesb445f132021-04-12 12:35:18 -07005147 /* msgq data availability */
5148 _POLL_TYPE_MSGQ_DATA_AVAILABLE,
5149
Benjamin Walshacc68c12017-01-29 18:57:45 -05005150 _POLL_NUM_TYPES
5151};
5152
Aastha Grover83b9f692020-08-20 16:47:11 -07005153#define Z_POLL_TYPE_BIT(type) (1U << ((type) - 1U))
Benjamin Walshacc68c12017-01-29 18:57:45 -05005154
5155/* private - states bit positions */
5156enum _poll_states_bits {
5157 /* default state when creating event */
5158 _POLL_STATE_NOT_READY,
5159
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07005160 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05005161 _POLL_STATE_SIGNALED,
5162
5163 /* semaphore is available */
5164 _POLL_STATE_SEM_AVAILABLE,
5165
Anas Nashif56821172020-07-08 14:14:25 -04005166 /* data is available to read on queue/FIFO/LIFO */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02005167 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05005168
Anas Nashif56821172020-07-08 14:14:25 -04005169 /* queue/FIFO/LIFO wait was cancelled */
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03005170 _POLL_STATE_CANCELLED,
5171
Nick Gravesb445f132021-04-12 12:35:18 -07005172 /* data is available to read on a message queue */
5173 _POLL_STATE_MSGQ_DATA_AVAILABLE,
5174
Benjamin Walshacc68c12017-01-29 18:57:45 -05005175 _POLL_NUM_STATES
5176};
5177
Aastha Grover83b9f692020-08-20 16:47:11 -07005178#define Z_POLL_STATE_BIT(state) (1U << ((state) - 1U))
Benjamin Walshacc68c12017-01-29 18:57:45 -05005179
5180#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05005181 (32 - (0 \
5182 + 8 /* tag */ \
5183 + _POLL_NUM_TYPES \
5184 + _POLL_NUM_STATES \
5185 + 1 /* modes */ \
5186 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05005187
Benjamin Walshacc68c12017-01-29 18:57:45 -05005188/* end of polling API - PRIVATE */
5189
5190
5191/**
5192 * @defgroup poll_apis Async polling APIs
5193 * @ingroup kernel_apis
5194 * @{
5195 */
5196
5197/* Public polling API */
5198
5199/* public - values for k_poll_event.type bitfield */
5200#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07005201#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
5202#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
5203#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02005204#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Nick Gravesb445f132021-04-12 12:35:18 -07005205#define K_POLL_TYPE_MSGQ_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_MSGQ_DATA_AVAILABLE)
Benjamin Walshacc68c12017-01-29 18:57:45 -05005206
5207/* public - polling modes */
5208enum k_poll_modes {
5209 /* polling thread does not take ownership of objects when available */
5210 K_POLL_MODE_NOTIFY_ONLY = 0,
5211
5212 K_POLL_NUM_MODES
5213};
5214
5215/* public - values for k_poll_event.state bitfield */
5216#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07005217#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
5218#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
5219#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02005220#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Nick Gravesb445f132021-04-12 12:35:18 -07005221#define K_POLL_STATE_MSGQ_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_MSGQ_DATA_AVAILABLE)
Patrik Flykt4344e272019-03-08 14:19:05 -07005222#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05005223
5224/* public - poll signal object */
5225struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05005226 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03005227 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05005228
Anas Nashife71293e2019-12-04 20:00:14 -05005229 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05005230 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
5231 * user resets it to 0.
5232 */
5233 unsigned int signaled;
5234
Anas Nashife71293e2019-12-04 20:00:14 -05005235 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05005236 int result;
5237};
5238
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03005239#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05005240 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03005241 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05005242 .signaled = 0, \
5243 .result = 0, \
5244 }
Anas Nashife71293e2019-12-04 20:00:14 -05005245/**
5246 * @brief Poll Event
5247 *
5248 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05005249struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05005250 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03005251 sys_dnode_t _node;
5252
Anas Nashife71293e2019-12-04 20:00:14 -05005253 /** PRIVATE - DO NOT TOUCH */
Andy Ross202adf52020-11-10 09:54:49 -08005254 struct z_poller *poller;
Benjamin Walshacc68c12017-01-29 18:57:45 -05005255
Anas Nashife71293e2019-12-04 20:00:14 -05005256 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005257 uint32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05005258
Anas Nashife71293e2019-12-04 20:00:14 -05005259 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005260 uint32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05005261
Anas Nashife71293e2019-12-04 20:00:14 -05005262 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005263 uint32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05005264
Anas Nashife71293e2019-12-04 20:00:14 -05005265 /** mode of operation, from enum k_poll_modes */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005266 uint32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05005267
Anas Nashife71293e2019-12-04 20:00:14 -05005268 /** unused bits in 32-bit word */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005269 uint32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05005270
Anas Nashife71293e2019-12-04 20:00:14 -05005271 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05005272 union {
5273 void *obj;
5274 struct k_poll_signal *signal;
5275 struct k_sem *sem;
5276 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02005277 struct k_queue *queue;
Nick Gravesb445f132021-04-12 12:35:18 -07005278 struct k_msgq *msgq;
Benjamin Walshacc68c12017-01-29 18:57:45 -05005279 };
5280};
5281
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -07005282#define K_POLL_EVENT_INITIALIZER(_event_type, _event_mode, _event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05005283 { \
5284 .poller = NULL, \
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -07005285 .type = _event_type, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05005286 .state = K_POLL_STATE_NOT_READY, \
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -07005287 .mode = _event_mode, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05005288 .unused = 0, \
Daniel Leung087fb942021-03-24 12:45:01 -07005289 { \
5290 .obj = _event_obj, \
5291 }, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05005292 }
5293
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -07005294#define K_POLL_EVENT_STATIC_INITIALIZER(_event_type, _event_mode, _event_obj, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05005295 event_tag) \
5296 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05005297 .tag = event_tag, \
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -07005298 .type = _event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05005299 .state = K_POLL_STATE_NOT_READY, \
Spoorthy Priya Yerabolu9247e8b2020-08-25 03:11:16 -07005300 .mode = _event_mode, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05005301 .unused = 0, \
Daniel Leung087fb942021-03-24 12:45:01 -07005302 { \
5303 .obj = _event_obj, \
5304 }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05005305 }
5306
5307/**
5308 * @brief Initialize one struct k_poll_event instance
5309 *
5310 * After this routine is called on a poll event, the event it ready to be
5311 * placed in an event array to be passed to k_poll().
5312 *
5313 * @param event The event to initialize.
5314 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
5315 * values. Only values that apply to the same object being polled
5316 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
5317 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03005318 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005319 * @param obj Kernel object or poll signal.
5320 *
5321 * @return N/A
5322 */
5323
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005324extern void k_poll_event_init(struct k_poll_event *event, uint32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05005325 int mode, void *obj);
5326
5327/**
5328 * @brief Wait for one or many of multiple poll events to occur
5329 *
5330 * This routine allows a thread to wait concurrently for one or many of
5331 * multiple poll events to have occurred. Such events can be a kernel object
5332 * being available, like a semaphore, or a poll signal event.
5333 *
5334 * When an event notifies that a kernel object is available, the kernel object
5335 * is not "given" to the thread calling k_poll(): it merely signals the fact
5336 * that the object was available when the k_poll() call was in effect. Also,
5337 * all threads trying to acquire an object the regular way, i.e. by pending on
5338 * the object, have precedence over the thread polling on the object. This
5339 * means that the polling thread will never get the poll event on an object
5340 * until the object becomes available and its pend queue is empty. For this
5341 * reason, the k_poll() call is more effective when the objects being polled
5342 * only have one thread, the polling thread, trying to acquire them.
5343 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03005344 * When k_poll() returns 0, the caller should loop on all the events that were
5345 * passed to k_poll() and check the state field for the values that were
5346 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005347 *
5348 * Before being reused for another call to k_poll(), the user has to reset the
5349 * state field to K_POLL_STATE_NOT_READY.
5350 *
Andrew Boie3772f772018-05-07 16:52:57 -07005351 * When called from user mode, a temporary memory allocation is required from
5352 * the caller's resource pool.
5353 *
Christian Taedcke7a7c4202020-06-30 12:02:14 +02005354 * @param events An array of events to be polled for.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005355 * @param num_events The number of events in the array.
Andy Ross78327382020-03-05 15:18:14 -08005356 * @param timeout Waiting period for an event to be ready,
5357 * or one of the special values K_NO_WAIT and K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005358 *
5359 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005360 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03005361 * @retval -EINTR Polling has been interrupted, e.g. with
5362 * k_queue_cancel_wait(). All output events are still set and valid,
5363 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
5364 * words, -EINTR status means that at least one of output events is
5365 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07005366 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
5367 * @retval -EINVAL Bad parameters (user mode only)
Benjamin Walshacc68c12017-01-29 18:57:45 -05005368 */
5369
Andrew Boie3772f772018-05-07 16:52:57 -07005370__syscall int k_poll(struct k_poll_event *events, int num_events,
Andy Ross78327382020-03-05 15:18:14 -08005371 k_timeout_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05005372
5373/**
Benjamin Walsha304f162017-02-02 16:46:09 -05005374 * @brief Initialize a poll signal object.
5375 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07005376 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05005377 *
Anas Nashifb503be22021-03-22 08:09:55 -04005378 * @param sig A poll signal.
Benjamin Walsha304f162017-02-02 16:46:09 -05005379 *
5380 * @return N/A
5381 */
5382
Anas Nashifb503be22021-03-22 08:09:55 -04005383__syscall void k_poll_signal_init(struct k_poll_signal *sig);
Andrew Boie3772f772018-05-07 16:52:57 -07005384
5385/*
5386 * @brief Reset a poll signal object's state to unsignaled.
5387 *
Anas Nashifb503be22021-03-22 08:09:55 -04005388 * @param sig A poll signal object
Andrew Boie3772f772018-05-07 16:52:57 -07005389 */
Anas Nashifb503be22021-03-22 08:09:55 -04005390__syscall void k_poll_signal_reset(struct k_poll_signal *sig);
Andrew Boie3772f772018-05-07 16:52:57 -07005391
Anas Nashifb503be22021-03-22 08:09:55 -04005392static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *sig)
Andrew Boie3772f772018-05-07 16:52:57 -07005393{
Anas Nashifb503be22021-03-22 08:09:55 -04005394 sig->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07005395}
5396
5397/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07005398 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07005399 *
Anas Nashifb503be22021-03-22 08:09:55 -04005400 * @param sig A poll signal object
Andrew Boie3772f772018-05-07 16:52:57 -07005401 * @param signaled An integer buffer which will be written nonzero if the
5402 * object was signaled
5403 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07005404 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07005405 * value if it was not.
5406 */
Anas Nashifb503be22021-03-22 08:09:55 -04005407__syscall void k_poll_signal_check(struct k_poll_signal *sig,
Andrew Boie3772f772018-05-07 16:52:57 -07005408 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05005409
5410/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05005411 * @brief Signal a poll signal object.
5412 *
5413 * This routine makes ready a poll signal, which is basically a poll event of
5414 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
5415 * made ready to run. A @a result value can be specified.
5416 *
5417 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07005418 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07005419 * k_poll_signal_reset(). It thus has to be reset by the user before being
5420 * passed again to k_poll() or k_poll() will consider it being signaled, and
5421 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005422 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05005423 * @note The result is stored and the 'signaled' field is set even if
5424 * this function returns an error indicating that an expiring poll was
5425 * not notified. The next k_poll() will detect the missed raise.
5426 *
Anas Nashifb503be22021-03-22 08:09:55 -04005427 * @param sig A poll signal.
Benjamin Walshacc68c12017-01-29 18:57:45 -05005428 * @param result The value to store in the result field of the signal.
5429 *
5430 * @retval 0 The signal was delivered successfully.
5431 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
5432 */
5433
Anas Nashifb503be22021-03-22 08:09:55 -04005434__syscall int k_poll_signal_raise(struct k_poll_signal *sig, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05005435
Anas Nashif954d5502018-02-25 08:37:28 -06005436/**
5437 * @internal
5438 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005439extern void z_handle_obj_poll_events(sys_dlist_t *events, uint32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05005440
Anas Nashif166f5192018-02-25 08:02:36 -06005441/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05005442
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005443/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05005444 * @defgroup cpu_idle_apis CPU Idling APIs
5445 * @ingroup kernel_apis
5446 * @{
5447 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05005448/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005449 * @brief Make the CPU idle.
5450 *
5451 * This function makes the CPU idle until an event wakes it up.
5452 *
5453 * In a regular system, the idle thread should be the only thread responsible
5454 * for making the CPU idle and triggering any type of power management.
5455 * However, in some more constrained systems, such as a single-threaded system,
5456 * the only thread would be responsible for this if needed.
5457 *
Ioannis Glaropoulos91f6d982020-03-18 23:56:56 +01005458 * @note In some architectures, before returning, the function unmasks interrupts
5459 * unconditionally.
5460 *
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005461 * @return N/A
5462 */
Andrew Boie07525a32019-09-21 16:17:23 -07005463static inline void k_cpu_idle(void)
5464{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005465 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07005466}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005467
5468/**
5469 * @brief Make the CPU idle in an atomic fashion.
5470 *
Peter Bigot88e756e2020-09-29 10:43:10 -05005471 * Similar to k_cpu_idle(), but must be called with interrupts locked.
5472 *
5473 * Enabling interrupts and entering a low-power mode will be atomic,
5474 * i.e. there will be no period of time where interrupts are enabled before
5475 * the processor enters a low-power mode.
5476 *
5477 * After waking up from the low-power mode, the interrupt lockout state will
5478 * be restored as if by irq_unlock(key).
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005479 *
5480 * @param key Interrupt locking key obtained from irq_lock().
5481 *
5482 * @return N/A
5483 */
Andrew Boie07525a32019-09-21 16:17:23 -07005484static inline void k_cpu_atomic_idle(unsigned int key)
5485{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005486 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07005487}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05005488
Anas Nashif30c3cff2019-01-22 08:18:13 -05005489/**
5490 * @}
5491 */
Anas Nashif954d5502018-02-25 08:37:28 -06005492
5493/**
5494 * @internal
5495 */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005496#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02005497/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005498#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005499#else
5500
Joakim Anderssone04e4c22019-12-20 15:42:38 +01005501#if !defined(CONFIG_ASSERT_NO_FILE_INFO)
5502#define __EXCEPT_LOC() __ASSERT_PRINT("@ %s:%d\n", __FILE__, __LINE__)
5503#else
5504#define __EXCEPT_LOC()
5505#endif
5506
Andrew Boiecdb94d62017-04-18 15:22:05 -07005507/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005508 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005509 *
5510 * We won't have a real exception frame to determine the PC value when
5511 * the oops occurred, so print file and line number before we jump into
5512 * the fatal error handler.
5513 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005514#define z_except_reason(reason) do { \
Joakim Anderssone04e4c22019-12-20 15:42:38 +01005515 __EXCEPT_LOC(); \
Andrew Boie56236372019-07-15 15:22:29 -07005516 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07005517 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005518
5519#endif /* _ARCH__EXCEPT */
5520
5521/**
5522 * @brief Fatally terminate a thread
5523 *
5524 * This should be called when a thread has encountered an unrecoverable
5525 * runtime condition and needs to terminate. What this ultimately
5526 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005527 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005528 *
5529 * If this is called from ISR context, the default system fatal error handler
5530 * will treat it as an unrecoverable system error, just like k_panic().
5531 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005532#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005533
5534/**
5535 * @brief Fatally terminate the system
5536 *
5537 * This should be called when the Zephyr kernel has encountered an
5538 * unrecoverable runtime condition and needs to terminate. What this ultimately
5539 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005540 * will be called will reason code K_ERR_KERNEL_PANIC.
Andrew Boiecdb94d62017-04-18 15:22:05 -07005541 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07005542#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07005543
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005544/*
5545 * private APIs that are utilized by one or more public APIs
5546 */
5547
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09005548/**
5549 * @internal
5550 */
5551extern void z_init_thread_base(struct _thread_base *thread_base,
Kumar Galaa1b77fd2020-05-27 11:26:57 -05005552 int priority, uint32_t initial_state,
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09005553 unsigned int options);
5554
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005555#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06005556/**
5557 * @internal
5558 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005559extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005560#else
Anas Nashif954d5502018-02-25 08:37:28 -06005561/**
5562 * @internal
5563 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005564#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05005565#endif
5566
Anas Nashif954d5502018-02-25 08:37:28 -06005567/**
5568 * @internal
5569 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005570extern bool z_is_thread_essential(void);
Guennadi Liakhovetski8d07b772021-04-01 13:46:57 +02005571
5572#ifdef CONFIG_SMP
5573void z_smp_thread_init(void *arg, struct k_thread *thread);
5574void z_smp_thread_swap(void);
5575#endif
5576
Anas Nashif954d5502018-02-25 08:37:28 -06005577/**
5578 * @internal
5579 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005580extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005581
Andrew Boied76ae462020-01-02 11:57:43 -08005582#ifdef CONFIG_PRINTK
Andrew Boie756f9072017-10-10 16:01:49 -07005583/**
5584 * @brief Emit a character buffer to the console device
5585 *
5586 * @param c String of characters to print
5587 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005588 *
Andrew Boie756f9072017-10-10 16:01:49 -07005589 */
5590__syscall void k_str_out(char *c, size_t n);
Andrew Boied76ae462020-01-02 11:57:43 -08005591#endif
Andrew Boie756f9072017-10-10 16:01:49 -07005592
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005593/**
5594 * @brief Disable preservation of floating point context information.
5595 *
5596 * This routine informs the kernel that the specified thread
5597 * will no longer be using the floating point registers.
5598 *
5599 * @warning
5600 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005601 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005602 *
5603 * @warning
5604 * This routine should only be used to disable floating point support for
5605 * a thread that currently has such support enabled.
5606 *
5607 * @param thread ID of thread.
5608 *
Katsuhiro Suzuki19db4852021-03-24 01:54:15 +09005609 * @retval 0 On success.
5610 * @retval -ENOTSUP If the floating point disabling is not implemented.
5611 * -EINVAL If the floating point disabling could not be performed.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005612 */
5613__syscall int k_float_disable(struct k_thread *thread);
5614
Katsuhiro Suzuki59903e22021-02-01 15:16:53 +09005615/**
5616 * @brief Enable preservation of floating point context information.
5617 *
5618 * This routine informs the kernel that the specified thread
5619 * will use the floating point registers.
5620
5621 * Invoking this routine initializes the thread's floating point context info
5622 * to that of an FPU that has been reset. The next time the thread is scheduled
5623 * by z_swap() it will either inherit an FPU that is guaranteed to be in a
5624 * "sane" state (if the most recent user of the FPU was cooperatively swapped
5625 * out) or the thread's own floating point context will be loaded (if the most
5626 * recent user of the FPU was preempted, or if this thread is the first user
5627 * of the FPU). Thereafter, the kernel will protect the thread's FP context
5628 * so that it is not altered during a preemptive context switch.
5629 *
5630 * The @a options parameter indicates which floating point register sets will
5631 * be used by the specified thread.
5632 *
5633 * For x86 options:
5634 *
5635 * - K_FP_REGS indicates x87 FPU and MMX registers only
5636 * - K_SSE_REGS indicates SSE registers (and also x87 FPU and MMX registers)
5637 *
5638 * @warning
5639 * Some architectures apply restrictions on how the enabling of floating
5640 * point preservation may be requested, see arch_float_enable.
5641 *
5642 * @warning
5643 * This routine should only be used to enable floating point support for
5644 * a thread that currently has such support enabled.
5645 *
5646 * @param thread ID of thread.
5647 * @param options architecture dependent options
5648 *
5649 * @retval 0 On success.
5650 * @retval -ENOTSUP If the floating point enabling is not implemented.
5651 * -EINVAL If the floating point enabling could not be performed.
5652 */
5653__syscall int k_float_enable(struct k_thread *thread, unsigned int options);
5654
Daniel Leungfc577c42020-08-27 13:54:14 -07005655#ifdef CONFIG_THREAD_RUNTIME_STATS
5656
5657/**
5658 * @brief Get the runtime statistics of a thread
5659 *
5660 * @param thread ID of thread.
5661 * @param stats Pointer to struct to copy statistics into.
5662 * @return -EINVAL if null pointers, otherwise 0
5663 */
5664int k_thread_runtime_stats_get(k_tid_t thread,
5665 k_thread_runtime_stats_t *stats);
5666
5667/**
5668 * @brief Get the runtime statistics of all threads
5669 *
5670 * @param stats Pointer to struct to copy statistics into.
5671 * @return -EINVAL if null pointers, otherwise 0
5672 */
5673int k_thread_runtime_stats_all_get(k_thread_runtime_stats_t *stats);
5674
5675#endif
5676
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005677#ifdef __cplusplus
5678}
5679#endif
5680
Anas Nashif73008b42020-02-06 09:14:51 -05005681#include <tracing/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005682#include <syscalls/kernel.h>
5683
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005684#endif /* !_ASMLANGUAGE */
5685
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005686#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */